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".

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

这题题意是给你一些边,看能够构成正方形,这题的数据比较水,为后面的poj1011埋下伏笔。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int vis[30],liang,a[30],n;//liang表示每条边的长
bool cmp(int a,int b){
return a<b;
}
int dfs(int x,int pos,int len)//x表示已经拼了几根,pos表示下次从哪根开始拼,len表示当前拼的这根已经拼了多少长度
{
int i,j;
if(x==3)return 1;
for(i=pos;i>=1;i--){
if(!vis[i]){
if(a[i]+len<liang){
vis[i]=1;
if(dfs(x,i-1,len+a[i]))
return 1;
vis[i]=0;
}
else if(a[i]+len==liang){
vis[i]=1;
if(dfs(x+1,n,0))return 1;
vis[i]=0; }
}
}
return 0;
} int main()
{
int m,i,j,T,sum;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum%4!=0){
printf("no\n");continue;
}
liang=sum/4;
sort(a+1,a+1+n,cmp);
memset(vis,0,sizeof(vis));
if(dfs(0,n,0))printf("yes\n");
else printf("no\n");
}
return 0;
}

最新文章

  1. 文件权限及chmod使用方法
  2. flash中字体兼容性
  3. Android adb的使用
  4. Ubuntu 16.04 下卸载 lnmp/lamp 方法
  5. PHP设计模式之装饰者模式
  6. C# 异步下载文件
  7. awk基础 [马哥视频]
  8. 不小心中了machook病毒
  9. 【Bootstrap3.0建站笔记二】button可下拉弹出层
  10. HTTP文件下载JAVA后台的实现
  11. Pycharm在运行过程中,查看每个变量的方法(show variables)跟终端一样显示变量
  12. 如何利用Python实现自动打卡签到
  13. matlab练习程序(加权最小二乘)
  14. Go语言编程 (许式伟 等 著)
  15. Luogu 2157 [SDOI2009]学校食堂 - 状压dp
  16. 第二阶段站立会议alpha版总结
  17. jQuery放大镜插件
  18. XJOI 3606 最大子矩形面积/LightOJ 1083 Histogram(单调栈/笛卡尔树)
  19. 五十三 网络编程 TCP/IP简介
  20. ES6 中 export ,export default 区别

热门文章

  1. 【ORA】ora-39700解决
  2. [WPF] 在单元测试中使用 Prism 的 EventAggregator,订阅到 ThreadOption.UIThread 会报错
  3. django使用缓存之drf-extensions
  4. 深度解读设备的“万能语言”HarmonyOS的分布式软总线能力
  5. Django orm中related_name/related_query_name区别
  6. 使用 .NETCore自带框架快速实现依赖注入
  7. Redis 核心篇:唯快不破的秘密
  8. 简单监控liunx中cpu、内存、磁盘及发送邮件参考
  9. Vue 3自定义指令开发
  10. Go RPC 框架 KiteX 性能优化实践 原创 基础架构团队 字节跳动技术团队 2021-01-18