Codeforces Round #356 (Div. 2) C. Bear and Prime 100 交互题

C. Bear and Prime 100

链接:

http://codeforces.com/contest/680/problem/C

题意:

上次做这道交互题完全看不懂,今天又扒出来,突然就知道了。。

现在在[2,100]里面有一个隐藏的数字,你现在可以问最多20个问题,每个问题可以提出一个数,如果这个数是隐藏的数字的因子的话

那么就会返回yes

否则就会返回no

让你判断这个数是合数,还是素数

题解:

把小于50的素数全部问了一遍,且把4 9 25 49这四个小于100的素数的平方问一遍就好了

如果超过1次回答为yes的话,那么就是合数。

道理很显然,因为一个合数肯定是一个素数乘以另外一个素数,所以至少有2嘛,第二个数就是小于等于50的了

代码:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     int a[20] = {
 7         2 ,3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
 8         4, 9, 25, 49 };
 9     int fg = 0;
10     for (int i = 0; i < 19; i++) {
11         cout << a[i] << endl;
12         string s;
13         cin >> s;
14         if (s == "yes")
15             if (fg == 1) {
16                 cout << "composite" << endl;
17                 return 0;
18             }
19             else
20                 fg = 1;
21     }
22     cout << "prime" << endl;
23     return 0;
24 }
原文地址:https://www.cnblogs.com/baocong/p/5914222.html