Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <stdio.h>
- int cnt;
- int v[1000005];
- int get_arr()
- {
- FILE *pipe;
- // 打开另外一个程序并建立管道通信
- pipe = popen("./gen2.py", "r");
- if (pipe == NULL) {
- fprintf(stderr, "无法打开程序\n");
- return -1;
- }
- // 读取程序的输出
- fscanf(pipe, "%d", &cnt);
- fprintf(stderr, "read cnt:%d from pipe\n", cnt);
- int idx = 0;
- while (fscanf(pipe, "%d", v + idx) != EOF) {
- // printf("从程序2接收到的数据: %d\n", data);
- idx++;
- }
- assert(idx == cnt);
- // 关闭管道通信
- pclose(pipe);
- return 0;
- }
- int main(int argc, char **argv)
- {
- assert(get_arr() == 0);
- fprintf(stderr, "get %d elements from random\n", cnt);
- int idx = 0, opt = 0;
- printf("%d\n", v[idx]), fflush(stdout);
- while (opt < 1000) {
- char op;
- int num;
- scanf("%c%d", &op, &num);
- if (op == '!') {
- if (num == cnt) {
- fprintf(stderr, "bingo! %d\n", num);
- return 0;
- } else {
- fprintf(stderr, "expect: %d, get: %d\n", cnt, num);
- return -1;
- }
- } else {
- if (op == '+') {
- idx = (idx + num) % cnt;
- printf("%d\n", v[idx]), fflush(stdout);
- } else if (op == '-') {
- idx = ((idx - num) % cnt + cnt) % cnt;
- printf("%d\n", v[idx]), fflush(stdout);
- } else {
- fprintf(stderr, "Wrong op:%c!\n", op);
- }
- }
- }
- fprintf(stderr, "too many try\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment