ACdream1726A Math game
来源:互联网 发布:淘宝女士春装 编辑:程序博客网 时间:2024/06/10 22:02
Recently, Losanto find an interesting Math game. The rule is simple: Tell you a number H, and you can choose some numbers from a set {a[1],a[2],……,a[n]}.If the sum of the number you choose is H, then you win. Losanto just want to know whether he can win the game.
Input
There are several cases.
In each case, there are two numbers in the first line n (the size of the set) and H. The second line has n numbers {a[1],a[2],……,a[n]}. 0
//4ms 1084k#include<stdio.h>#include<string.h>long long a[50],sum[50];int flag,n,h;void dfs(long long cnt,int m)//cnt是当前累加的和,m是当前累加了几个数 { if(cnt==h) {flag=1;return;} if(flag||m==n+1) return; if(sum[n]-sum[m-1]+cnt<h||cnt>h) return; dfs(cnt+a[m],m+1); dfs(cnt,m+1); return;}int main(){ while(~scanf("%d%lld",&n,&h)) { memset(sum,0,sizeof(sum)); flag=0; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); sum[i]=sum[i-1]+a[i]; } dfs(0,1); if(flag) printf("Yes\n"); else printf("No\n"); } return 0;}
//4ms 1084k#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,m,a[45],sum[45];int dfs(int top,int s){ if(s==m) return 1; if(top>=n||s>m||sum[n]-sum[top]+s<m) return 0; if(dfs(top+1,s)) return 1; if(dfs(top+1,s+a[top])) return 1; return 0;}int main(){ while(~scanf("%d%d",&n,&m)) { sum[0]=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum[i+1]=sum[i]+a[i]; } if(sum[n]<m) printf("No\n"); else if(dfs(0,0)) printf("Yes\n"); else printf("No\n"); } return 0;}
//988ms 1084k#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,m,a[45],sum[45],book[45];int dfs(int top,int s){ if(top>=n||s>m) return 0; if(s==m) return 1; for(int i=top;i<n;i++) { if(book[i]||sum[n]-sum[i]+s<m) continue; book[i]=1; if(dfs(top+1,s+a[i])) return 1; book[i]=0; } return 0;}int main(){ while(~scanf("%d%d",&n,&m)) { memset(book,0,sizeof(book)); sum[0]=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum[i+1]=sum[i]+a[i]; } if(sum[n]<m) printf("No\n"); else if(dfs(0,0)) printf("Yes\n"); else printf("No\n"); } return 0;}
阅读全文
0 0
- ACdream1726A Math game
- 10400 - Game Show Math
- uva10400 - Game Show Math
- UVaOJ10400 - Game Show Math
- 10400 game show math
- 10400 - Game Show Math
- 10400 -Game Show Math
- UVA10400- Game Show Math
- A Math game
- ACdream1726-A Math game
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- uva 10400 - Game Show Math
- 10400 - Game Show Math------水题
- UVa 10400 - Game Show Math
- UVA 10400 - Game Show Math
- uva 10400 Game Show Math
- uva 10400 Game Show Math
- 每日产品辣评:就是要睡你,小米杂货铺再填新丁
- DARPA推出模块化计算框架,芯片想用几核用几核
- terminal input EOF
- android进程间通信ipc Socket (二)
- 51nod 1384 全排列(next_permutation函数)
- ACdream1726A Math game
- 大端字节序、小端字节序
- 学生冲动消费隐藏的潜在需求——早点儿理财专家分析
- 中兴发布国民指纹手机 腾讯电脑管家新鲜开约
- 不起眼的foream便利贴相机 为何领跑众筹数据?
- 传金立超级续航M5 Plus将搭载 6.0英寸全高清AMOLED奥魔丽大屏
- 括号序列的判断
- 每日产品辣评:史上最快换代机型,华为畅享5发布两个月即迎来畅享5s
- Tornado 学习小结