There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:
- The crow sets ai initially 0.
- The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus, ai = bi - bi + 1 + bi + 2 - bi + 3....
Memory gives you the values a1, a2, ..., an, and he now wants you to find the initial numbers b1, b2, ..., bn written in the row? Can you do it?
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row.
The next line contains n, the i'th of which is ai ( - 109 ≤ ai ≤ 109) — the value of the i'th number.
Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.
5 6 -4 8 -2 3
2 4 6 1 3
5 3 -2 -1 5 6
1 -3 4 11 6
In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and - 4 = 4 - 6 + 1 - 3.
In the second sample test, the sequence 1, - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6.
题目大意:
ai=b1-b2+b3-b4+.......bn
给你所有的ai,让你求出bi;
思路:
根据题干中的样例来分析:
a1=b1-b2+b3-b4+b5;
a2=b2-b3+b4-b5;
那么a1+a2=b1;
同理a2+a3=b2;依次类推;
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int a[100005];
int ans[100005];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
ans[n]=a[n];
for(int i=1;i<=n-1;i++)
{
ans[i]=a[i]+a[i+1];
}
for(int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
printf("\n");
}
}
本文介绍了一种通过已知变化后的数列逆向求解原始数列的方法。具体地,对于给定的一系列数a1, a2, ..., an,其中每个数ai都是通过特定规则从原始序列b1, b2, ..., bn中计算得出。文章详细解释了如何根据这些变化规律逆向还原出初始数列。
1330

被折叠的 条评论
为什么被折叠?



