標籤:
人機互動,顧名思義,就是人與電腦間的交流互動,我們對電腦的每一次操作程式中的每一條語句都是與電腦的交流,這類題目是將人和電腦調換位置,例如猜數遊戲,常規的做法是讓人輸入數,電腦判斷大還是小,而這類人機互動題目中需要你的程式充當人來輸入資料,再自己來判斷大小。
例題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 else22 {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。
C. Bear and Prime 100time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
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).
Input
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.
Output
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.
ExamplesInput
yes
no
yes
Output
2
80
5
composite
Input
no
yes
no
no
no
Output
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;}
類比人機互動類題目