codeforces刷题实录
1296A.Array with Odd Sum
奇数和数组
原题
time limit per test:1 second,1秒。
memory limit per test:256 megabytes,256兆字节。
input:inputstandard,标准输入。
output:outputstandard,标准输出。
You are given an array a consisting of n integers.
给你一个由n个整数组成的数组a。
In one move, you can choose two indices 1 i,j n such that i j and set :=. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations.
在一次移动中,你可以选择两个索引1 i,j n,使i j,并设置:=。你可以执行这样的移动任意次数(可能为零)。你可以在不同的操作中选择不同的索引。
The operation := is the operation of assignment (i.e. you choose i and j and replace with ).
运算:=是赋值运算(即选择i和j,并将替换为)。
Your task is to say if it is possible to obtain an array with an odd (not divisible by 2) sum of elements.
任务:你的任务是给出是否有可能得到一个奇数(不能被2整除)元素和的数组。
You have to answer t independent test cases.
你必须回答 t 个独立的测试用例。
Input 输入
The first line of the input contains one integer t (1 t 2000) — the number of test cases.
输入的第一行包含一个整数 t(1 t 2000)——测试用例的数量。
The next 2t lines describe test cases. The first line of the test case contains one integer n (1 n 2000) — the number of elements in a. The second line of the test case contains n integers ,,…, (1 2000), where is the i-th element of a.
接下来的2t行描述测试用例,每个测试用例占2行。
每个测试用例的第一行包含一个整数n(1 n 2000)——a中元素的数目;
第二行包含n个整数,,…,(1 2000),其中**是a的第i个元素**。
It is guaranteed that the sum of n over all test cases does not exceed 2000 (n 2000).
保证所有测试用例的n之和不超过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.
对于每个测试用例,如果数组内元素和为奇数,则在其上打印答案-“YES”(不带引号),否则打印“NO”。
Example 例
input
5
2
2 3
4
2 2 8 8
3
3 3 3
4
5 5 5 5
4
1 1 1 1
output
YES
NO
YES
NO
NO
简析
题意要求奇数和数组,且允许数组内元素互相替代任意次。
这就意味着数组内只要同时有奇数和偶数,就能满足奇数和数组。
仅有偶数时必然不能满足奇数和数组。
仅有奇数时,奇数的数量也为奇数,则能满足奇数和数组。
代码
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int a;
int odd = 0,even = 0;
while(n--){
scanf("%d",&a);
if(a % 2 == 0){
even++;
}else{
odd++;
}
}
if(odd % 2 != 0/*仅有奇数情形*/ ||
(odd && even)/*同时存在奇偶数*/){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}
来源:CSDN
作者:Iftty
链接:https://blog.csdn.net/Iftty/article/details/104246913