hdoj-5616-Jam's balance
来源:互联网 发布:发房源赚钱软件 编辑:程序博客网 时间:2024/05/16 14:11
Problem Description
Jim has a balance and N weights. (1≤N≤20)
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
Input
The first line is a integer T(1≤T≤5), means T test cases.
For each test case :
The first line is N, means the number of weights.
The second line are N number, i’th number wi(1≤wi≤100) means the i’th weight’s weight is wi.
The third line is a number M. M is the weight of the object being measured.
Output
You should output the “YES”or”NO”.
Sample Input
1
2
1 4
3
2
4
5
Sample Output
NO
YES
YES
Hint
For the Case 1:Put the 4 weight alone
For the Case 2:Put the 4 weight and 1 weight on both side
题意:给一个只能测量两边重量是否相等的天平,有n个已知重量的砝码,问能否测量出给出的物体的重量。
题解:控制两个标记数组,然后一个个暴力枚举就好了,反正数据范围小
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>using namespace std;int main(){ int t; int a[105]; bool vis[4005],vis1[4005]; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(vis,0,sizeof(vis)); vis[0]=1; for(int i=1;i<=n;i++) { memset(vis1,0,sizeof(vis1)); for(int j=0;j<=2000;j++) { if(vis[j]==1) { if(vis1[j-a[i]]>=0) vis1[j-a[i]]=1; if(vis1[j+a[i]]<=2000) vis1[j+a[i]]=1; } } for(int i=0;i<=2000;i++) { if(vis[i]||vis1[i]) vis[i]=1; } } int m; scanf("%d",&m); while(m--) { int x; scanf("%d",&x); if(vis[x]) printf("YES\n"); else printf("NO\n"); } } return 0;}
- hdoj-5616-Jam's balance
- HDOJ 5616 Jam's balance(母函数)
- hdoj 5616 Jam's balance 【母函数】
- HDOJ 5616-Jam's balance【模拟】
- HDOJ 5616 Jam's balance (暴力)
- hdoj Jam's balance 5616 (母函数&暴力)
- HDU 5616 Jam's balance
- hdu 5616 Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance
- HDU 5616Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance(dp)
- hdu 5616 Jam's balance ( 动态规划 )
- hdu 5616 Jam's balance(简单dp)
- HDU 5616 Jam's balance(dp)
- HDU 5616 Jam's balance(01背包)
- HDU 5616 Jam's balance dp, bitset
- web学习记录
- Android ToolBar使用
- strcpy函数
- HTTP协议详解
- 解决java 向jdbc中存储数据中文乱码问题
- hdoj-5616-Jam's balance
- Unity3D动作类游戏中两种相机抖动
- python自学笔记之7编写函数、print、版本差距
- Item09 绝不在构造和析构过程调用virtual函数
- 格式 内存四区 操作字符串
- Picasso的简单介绍和使用
- CodeForces 41A+43A【课上无聊刷水题系列】
- 360和头条笔试
- 关于Cookie的使用案例和注意事项