codeforces679A_Bear and Prime 100 交互题

传送门

第一道交互题

题意

  电脑事先想好了一个数【2,100】

  你会每次问电脑一个数是否是它想的那个数的因数

  电脑会告诉你yes或no    至多询问20次  最后要输出它想的数是质数还是合数

思路

  枚举<50的质数和4,9,25,49即可判断

  4,9, 25,49单独看作质数是这样方便判断2^2,3^2,...,9^2

解释

  在使用多个输出函数连续进行多次输出时,有可能发现输出错误。

  因为下一个数据再上一个数据还没输出完毕,还在输出缓冲区中时,下一个printf就把另一个数据加入输出缓冲区,

  结果冲掉了原来的数据,出现输出错误。

  在 printf();后加上fflush(stdout);强制马上输出,避免错误。

fflush只能加于你的输出后 电脑的反馈前

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 char fb[5];
 5 int init[40] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 4, 9, 25, 49};
 6
 7 int main() {
 8     int cnt = 0; bool flag = 1;
 9     for (int i = 0; i <= 18; ++i){
10         printf("%d\n", init[i]);
11         fflush(stdout);
12         scanf("%s", fb);
13         if (fb[0] == ‘y‘) ++cnt;
14         if (cnt >= 2) {
15             flag = 0; break;
16         }
17     }
18     flag ? puts("prime") : puts("composite");
19     return 0;
20 }

原文地址:https://www.cnblogs.com/Fo0o0ol/p/10090005.html

时间: 2024-12-07 11:36:55

codeforces679A_Bear and Prime 100 交互题的相关文章

codeforces 679A Bear and Prime 100 交互

第一次交互题,记录一下吧 #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring> using namespace std; typed

Codeforces A - Bear and Prime 100(交互题)

A - Bear and Prime 100 思路:任何一个合数都可以写成2个以上质数的乘积.在2-100中,除了4,9,25,49外都可以写成两个以上不同质数的乘积. 所以打一个质数加这四个数的表:{2,3,4,5,7,9,11,13,17,19,23,25,29,31,37,41,43,47,49},询问19次,如果能被整出两次以上,说明是合数,否则是质数. #include<bits/stdc++.h> using namespace std; #define ll long long

2019雅礼集训 D10T2 硬币翻转 [交互题]

题目描述: coin.h: #include<string> void guess(); int ask(std::string coin); grader.cpp: #include "coin.h" #include <iostream> #include <assert.h> using namespace std; namespace U { using u64 = unsigned long long; using i64 = long l

E. XOR Guessing 交互题 Educational Codeforces Round 71 (Rated for Div. 2)

E. XOR Guessing 交互题. 因为这个数最多只有14位 0~13,所以我们可以先处理后面7位,然后再处理后面7位. 因为异或的性质,如果一个数和0异或,那么就等于本身. 所以我们第一次异或1~100 所以 后面从7到13位就都是0,所以结果的后面的7位就可以算出来. 然后同理可以把前面七位找到. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm>

CF 714D Searching Rectangles 交互题 二分

题意:交互题,已知一个n*n的地图,电脑藏了两个不相交的矩形(矩形的边和坐标轴平行).每次向电脑询问左下[x1,y1]右上[x2,y2]的矩形内完全包含多少个藏着的矩形.n<=2^16.请在200次内猜出两个矩形左下,右上坐标. 先二分出两个不相交矩形的分界线,要么横着要么竖着.接在在每一快中二分出矩形的四条边即可(最左,右,上,下边界) #include <bits/stdc++.h> using namespace std; int n,ans[20],cnt=0; inline i

Moscow Subregional 2010 Problem K. KMC Attacks 交互题、队列优化、枚举

ACM ICPC 2010-2011 NEERC Moscow Subregional Contest Moscow, October 24, 2010 Problem K. KMC Attacks Time limit: 2 seconds Memory limit: 256 megabytes Warrant VI is a remote planet located in the Koprulu Sector. Warrant VI features a strange huge fiel

Gym - 101375H MaratonIME gets candies 交互题

交互题介绍:https://loj.ac/problem/6 题意:输出Q X ,读入><= 来猜数,小于50步猜出就算过样例 题解:根本不需要每次输出要打cout.flush()... ac: #include<iostream> #include<string> using namespace std; int main() { int l = 1, r = 1e9; while (l <= r) { int mid = (l + r) / 2; cout &

Codeforces Round #504 E - Down or Right 交互题

1023E 题意: 交互题.在一个有障碍地图中,问如何走才能从(1,1)走到(n,n),只能向右或者向左走.每次询问两个点,回复你这两个点能不能走通. 思路: 只用最多2*n-2次询问.从(1,1),能向右走就向右走,不能就向下走,直到走到斜对角线上.从(n,n)出发,能向上走就向上走,不能就向左走,直到走到斜对角线上. 因为保证有路,所以最后输出(1,1)出发的正向路径,加上从(n,n)出发的反向路径. #include <algorithm> #include <iterator&g

Codeforces 1137D - Cooperative Game - [交互题+思维题]

题目链接:https://codeforces.com/contest/1137/problem/D 题意: 交互题. 给定如下一个有向图: 现在十个人各有一枚棋子(编号 $0 \sim 9$),在不知道 $t,c$ 的值的情况下,他们同时从home出发,要最终到达flag处. 你只能选择移动哪几个人的棋子,但棋子移动到哪里由程序确定并给出. 题解: 看网上大佬一个神仙解法……看得我一愣一愣的…… 选定两颗棋子,第一颗每次都移动,第二颗隔一次移动一次.所以,进行了 $2t$ 次之后第二颗棋子刚好