cf 727C Guess the Array

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yjf3151731373/article/details/52829145
Guess the Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem. You should use flush operation after each printed line. For example, in C++ you should usefflush(stdout), in Java you should use System.out.flush(), and in Pascal — flush(output).

In this problem you should guess an array a which is unknown for you. The only information you have initially is the length n of the array a.

The only allowed action is to ask the sum of two elements by their indices. Formally, you can print two indices i and j (the indices should be distinct). Then your program should read the response: the single integer equals to ai + aj.

It is easy to prove that it is always possible to guess the array using at most n requests.

Write a program that will guess the array a by making at most n requests.

Interaction

In each test your program should guess a single array.

The input starts with a line containing integer n (3 ≤ n ≤ 5000) — the length of the array. Your program should read it at first.

After that your program should print to the standard output the requests about the sum of two elements or inform that the array is guessed.

  • In case your program is making a request to ask the sum of two elements, it should print line in the format "? i j" (i and j are distinct integers between 1 and n), where i and j are indices in the array a.
  • In case your program informs that the array is guessed, it should print line in the format "a1 a2 ... an" (it is guaranteed that all aiare positive integers not exceeding 105), where ai is the i-th element of the array a.

The response on a request is a single integer equal to ai + aj, printed on a separate line.

Your program can do at most n requests. Note that the final line «a1 a2 ... an» is not counted as a request.

Do not forget about flush operation after each printed line.

After you program prints the guessed array, it should terminate normally.

Example
input
5
 
9
 
7
 
9
 
11
 
6
 
output
 
? 1 5
 
? 2 3
 
? 4 1
 
? 5 2
 
? 3 4
 
! 4 6 1 5 5
Note

The format of a test to make a hack is:

  • The first line contains an integer number n (3 ≤ n ≤ 5000) — the length of the array.

  • The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array to guess.


交互题;
给你一个数组长度,让你猜数组元素,经过n 次询问,每次询问两个数的和,算出每个数,询问的和也是自己输入, C++输出只能用cout,,,,不知道为什么


#include <bits/stdc++.h>
using namespace std;
const int MaxN = 10050;
int N, a[MaxN], A[MaxN];


int main()
{
    scanf("%d",&N);
    for(int i=2;i<=N;i++)
    {
        cout << "? 1 " << i << endl;
        fflush(stdout);
        scanf("%d",&a[i]);
    }
     cout << "? 2 3" << endl;
    scanf("%d",&a[1]);
    fflush(stdout);
    A[1]=(a[2]+a[3]-a[1])/2;
    for(int i=2;i<=N;i++)
        A[i]=a[i]-A[1];
    cout << "!";
    fflush(stdout);
    for(int i=1;i<=N;i++)
         cout << " " << A[i];
    fflush(stdout);
    return 0;
}


补充:

#include<stdlib.h>
#include<stdio.h>

int sum;//sum is a global variable

main()
{
 int i;//i is a local variable

 sum = 0;
 for(i = 1;i <= 5 ;i++) //iterate i from 1 to 5
 {
  printf("The value of i is %d/n",i);
  fflush(stdout);//flush the buffer
  sum += i;
 }
 printf("The sum is %d/n",sum);
 exit(0);//terminate the program

 

这个简单的c程序中用到了fflush(stdout),目的是清空缓冲,强制结果马上显示到屏幕上。

一般写c程序的时候,我都只用了printf(……),加不加fflush(stdout)好像没什么区别,都是立即就显示出结果了,但是用fork()的时候效果就不一样了。

#include<stdio.h>
#include<unistd.h>
int main()
{
        printf("hello");
        fflush(stdout);
        fork();
        return 0;
}

 

调用printf(……)时,输出结果一般会被标准库缓存起来,不一定能及时写到输出设备上,但是fflush(stdout)能强制把缓存内容输出。


猜你喜欢

转载自blog.csdn.net/yjf3151731373/article/details/52829145
今日推荐