CodeForces 679A Bear and Prime 100【交互题+数论】
来源:互联网 发布:json后台传值到前台 编辑:程序博客网 时间:2024/05/31 11:04
第一次做交互题:所谓交互题,就是我们给一个结果,系统返回一个结果。
题意:系统会隐藏一个数字x∈[2,100],你至多可以询问20次,系统会反应当前你给的值是不是x的因数,需要你最后判断x是质数还是合数
思路:对于一个合数,必定能被2个以上的质数整除。那么我们只要枚举2~100内的质数,然后看一看是不是有2个以上的质数是x的因数。
但题目里说只能询问20次。 因为x∈[2,100] 对于大于50的质数我们就不需要去判断是不是其因子,!因为x∈[2,100]!。如果是其因子,那肯定是质数。
因此,我们只要判断2~50内的质数是不是其因子即可,并且数量不少于2就是合数。为了有统一性,对于x=4,9,25,49这些数,按照上面的方法去算数量只有1,那么判断成质数就错了,所以要x=4,9,25,49加到a数组中,正好是19个。
数据分析:2<=x<=100。
复杂度分析:O(19)
#include <bits/stdc++.h>using namespace std;typedef long long ll;int isprime[200];int a[500]= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,4,25,49,9};/*void init(){ for(int i=1;i<=150;i++) isprime[i]=1; isprime[1]=isprime[0]=0; for(int i=2;i<=100;i++) { if(isprime[i]) { for(int j=i+i;j<=100;j+=i) isprime[j]=0; } } for(int i=1;i<=50;i++) { if(isprime[i]) printf("%d,",i); }}*/int main(void){ //init(); int cnt=0; for(int i=0;i<=18;i++) { char str[5]; printf("%d\n",a[i]); fflush(stdout); scanf("%s",str); if(str[0]=='y') cnt++; } if(cnt>=2) { printf("composite\n"); fflush(stdout); } else { printf("prime\n"); fflush(stdout); } return 0;}
解释一下fflush(stdout)。 fflush 就强制输出磁盘里保存的东西。然后如果你不强制输出,信息就一直保存在磁盘里不输出,达不到512B,就一直在等待新的数据进来,如果没数据进来【我猜应该有个计时器】到一定时间就会输出。 然后交互题用fflush的原因应该是想一一对应。纯属猜想,如果有不对的 请指正 ~
阅读全文
0 0
- CodeForces 679A Bear and Prime 100【交互题+数论】
- [Codeforces 679A] Bear And Prime 100 交互题
- CodeForces 679A Bear and Prime 100
- 交互题 codeforces C. Bear and Prime 100
- CF679A:Bear and Prime 100(交互 & 数论)
- codeforces 680C Bear and Prime 100(数论)
- A. Bear and Prime 100
- Codeforces 385C Bear and Prime Numbers(数论)
- Codeforces 385C Bear and Prime Numbers(数论)
- codeforces 680C Bear and Prime 100
- codeforces 680C Bear and Prime 100
- Bear and Prime 100
- Codeforces Round #356 (Div. 2)-C. Bear and Prime 100
- Codeforces Round #356 C. Bear and Prime 100
- Codeforces Round #356 (Div. 2)-C. Bear and Prime 100
- Codeforces 385 C. Bear and Prime Numbers
- CodeForces 385C Bear and Prime Numbers
- Codeforces 385C Bear and Prime Numbers
- BS与CS的恩恩怨怨
- es6 笔记
- 关于在web页面乱码
- hibernate的list映射和map映射
- ROS程序发布随机速到turltlesim
- CodeForces 679A Bear and Prime 100【交互题+数论】
- 每天积累一点(二)--mysql事物级别
- POJ 3076 Sudoku 精确覆盖问题DLX
- mysql (三)存储引擎
- xhconn.js中需要注意的问题
- 2017 HDU 6105 多校联合赛 Gameia
- Java自定义类加载器与双亲委派模型
- 疯狂数队列 编程 python
- Day 5- 12.Compiling and running