cf 1011 D. Rocket

此题为一道交互题,交互题就是你自己输出的数据是计算机程序的读入文件,而计算机的输出文件需要你读入。每次输出之后要加一个fflush(stdout);,来清空输出缓冲区

#include<bits/stdc++.h>
using namespace std;
int p[110];
int main()
{
    int m,n,x;
    scanf("%d%d",&m,&n);
    for(int i=0;i<n;i++)
    {
        printf("1\n");
        fflush(stdout);
        scanf("%d",&x);
        if(x==0) return 0;
        else p[i]=x;
    }
    int l=1,r=m;
    int t=0;
    while(true)
    {
        int mid=(l+r)/2;
        printf("%d\n",mid);
        fflush(stdout);
        scanf("%d",&x);
        if(p[t%n]==-1)
        {
            x=-x;
        }
        if(x==0)
          break;
        else if(x==1)
        {
            l=mid+1;
        }
        else if(x==-1)
            r=mid-1;
        t++;
    }
}

猜你喜欢

转载自www.cnblogs.com/lishengkangshidatiancai/p/10291135.html