You are given an integer
. So,
For example, if
and then.
Determine whether
is even or odd.
The first line contains two integers
) — the base of the number and the number of digits.
The second line contains
integers () — the digits of.
The representation of
contains no unnecessary leading zero. That is, can be equal to only if.
Print "even" if
is even, otherwise print "odd".
You can print each letter in any case (upper or lower).
13 3
3 2 7
even
10 9
1 2 3 4 5 6 7 8 9
odd
99 5
32 92 85 74 4
odd
2 2
1 0
even
In the first example,
, which is even.
In the second example,
is odd.
In the third example,
is odd.
In the fourth example
처음에 시도해본 언어는 python.
그런데 난데 없는 시간초과가 자꾸 뜨길래
c++로 도전! 그러나 test19에서 에러가 떴다.
무슨 숫자든 다 %10 해주어 1의 자리수만 연산하여 홀짝을 판별하는 알고리즘이어서 당연히 맞을 줄 알았는데;;
그래서 Round종료후 1시간동안 끙끙 머리를 싸맨 결과
홀+홀 = 짝
홀+짝 = 홀
짝+짝 = 짝
개념을 쓰면 된다는 것을 알았다.
b와 a마지막 들은 다 홀수이면 1 짝수이면 0
그리고 마지막엔 b가 1이므로 a의 마지막 원소만 더해주어
홀짝을 판별하는 알고리즘을 생각해내었다. (백준과는 다르게 솔루션이 없어서 너무 힘들었다..)
아래는 c++소스코드이다.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
int b, k;
vector<int> a;
cin >> b >> k;
for (int i = 0; i < k; i++) {
int temp;
cin >> temp;
a.push_back(temp);
}
if (b % 2 == 0) {
b = 0;
}
else {
b = 1;
}
auto iter_a = a.begin();
long long int sum = 0;
for (iter_a; iter_a != a.end()-1; iter_a++) {
if (*iter_a % 2 == 1) {
sum += (*iter_a * b);
}
}
sum += *(a.end()-1);
if (sum % 2 == 0) {
cout << "even";
}
else {
cout << "odd";
}
return 0;
}
'개발 > 알고리즘' 카테고리의 다른 글
합병정렬 (0) | 2019.03.19 |
---|---|
python 속도 높이기 팁 (0) | 2019.03.18 |
4949 The Balance of the World (0) | 2019.01.24 |
2312 수 복원하기 (0) | 2019.01.24 |
1074번 Z (0) | 2019.01.15 |