《codeforces刷题实录——1296A.Array with Odd Sum》

我只是一个虾纸丫 提交于 2020-02-10 16:35:33

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 \leq i,j \leq n such that i \neq j and set aia_i:=aja_j. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations.

在一次移动中,你可以选择两个索引1 \leq i,j \leqn,使i \neq j,并设置aia_i:=aja_j你可以执行这样的移动任意次数(可能为零)。你可以在不同的操作中选择不同的索引。

The operation := is the operation of assignment (i.e. you choose i and j and replace aia_i with aja_j).

运算:=是赋值运算(即选择i和j,并将aia_i替换为aja_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 \leq t \leq 2000) — the number of test cases.

输入的第一行包含一个整数 t(1 \leq t \leq 2000)——测试用例的数量。

The next 2t lines describe test cases. The first line of the test case contains one integer n (1 \leq n \leq 2000) — the number of elements in a. The second line of the test case contains n integers a1a_1,a2a_2,…,ana_n (1 \leq aia_i \leq 2000), where aia_i is the i-th element of a.

接下来的2t行描述测试用例,每个测试用例占2行。
每个测试用例的第一行包含一个整数n(1 \leq n \leq 2000)——a中元素的数目;
第二行包含n个整数a1a_1a2a_2,…,ana_n(1 \leq aia_i \leq 2000),其中**aia_i是a的第i个元素**。

It is guaranteed that the sum of n over all test cases does not exceed 2000 (\sumn \leq 2000).

保证所有测试用例的n之和不超过2000(\sumn \leq 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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!