ZOJ 1909 I-Square
2024-10-01 00:45:23
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;
}
写这个专题的时候想到暑假被搜索支配的恐惧 写不出来或者很紧张的时候就很喜欢听《最佳歌手》
最新文章
- App.xaml.cs
- Android 学习笔记之AndBase框架学习(四) 使用封装好的函数实现单,多线程任务
- jq 的连续动画
- 一张png图片 上面有多个图标,如何用CSS准确的知道其中某个图片的坐标
- Codeforces Gym 100015B Ball Painting 找规律
- activity+fragment多次切换出现页面空白问题
- [AngularJS] Use ng-model-options to limit $digest
- hdu 2106
- 方案:抵御 不明SSL证书导致的 中间人攻击
- ng自定义一个过滤器
- 利用python破解sqlserver账号密码
- 使用java实现快速排序(挖坑填数法和指针交换法)
- maven pom添加本地jar,不提交私库
- [QTree6]Query on a tree VI
- Spark机器学习(2):逻辑回归算法
- LeetCode 12. Integer to RomanLeetCode
- setTimeout闭包常见问题
- vue入门全局配置
- 以快板之名说Android 应用程序电源管理
- java节假日api--关于节假日想到的