• 交互题食用方式


    下文翻译自CF大大提高英语能力

    原文链接

    有时你会在编程比赛里遇到交互题(包括CF)

    在这种问题中,输入的数据可能不是预先定好的,但是是为你的解答量身定做的。评测人写了一个特殊的程序——interactor,这样它的输出对应着你的程序的输入,而你的输出又对映着它的输入。换句话说,你的程序和interactor交换数据,而我的输出基于程序间的“历史交流记录”。
    需要注意的是,如果你输出了一些数据,这些数据可能被放置于内部缓存区里,而且或许没有被直接传输给interactor。为了避免这种情况的发生,你需要每次输出时用一种特殊的清除缓存操作。这里有各种语言的清楚缓存操作:
    fflush(stdout) C++(scanf printf)
    cout<<flush C++(cout)
    System.out.flush() JAVA
    stdout.flush() Python
    flush(output) Pascal

    这里是一些交互题的经典问题:

    交互题的输入输出远远小于其他题——尽量用scanf/printf代替cin/cout。
    一般人为检验交互题的解答会远远难于常题,因为参与者要在检验过程中扮演interactor的角色。
    你可以尝试去做下面这道题题目链接

    Problem
    Guess the number

    Statement
    In this problem there is some hidden number and you have to interactively guess it. The hidden number is always an integer from 1 and to 1 000 000.

    You can make queries to the testing system. Each query is one integer from 1 to 1 000 000. Flush output stream after printing each query. There are two different responses testing program can provide:

    string < (without quotes), if the hidden number is less than the integer in your query;
    string >= (without quotes), if the hidden number is greater than or equal to the integer in your query.
    When your program wants to guess the hidden number, print string ! x, where x is the answer, and terminate your program immediately after flushing the output stream.

    Your program is allowed to make no more than 25 queries (not including printing the answer) to the testing system.

    Input
    Use standard input to read the responses to the queries.

    The input will contain responses to your queries — strings < and >=. The i-th string is a response to the i-th your query. When your program will guess the number print ! x, where x is the answer and terminate your program.

    The testing system will allow you to read the response on the query only after your program print the query for the system and perform flush operation.

    Output
    To make the queries your program must use standard output.

    Your program must print the queries — integer numbers xi (1 ≤ xi ≤ 106), one query per line. After printing each line your program must perform operation flush.

    Each of the values xi mean the query to the testing system. The response to the query will be given in the input file after you flush output. In case your program guessed the number x, print string ! x, where x — is the answer, and terminate your program.

    正解:很显然这道题可以用二分查找来做

    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    int main() {
        int l = 1, r = 1000000;
        while (l != r) {
            int mid = (l + r + 1) / 2;
            printf("%d
    ", mid);
            fflush(stdout);
    
            char response[3];
            scanf("%s", response);
            if (strcmp(response, "<") == 0)
                r = mid - 1;
            else
                l = mid;
        }
    
        printf("! %d
    ", l);
        fflush(stdout);
    }
    
  • 相关阅读:
    SIEM思考
    PowerDesigner15在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
    Mongo驱动
    RsysLog
    vi全局替换方法
    RPM
    hdu1195 Open the Lock (DFS)
    调制:调幅(AM)与调频(FM)
    调制:调幅(AM)与调频(FM)
    追本溯源 —— 诗词、名言
  • 原文地址:https://www.cnblogs.com/hjmmm/p/10485899.html
Copyright © 2020-2023  润新知