Codeforces 1425F Flamingoes of Mystery

题目链接

1425F

题解

题意

这是一道神奇的交互题。
大小为n的数组,你有n次询问的机会,oj会回答你的每次询问,最后需要输出该数组。

思路

因为有n次机会,需要得到n个数字,那么我们必然在每一次询问中得到至少一个元素。
N的取值范围得知最少有3个,我们可以先得到这三个,然后通过之后每一个元素的前一个得到该元素。

AC代码

#include <bits/stdc++.h>
using namespace std;
int const N = 1e3 + 10;
int ans[N];

int main() {
    
    
    int n;cin >> n;
    int s12, s23, s13;
    printf("? 1 2\n");
    fflush(stdout);
    scanf("%d", &s12);
    printf("? 1 3\n");
    fflush(stdout);
    scanf("%d", &s13);
    printf("? 2 3\n");
    fflush(stdout);
    scanf("%d", &s23);
    ans[3] = s13 - s12;
    ans[2] = s23 - ans[3];
    ans[1] = s12 - ans[2];
    for (int i = 4; i <= n; i++) {
    
    
        printf("? %d %d\n", i-1, i);
        fflush(stdout);
        int t;
        scanf("%d", &t);
        ans[i] = t - ans[i-1];
    }
    printf("! ");
    for (int i = 1; i <= n ;i++) printf(" %d", ans[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45934120/article/details/108896478