ZCMU—1777
2016-12-19 10:05
405 查看
1777: 寻找倍数
Time Limit: 1 Sec Memory Limit: 128 MB[Submit][Status][Web
Board]
Description
给出n(n<=10000)个正整数,每个数xi<=15000.可以在这个n个数中选择一些数出来,至少选择一个,是否存在一种选择方案使得选择出来的数的和是n的整数倍
Input
第一行一个T(T<=500),第二行一个数n,接下来n个正整数Output
Case #x: y,其中x是测试编号,从1开始,y表示答案,如果存在y为Yes,否则为NoSample Input
25
1 2 3 4 1
2
1 2
Sample Output
Case #1: YesCase #2: Yes
【分析】
砝码称重...经典题,数据只有10000个数,所以可以用队列直接做,遍历一遍数组,每次对当前这个数,去加上前面已经出现过的所有数,得到新的数,然后判重,没出现过的就加进队列,然后做完n个数,中途加个判断,如果某一次得到的数已经是n的倍数了那么就直接跳出循环【代码】
#include <cstdio> #include <iostream> #include <vector> #include <cstring> using namespace std; vector<int>a; int main() { int n,x; int f[10100]; int pp;scanf("%d",&pp); for (int p=1;p<=pp;p++) { int tt=0; memset(f,0,sizeof(f)); scanf("%d",&n); scanf("%d",&x); a.push_back(x); for (int i=1;i<n;i++) { scanf("%d",&x); if (x%n==0) tt=1; if (!tt) { for (int j=0;j<a.size();j++) { int xx=(a[j]+x)%n; if (xx==0) {tt=1;break;} if (!f[xx]) { f[xx]=1; a.push_back(xx); } } } } printf("Case #%d: %s\n",p,tt==0?"No":"Yes"); } return 0; }
相关文章推荐
- Request —— 让 Node.js http请求变得超简单
- OpenCV入门四:用OpenCV读取一个图像,并使用Canny算子进行边缘提取
- 平滑滤波
- sqlldr 连接远程数据配置
- ofbiz 电商项目 apache顶级
- C# 中的委托和事件
- unity 实现pageView翻页效果
- 分享一下事件监听addEventListener----attachEvent的用法
- php echo、 print()、 print_r()、 var_dump()的区别
- Mysql 常用SQL语句集锦
- 《Java多线程编程核心技术》学习笔记(三)——Lock、Timer、单例模式
- mysql主从同步问题解决汇总
- java实现图片转化为字符图片--统计字符所占的面积
- okhttp3基础使用的封装
- 感谢CSDN的博客专家福利——最新一期的程序员杂志和技术书籍自选(附图)
- sql语句- 查询 本月 本周 等相关数据
- 一张“神图”看懂单机/集群/热备/磁盘阵列(RAID)
- MySql事务机制
- while(true)循环与CPU占用率问题
- php system和exec区别