人机交互,顾名思义,就是人与计算机间的交流互动,我们对计算机的每一次操作程序中的每一条语句都是与计算机的交流,这类题目是将人和计算机调换位置,例如猜数游戏,常规的做法是让人输入数,计算机判断大还是小,而这类人机交互题目中需要你的程序充当人来输入数据,再自己来判断大小。
例题1:猜数游戏http://codeforces.com/gym/101021
分析:范围[1,1e6],二分判断。程序输出数字,我们输入大小。(这个代码是随机数)。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int n; 6 srand((int)time(0)); 7 string str; 8 int max=1e6,min=1; 9 int T=25,a; 10 a=(rand()%(max-min))+min; 11 cout<<a<<endl; 12 fflush(stdout); 13 while(T--&&(max-min)!=1) 14 { 15 cin>>str; 16 if(str==">=") 17 { 18 min=a; 19 a=(rand()%(max-min))+min; 20 } 21 else 22 { 23 max=a; 24 a=(rand()%(max-min+1))+min; 25 } 26 if(T==0||(max-min)==1) 27 cout<<"! "; 28 cout<<a<<endl; 29 fflush(stdout); 30 } 31 // cout<<'!'<<(rand()%(max-min+1))+min<<endl; 32 // fflush(stdout); 33 return 0; 34 }
例题2:方(芳)格(哥)取数 http://www.51isoft.com/v3/problem_show.php?pid=34980 北师大OJ(冷清,只有我一个人让我的连WA很明显)。
分析:一个矩阵,每个坐标对应的数值大小从上往下从左往右递增,要求已知数值在不在矩阵内。程序输出坐标查询,我们输入该坐标的对应值。
思路:现从每一行的最右侧记最大值开始查询,找到比已知数值大的数,则该点位于此行中,再从友向左遍历便可。
#include<iostream> #include<cstdio> using namespace std; int main() { int T; cin>>T; while(T--) { int n,m,k,ans,i,j,flag=0; cin>>n>>m>>k; int sum=n+m; i=1;j=m; while(sum--) { cout<<i<<' '<<j<<endl; cin>>ans; if(ans==k) { flag=1; break; } else if(ans<k) i++; else j--; if(j<1||i>n) break;//若ans<k则位置在本行。再从m递减找到位置 } if(!flag) cout<<"NO"<<endl; else cout<<"YES"<<endl; } return 0; }
例题3:CF680C。
This is an interactive problem. In the output section below you will see the information about flushing the output.
Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.
Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.
You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".
For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.
When you are done asking queries, print "prime" or "composite" and terminate your program.
You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.
You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below).
After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise.
Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input.
In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program.
To flush you can use (just after printing an integer and end-of-line):
- fflush(stdout) in C++;
- System.out.flush() in Java;
- stdout.flush() in Python;
- flush(output) in Pascal;
- See the documentation for other languages.
Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input.
yes
no
yes
2
80
5
composite
no
yes
no
no
no
58
59
78
78
2
prime
分析:与猜数问题类似,我们心里想一个数,让程序来猜是素数还是和数。程序输出一个数我们出入是否能被整除。
思路:每个和数都可看成两个素数之积,题目范围是2~100所以我们只需要考虑2~50的素数(2*51就超过了100)。用2~50的素数来检验。当能被两个素数整除的时候就可确认为和数。特别注意4,9,25,49.
#include<bits/stdc++.h> using namespace std; int main() { int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; int i=0,flag=0,T=20; string str; while(T--) { cout<<prime[i]<<endl; fflush(stdout); cin>>str; if(str=="yes") flag++; if(flag==1&&(i==0||i==1||i==2||i==3))//如果是2,3,5,7则须再判断一次。 { cout<<prime[i]*prime[i]<<endl; fflush(stdout); cin>>str; if(str=="yes") flag++; } if(flag==2||i==14) break; i++; } if(flag==2) cout<<"composite"<<endl; else cout<<"prime"<<endl; return 0; }