交互题[CF1103B Game with modulo、CF1019B The hat、CF896B Ithea Plays With Chtholly]

时间:2023-03-09 07:05:57
交互题[CF1103B Game with modulo、CF1019B The hat、CF896B Ithea Plays With Chtholly]

交互题就是程序与电脑代码的交互。

比如没有主函数的程序,而spj则给你一段主函,就变成了一个整体函数。

还有一种就是程序和spj之间有互动,这个用到fflush(stdout);这个函数就可以实现交互了

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;
}

CF1019B The hat

又是b题(我就只能做做b题,我太菜了QWQ)

#include <bits/stdc++.h>
using namespace std;
int n, t, r, mid, l;
inline int find (int x) {
printf("? %d\n", x + 1);
fflush(stdout);
scanf("%d", &x);
return x;
}
int main() {
scanf("%d", &n);
t = n / 2;
if (t & 1) {
printf("! -1\n");
return 0;
}
r = n - 1;
while (l < r) {
mid = l + r >> 1;
if (find(mid) - find((mid + t) % n) >= 0)
r = mid;
else
l = mid + 1;
}
printf("! %d", l + 1);
fflush(stdout);
return 0;
}

  

CF896B Ithea Plays With Chtholly

还是B题2333

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