当前位置: 首页 > 工具软件 > Crow > 使用案例 >

Memory and Crow(CodeForces 712A)

闻人吕恭
2023-12-01

题目描述:
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?

Input
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.

Output
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.

Examples
5
6 -4 8 -2 3
2 4 6 1 3

5
3 -2 -1 5 6
1 -3 4 11 6

题目描述:
有两个数组,一个数组为a,一个为b ,在给你一个数n,和数组啊,我们要求出数组b的每个元素,并且我们知道ai = bi - bi + 1 + bi + 2 - bi + 3…
到底是什么意思呢
就是a1 = b1-b2-b3+b4…
a2 = b2-b3+b4-b5…
依次类推
然后让我们求出满足a数组的b数组的元素。

解题思路:
乍一看感觉还挺难的,但其实只要出发点找对就so easy,我们发现an和bn是相同的,并且我们只要从bn倒推回来是不是就可以顺利找到答案了,在倒退过程中不难发现bn = an + an+1;
直接上代码

#include <iostream>
#include <cstdio>

using namespace std;
const int maxn = 1e5+10;

int A[maxn],B[maxn];//分别存储a,b数组

int main () {
	int n;
	cin >> n;
	for(int i = 1;i <= n;i++) 
		scanf("%d",&A[i]);
	B[n] = A[n];
	for(int i = n-1;i > 0;i--)//倒推找出数组b的每个元素
		B[i] = A[i]+A[i+1];
	for (int i = 1;i <= n;i++) {
		if(i == 1)
			cout << B[i];
		else 
			cout << " " << B[i];
	}
	cout << "\n";
	return 0; 
}
 类似资料:

相关阅读

相关文章

相关问答