冬令营第一天
B. Maximums
题目链接
题意:
输入一组数据,根据题干要求转化成输出
Alicia has an array, a1,a2,…,an, of non-negative integers. For each 1≤i≤n, she has found a non-negative integer xi=max(0,a1,…,ai−1). Note that for i=1, xi=0.
For example, if Alicia had the array a={0,1,2,0,3}, then x={0,0,1,2,2}.
Then, she calculated an array, b1,b2,…,bn: bi=ai−xi.
For example, if Alicia had the array a={0,1,2,0,3}, b={0−0,1−0,2−1,0−2,3−2}={0,1,1,−2,1}.
Alicia gives you the values b1,b2,…,bn and asks you to restore the values a1,a2,…,an. Can you help her solve the problem?
分析:
所求方法由b反推a,根据题干要求可以把它分成三个部分,a—输入单个数字、x–前b i-1 数中最大数、b–输出数字。b=a+x,每输入一个数字,关键求出x,求b。
代码:
#include"bits/stdc++.h"
using namespace std;
int main(){
int n,a,x=0,b;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a;
b=a+x;
cout<<b<<" ";
if(a>0){
x+=a;
}
}
return 0;
}
今日小结
今天是冬令营开始的第一天,紧跟课程进度。20级讲的是内存,19级先讲的是STL和前一天要求做的题,个人感觉对于我来说题目有点难,不过也是收获满满。明天再接再厉。