您的位置:首页 > 其它

Interactive Problems(交互式问题) 2016.6.9

2016-06-09 00:20 260 查看
参考:http://codeforces.com/blog/entry/45307

在编程竞赛中(包括CodeForces)有时你会看到交互式问题

在这种类型的问题中,对于你编程的输入数据,或许不被预先决定,而是由你的解法专门构造

Jury 写了一个特殊的程序 -- 交互器,它的输出被转变为你的解法的输入,并且你的程序的输出被发送到交互器的输入

换句话说,你的解法和交互器交换数据,并且我决定输出什么基于 “交流的历史”

当你写一个交互式问题的解法,记住 “如果你要输出一些数据,也许这些数据首先被放到内部缓冲区,不被直接转移到交互器”  是重要的

为了防止这种情况,每次你输出一些数据,必须用特殊的 flush 操作

交互式问题有一些特征

交互式问题中的输入/输出运行的要比平常问题慢,所以建议使用 scanf / printf
通常,人工测试交互式问题的解法比较困难,因为测试的时候参与者需要担任交互器的角色
“自定义调用” 标签不了解问题的交互器,所以你不能充分的测试你的解法
CodeForces的比赛中有时候会出现交互式的问题,Hack 数据的格式将在问题的声明中给出
(感觉这波翻译用处不是很大。。。而且菜的抠脚

例题:

1、CodeForces_101021A Guess the Number

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const int mod = 1e9 + 7;
const int INF = 0x7fffffff;

int main()
{
int Left = 1, Right = 1000000;
while (Left != Right) {
int Mid = (Left + Right + 1) / 2;
printf("%d\n", Mid);    //先猜一个数
fflush(stdout);
char Responses[5];
scanf("%s", Responses);     //系统对于猜的数,给出一个Responses
if (strcmp("<", Responses) == 0) {      //若真实值小于猜的数,系统返回 "<" ,我们就在比猜的数小的区间继续猜
Right = Mid - 1;
} else {                                //否则我们就在大于等于猜的数的区间猜
Left = Mid;
}
}
printf("! %d\n", Left);     //最后我们就猜出了这个数
fflush(stdout);
return 0;
}

2、CodeForces_680C Bear and Prime 100

题意:

不超过 20 次的询问测素数

解题思路:

先判断 能否整除 2、3、5、7

若都不能整除,为素数

能被两个及以上整除,为合数

只能被其中一个整除,继续判断

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
int Prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
int End[] = {15, 11, 8, 6};
bool vis[10];
char Responses[10];

int main()
{
int Count = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < 4; ++i) {
printf("%d\n", Prime[i]);
fflush(stdout);
scanf("%s", Responses);
if (strcmp(Responses, "yes") == 0) {
vis[i] = true;
++Count;
}
}
bool IsPrime = true;
if (Count > 1) {
IsPrime = false;
} else if (Count == 1) {
for (int i = 0; i < 4; ++i) {
if (vis[i]) {
for (int j = 0; j < End[i]; ++j) {
printf("%d\n", Prime[j] * Prime[i]);
fflush(stdout);
scanf("%s", Responses);
if (strcmp(Responses, "yes") == 0) {
IsPrime = false;
break;
}
}
break;
}
}
}
if (IsPrime) {
printf("prime\n");
} else {
printf("composite\n");
}
fflush(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: