Human-computer interaction, as the name implies, is the interaction between people and computers, we each of the computer operation of each statement is the communication with the computer, this kind of topic is the person and the computer exchange position, such as guessing the number of games, the conventional practice is to let people enter the number, the computer judge big or small, And this kind of human-computer interaction problem needs your program to act as a person to input data, and then to judge the size.
Example 1: Guessing games http://codeforces.com/gym/101021
Analysis: Range [1,1e6], two-point judgment. Program output number, we enter the size. (This code is a random number).
1#include <bits/stdc++.h>2 using namespacestd;3 intMain ()4 {5 intN;6Srand ((int) Time (0));7 stringstr;8 intmax=1e6,min=1;9 intt= -, A;TenA= (rand ()% (max-min)) +min; Onecout<<a<<Endl; A fflush (stdout); - while(t--&& (max-min)! =1) - { theCin>>str; - if(str==">=") - { -min=A; +A= (rand ()% (max-min)) +min; - } + Else A { atmax=A; -A= (rand ()% (max-min+1))+min; - } - if(t==0|| (max-min) = =1) -cout<<"! "; -cout<<a<<Endl; in fflush (stdout); - } to //cout<< '! ' << (rand ()% (max-min+1)) +min<<endl; + //fflush (stdout); - return 0; the}
Example 2: Fang (Fang) lattice (elder brother) take number http://www.51isoft.com/v3/problem_show.php?pid=34980 Beijing Normal University OJ (deserted, only I one person make my even WA very obvious).
Analysis: A matrix in which the value of each coordinate is incremented from the top to the right, requiring that the known quantity value is not within the matrix. program output coordinate query, we enter the corresponding value of this coordinate.
Idea: Now query from the rightmost maximum value of each row, find a number larger than the known quantity value, then the point is in this line, and then walk from the friend to the left.
#include <iostream>#include<cstdio>using namespacestd;intMain () {intT; CIN>>T; while(t--) { intn,m,k,ans,i,j,flag=0; CIN>>n>>m>>K; intsum=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++; Elsej--; if(j<1|| I>n) Break;//If the ans<k is located in the bank. And then descending from M to find the position } if(!flag) cout<<"NO"<<Endl; Elsecout<<"YES"<<Endl; } return 0;}
Example 3:cf680c.
C. Bear and Prime 100time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
This was an interactive problem. In the output sections below you'll see the information about flushing the output.
Bear Limak thinks of some hidden number-an integers 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, distinct divisors, 1 and x. If integer x > 1 is not prime, it's called Composite.
You can ask up to queries about divisors of the hidden number. In each query you should print a integer from interval [2, 100]. The system would answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer would be "no".
For example, if the hidden number is and then the system would answer "yes" only if you print 2, 7 or 14.
When you were done asking queries, the print "prime" or "composite" and terminate your program.
You'll get the wrong Answer verdict if you ask more than queries, or if you print a integer not from the RAN GE [2, 100]. Also, you'll get the wrong Answer verdict if the printed Answer isn ' t correct.
You'll get the idleness Limit exceeded verdict If you don ' t print anything (and you should) or if you forget about F Lushing the output (more info below).
Input
After all query should read one string from the input. It'll be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise.
Output
Up to the times you can ask a query-print an integer from interval [2, +] in one line. You have to both print the end-of-line character and flush the output. After flushing your should read a response from the input.
In any moment you can print the answer "prime" or "composite" (without the quotes). After this, flush the output and terminate your program.
To flush your 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 is 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
Analysis: Similar to the problem of guessing, we think of a number, let the program to guess whether it is prime or number. Program output A number of whether we can be divisible.
Idea: Each sum can be regarded as the product of two primes, the topic range is 2~100 so we only need to consider 2~50 prime number (2*51 more than 100). Use the prime number of 2~50 to test. When it can be divisible by two primes, it can be confirmed as sum. Pay special attention to 4,9,25,49.
#include <bits/stdc++.h>using namespacestd;intMain () {intprime[]={2,3,5,7, One, -, -, +, at, in, to,Panax Notoginseng, A, +, -}; intI=0, flag=0, t= -; stringstr; 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))//if it is a 2,3,5,7, it must be judged again. {cout<<prime[i]*prime[i]<<Endl; Fflush (stdout); CIN>>str; if(str=="Yes") flag++; } if(flag==2|| i== -) Break; I++; } if(flag==2) cout<<"Composite"<<Endl; Elsecout<<"Prime"<<Endl; return 0;}
Simulation of human-Computer Interaction class topics