문제 링크입니다: www.acmicpc.net/problem/18005
18005번: Even or Odd?
Output 2 if the sum of any n consecutive integers in the range from 1 to 1018 must be even, 1 if the sum must be odd, or 0 if the sum could be either even or odd.
www.acmicpc.net
N이 홀수일 경우 합 -> 홀수/짝수 모두 가능
i) 홀수가 홀수개인 경우 -> 홀수
ii) 홀수가 짝수개인 경우 -> 짝수
N이 짝수일 경우 합 (가우스의 덧셈 법칙 이용)
ex) N=6, [1, 2, 3, 4, 5, 6]
ex) N=4, [1, 2, 3, 4]
i) N/2이 홀수일 경우 -> (1 + 6) + (2 + 5) + (3 + 4) -> 결국 동일한 합을 이루는 쌍이 홀수개이므로 홀수
ii) N/2이 짝수일 경우 -> (1 + 4) + (2 + 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
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int n; | |
cin >> n; | |
int result; | |
if (n % 2) | |
{ | |
result = 0; | |
} | |
else | |
{ | |
if ((n / 2) % 2) | |
{ | |
result = 1; | |
} | |
else | |
{ | |
result = 2; | |
} | |
} | |
cout << result << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 18408번 3 つの整数 (0) | 2021.03.27 |
---|---|
백준 18330번 Petrol (0) | 2021.03.27 |
백준 17903번 Counting Clauses (0) | 2021.03.26 |
백준 17874번 Piece of Cake! (0) | 2021.03.26 |
백준 17863번 FYI (0) | 2021.03.26 |