交互题[CF1103B Game with modulo]

fflush(stdin)

  作用:清理标准输入流,把多余的未被保存的数据丢掉

fflush(stdout)

  作用:清空输出缓冲区,并把缓冲区内容输出

CF1103B Game with modulo

题意:有一个需要你猜的数a,你每次可以猜两个数,如果x%a>=y%a,则返回x,否则返回y。你最多可以猜60次。

这很明显是个二分,所以这大概是道交互题练手题

#include<bits/stdc++.h>
using namespace std;
char s[20], s1[20];
int main() {
	while ((~scanf("%s", s)) && s[0] == 's') {
		int x = 0, y = 1;
		while (true) {
			printf("? %d %d\n", x, y);
			fflush(stdout);
			scanf("%s", s1);
			if (s1[0] == 'x')
				break;
			x = y, y = y * 2 + 1;
		}
		int mid, l = x, r = y;
		while (l + 1 < r) {
			mid = l + r >> 1;
			printf("? %d %d\n", l, mid);
			fflush(stdout);
			scanf("%s", s1);
			if (s1[0] == 'x')
				r = mid;
			else 
				l = mid;
		}
		if (l == 0) { 
			printf ("! 1\n");
			fflush(stdout);
			continue;
		}
		printf("? %d %d\n", r, r + 1);
		fflush(stdout);
		scanf("%s", s1);
		if (s1[0] == 'x')
			r --;
		printf("! %d\n",r);
		fflush(stdout);
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/wjnclln/p/10571604.html