![[백준 / BOJ] 14681번 사분면 고르기 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FTPrxH%2FbtrcOSWTp4Z%2FUjLyar7mlRFUCG9iCOMSjk%2Fimg.png)
[백준 / BOJ] 14681번 사분면 고르기 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2021. 8. 22. 21:52
반응형
링크 : https://www.acmicpc.net/problem/14681
14681번: 사분면 고르기
점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.
www.acmicpc.net
문제

문제 풀이
다시 풀었을 때 삼항 연산자를 사용해보았다.
C++ 상세 풀이
C++ 풀이 펼쳐보기
이전 문제에서 사용했던 삼항 연산자를 그대로 활용해보았다. 한 줄로 간단하게 풀이된다.
std::cout << (x > 0 ? (y > 0 ? 1 : 4) : (y > 0 ? 2 : 3));
Python 상세 풀이
Python 풀이 펼쳐보기
이전 문제에서 사용했던 삼항 연산자를 활용해보고 싶었지만 조건 1이 참일 때 다시금 조건 하나를 더 주는 법을 모르겠어서 if-else문을 사용했다.
if x > 0:
print(1 if y > 0 else 4)
else:
print(2 if y > 0 else 3)
코드 전문
C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int x, y; | |
std::cin >> x >> y; | |
std::cout << (x > 0 ? (y > 0 ? 1 : 4) : (y > 0 ? 2 : 3)); | |
return 0; | |
} |
+ 이전 풀이
더보기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main() | |
{ | |
int x, y; | |
std::cin >> x >> y; | |
if (x > 0 && y > 0) | |
std::cout << "1"; | |
else if (x < 0 && y > 0) | |
std::cout << "2"; | |
else if (x < 0 && y < 0) | |
std::cout << "3"; | |
else | |
std::cout << "4"; | |
return 0; | |
} |
Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin.readline | |
if __name__ == "__main__": | |
x = int(input()); y = int(input()) | |
if x > 0: | |
print(1 if y > 0 else 4) | |
else: | |
print(2 if y > 0 else 3) |
+ 이전 풀이
더보기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = int(input()) | |
y = int(input()) | |
if x > 0 and y > 0: | |
print("1") | |
elif x < 0 and y > 0: | |
print("2") | |
elif x < 0 and y < 0: | |
print("3") | |
else: | |
print("4") |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 10950번 A+B - 3 (C++, Python) (0) | 2021.08.22 |
---|---|
[백준 / BOJ] 2739번 구구단 (C++, Python) (0) | 2021.08.22 |
[백준 / BOJ] 2884번 알람 시계 (C++, Python) (0) | 2021.08.22 |
[백준 / BOJ] 2753번 윤년 (C++, Python) (0) | 2021.08.21 |
[백준 / BOJ] 9498번 시험 성적 (C++, Python) (0) | 2021.08.21 |
@Reo :: 코드 아카이브
자기계발 블로그