CF679A Bear and Prime 100(prime)
来源:互联网 发布:制版软件 编辑:程序博客网 时间:2024/05/25 05:36
写了一道很水的交互题。。有一个2,100之间的整数x,你要通过提20个以内的问题来判断它是素数还是合数。首先此题很水,解法就是:任何一个合数都含有至少两个不同的质数因子,或者含有一个质数的平方倍的因子。对于100以内的质数,属于后者的合数只有4,9,25,49。而分解一个100以内的合数,他最大的质因子超不过50.而50以内的质因子只有15个。所以我们可以最多提19个问题,每次询问这个数有没有x这个因子,如果有两个及以上,就一定是合数。
ps:fflush(stdout)的作用其实就是把你输出的东西输入到它的程序里,好给你回馈。
#include <cstdio>#include <cstring>#include <iostream>char s[10];int cnt=0;inline bool isprime(int x){ for(int i=2;i*i<=x;++i) if(x%i==0) return 0; return 1;}int main(){ int n=100; for(int i=2;i<=n/2&&cnt<2;++i){ if(isprime(i)){ printf("%d\n",i); fflush(stdout); scanf("%s",s+1); if(s[1]=='y') cnt++; if(i*i<=n){ printf("%d\n",i*i); fflush(stdout); scanf("%s",s+1); if(s[1]=='y') cnt++; } } } if(cnt>=2) puts("composite");else puts("prime"); return 0;}
阅读全文
0 0
- CF679A Bear and Prime 100(prime)
- CF679A:Bear and Prime 100(交互 & 数论)
- Bear and Prime 100
- A. Bear and Prime 100
- codeforces356 C.Bear and Prime 100 [模拟]
- codeforces 680C Bear and Prime 100
- CodeForces 679A Bear and Prime 100
- codeforces 680C Bear and Prime 100
- Codeforces Round #356 (Div. 2)-C. Bear and Prime 100
- Codeforces Round #356 C. Bear and Prime 100
- 交互题 codeforces C. Bear and Prime 100
- Codeforces Round #356 (Div. 2)-C. Bear and Prime 100
- [Codeforces 679A] Bear And Prime 100 交互题
- CodeForces 679A Bear and Prime 100【交互题+数论】
- codeforces 680C Bear and Prime 100(数论)
- Codeforces 385 C. Bear and Prime Numbers
- CodeForces 385C Bear and Prime Numbers
- Codeforces 385C Bear and Prime Numbers
- dom监听属性的变化
- js动态生成select,并初始化后台原有数据
- MATLAB和C语言混合编程问题
- UE4 自动创建破碎物件的actor(C++版本)
- Filter过滤器例外URL设置
- CF679A Bear and Prime 100(prime)
- Android map api密匙获取 输入命令后显示keytool 不是内部或外部命令
- angulsrjs2.0---demo---使静态文字每秒随机变颜色
- git rebase简介(高级篇)
- $.ajax()方法详解
- 【Light OJ 1104】Birthday Paradox
- JUC集合-09之 LinkedBlockingDeque
- Jmeter使用
- CS231n课程笔记翻译系列之目录汇总