Description

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".
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
yes
no
yes

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int data[250],state[250];
int ave,N,M,sum;
int dfs(int x,int pos,int len)
{
int i;
if(x==3)//如果有三条边都满足,结束
return 1;
for(i=pos;i>=0;i--){
if(!state[i]){
state[i]=1;
if(len+data[i]<ave){
if(dfs(x,i-1,len+data[i]))//i-1的作用是剪枝
return 1;
}
if(len+data[i]==ave){
if(dfs(x+1,M-1,0))
return 1;
}
state[i]=0;
}
}
return 0;
}
int main ()
{
scanf("%d",&N);
while(N--){
sum=0;
memset(state,0,sizeof(state));
//memset(data,0,sizeof(data));
scanf("%d",&M);
for(int i=0;i<M;sum+=data[i],i++)
scanf("%d",&data[i]);
ave=sum/4;
if(M<4||ave*4!=sum||ave<data[M-1]){
printf("no\n");
continue;
}
if(dfs(0,M-1,0))
printf("yes\n");
else
printf("no\n"); }
return 0;
}

最新文章

  1. AS3深拷贝数据对象(2)深拷贝VO对象
  2. Android模拟器问题:No system images installed for this target
  3. Mysql字符集设置 2 图
  4. DataTable操作(建表,建行,建列,添加数据)
  5. 【转】Entity Framework教程
  6. [置顶] Win8.1慎用360优化,可能导致安装驱动出现数据无效的问题。附解决方法
  7. 查看linux/AIX系统内存及CPU占用百分比
  8. Quartz 2D 概述
  9. 枚举+搜索 hdu-4431-Mahjong
  10. Bootstrap3.0(进度条、媒体对象、列表组、面板)
  11. c#基础——for循环嵌套经典练习题(打★)
  12. RSA加密算法 C++实现
  13. Centos7 下 tty2等文字窗口的中文乱码问题分析
  14. 深入css布局篇(1) — 盒模型 &amp; 元素分类
  15. spawn-fcgi启动的一些报错问题
  16. NEO-Karl-Python1
  17. 20175202 《Java程序设计》第八周学习总结
  18. HDU 2191 - 单调队列优化多重背包
  19. 去掉a标签
  20. Java之IO(十四)IO包中其它类

热门文章

  1. VBS调用windows api函数(postmessage)实现后台发送按键脚本
  2. Chapter 2 Open Book——22
  3. leetcode24,交换链表相邻的节点
  4. hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
  5. .net文件上传,客户端用jquery file upload
  6. 百度前端面试题-类似slack的在线聊天室
  7. fp oo
  8. Android:SQLite无法update/insert/delete数据(数据库被locked)
  9. MFC 透明内存DC
  10. UIImagePickerController 相关