A. Array with Odd Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array 𝑎a consisting of 𝑛n integers.
In one move, you can choose two indices 1≤𝑖,𝑗≤𝑛1≤i,j≤n such that
𝑖≠𝑗i≠j and set 𝑎𝑖:=𝑎𝑗ai:=aj. You can perform such moves any
number of times (possibly, zero). You can choose different indices in
different operations. The operation := is the operation of assignment
(i.e. you choose 𝑖i and 𝑗j and replace 𝑎𝑖ai with 𝑎𝑗aj).Your task is to say if it is possible to obtain an array with an odd
(not divisible by 22) sum of elements.You have to answer 𝑡t independent test cases.
Input
The first line of the input contains one integer 𝑡t
(1≤𝑡≤20001≤t≤2000) — the number of test cases.The next 2𝑡2t lines describe test cases. The first line of the test
case contains one integer 𝑛n (1≤𝑛≤20001≤n≤2000) — the number of
elements in 𝑎a. The second line of the test case contains 𝑛n
integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤20001≤ai≤2000), where 𝑎𝑖ai
is the 𝑖i-th element of 𝑎a.It is guaranteed that the sum of 𝑛n over all test cases does not
exceed 20002000 (∑𝑛≤2000∑n≤2000).Output
For each test case, print the answer on it — “YES” (without quotes) if
it is possible to obtain the array with an odd sum of elements, and
“NO” otherwise.
Example
input
Copy
5
2
2 3
4
2 2 8 8
3
3 3 3
4
5 5 5 5
4
1 1 1 1
output
Copy
YES
NO
YES
NO
NO
思路如下
先判断:序列之和是不是一个 奇数,如果是直接输出,否则(为偶数时)再继续进行判断,我们想 把这个序列和 从“偶数” ——> 变为“奇数”?,其实我们只需要把数组里面的 一个偶数变成奇数就行了,这就要求,序列里面 至少 要有一个奇数 和一个 偶数,那么 这个时候就也可以输出 yes了,,其它的情况直接输出 No
题解如下
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
const int Len = 2005;
int ar[Len];
int main()
{
//freopen("T.txt","r",stdin);
int t;
scanf("%d",&t);
while(t --)
{
int sum = 0;
int n;
scanf("%d",&n);
int odd = 0,even = 0;
int tem;
for(int i = 0; i < n; i ++)
{
scanf("%d",&tem);
if(tem % 2 == 0)
odd = 1;
else
even = 1;
sum += tem;
}
if(sum % 2 != 0 || (odd && even))
cout<<"YES\n";
else
cout<<"NO\n";
}
return 0;
}