https://vjudge.net/contest/67836#problem/I

Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

Input

The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.

Output

For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".

Sample Input

3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

Sample Output

yes
no
yes

代码:

#include <bits/stdc++.h>
using namespace std; int n, ave, maxx, sum;
int a[50];
bool flag = false;
int vis[50]; void dfs(int num, int len, int start) {
if(flag)
return; if(num == 4) {
flag = true;
return ;
} if(len == ave) {
dfs(num + 1, 0, 1);
if(flag)
return ;
} for(int i = start; i <= n; i ++) {
if(vis[i] == 0 && len + a[i] <= ave) {
vis[i] = 1;
dfs(num, len + a[i], i + 1);
vis[i] = 0;
if(flag)
return ;
}
}
} int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%d", &n);
maxx = 0, sum = 0;
for(int i = 1; i <= n; i ++) {
scanf("%d", &a[i]);
sum += a[i];
}
sort(a + 1, a + 1 + n);
maxx = a[n];
ave = sum / 4;
if(sum % 4 != 0 || maxx > ave) {
printf("no\n");
continue;
}
memset(vis, 0, sizeof(vis));
flag = false;
dfs(0, 0, 1);
if(flag)
printf("yes\n");
else
printf("no\n");
}
return 0;
}

  写这个专题的时候想到暑假被搜索支配的恐惧 写不出来或者很紧张的时候就很喜欢听《最佳歌手》

最新文章

  1. App.xaml.cs
  2. Android 学习笔记之AndBase框架学习(四) 使用封装好的函数实现单,多线程任务
  3. jq 的连续动画
  4. 一张png图片 上面有多个图标,如何用CSS准确的知道其中某个图片的坐标
  5. Codeforces Gym 100015B Ball Painting 找规律
  6. activity+fragment多次切换出现页面空白问题
  7. [AngularJS] Use ng-model-options to limit $digest
  8. hdu 2106
  9. 方案:抵御 不明SSL证书导致的 中间人攻击
  10. ng自定义一个过滤器
  11. 利用python破解sqlserver账号密码
  12. 使用java实现快速排序(挖坑填数法和指针交换法)
  13. maven pom添加本地jar,不提交私库
  14. [QTree6]Query on a tree VI
  15. Spark机器学习(2):逻辑回归算法
  16. LeetCode 12. Integer to RomanLeetCode
  17. setTimeout闭包常见问题
  18. vue入门全局配置
  19. 以快板之名说Android 应用程序电源管理
  20. java节假日api--关于节假日想到的

热门文章

  1. 第8章 ZooKeeper操作
  2. 2.2 vivi虚拟视频驱动测试
  3. Lambda表达式的语法与如何使用Lambda表达式
  4. java 对象的初始化流程(静态成员、静态代码块、普通代码块、构造方法)
  5. Java设计模式(23)——行为模式之访问者模式(Visitor)
  6. 北京Uber优步司机奖励政策(3月20日)
  7. 长沙Uber优步司机奖励政策(12月14日到12月20日)
  8. QtChart 初体验
  9. jsp传递参数的四种方法
  10. php api_token 与 user_token 简析