Problem: Fair Division

Description
Alice and Bob received n candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice’s candies is equal to the total weight of Bob’s candies.Check if they can do that.Note that candies are not allowed to be cut in half.
Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤100) — the number of candies that Alice and Bob received.

The next line contains n integers a1,a2,…,an — the weights of the candies. The weight of each candy is either 1 or 2.

It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case, output on a separate line:
“YES”, if all candies can be divided into two sets with the same weight;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Sample Input
5
2
1 1
2
1 2
4
1 2 1 2
3
2 2 2
3
2 1 2
Sample Output
YES
NO
YES
NO
NO
Hint
In the first test case, Alice and Bob can each take one candy, then both will have a total weight of 1.
In the second test case, any division will be unfair.
In the third test case, both Alice and Bob can take two candies, one of weight 1 and one of weight 2.
In the fourth test case, it is impossible to divide three identical candies between two people.
In the fifth test case, any division will also be unfair.
这题呢,就是比较1的个数;
如果1的个数是奇数,那么必不可能平均分;
如果1的个数是偶数,且它不为0,一定可以;
如果全是2:
2的个数为奇数,不行;
2的个数为偶数,可以;
代码如下:

#include<stdio.h>
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int a[n];
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		int c=0;
		for(int i=0;i<n;i++)
		{
			if(a[i]==1) c++;
		}
		if(c%2==0&&c!=0) printf("YES\n");
		else  if(c%2!=0&&c!=0)     printf("NO\n");
		if(c==0&&n%2!=0) printf("NO\n");
		else if(c==0&&n%2==0)	printf("YES\n");
	}
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZSTU_呆大鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值