B - Stone Monument
Time limit : 2sec / Memory limit : 256MB
Score : 200 points
Problem Statement
In some village, there are 999 towers that are 1,(1+2),(1+2+3),…,(1+2+3+…+999) meters high from west to east, at intervals of 1 meter.
It had been snowing for a while before it finally stopped. For some two adjacent towers located 1 meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.
Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.
Assume also that the depth of the snow cover is always at least 1 meter.
Constraints
- 1≤a<b<499500(=1+2+3+…+999)
- All values in input are integers.
- There is no input that contradicts the assumption.
[题目解释]
这里有999座塔,第1座高度为1,第二座高度为(1+2),…,第999座高度为(1+2+3+…+999),现在有雪覆盖在塔的底部即使塔的高度降低雪的厚度,给你两座相邻且高度分别为a,b(a<b)的塔,求雪的厚度为多少?
[题目解析]
我们发现第1座塔高度为1,第二座塔高度为(1+2),得到第n座塔高度为公差为1从1到n的等差数列之和即(1+2+…+n)=(1+n)/2,因为a,b两座塔是相邻的,也就是说b-a可以得到b的等差数列最后一项,这样我们就可以等差数列求和公式算出b塔最初的高度,b塔的最初高度减去现在b塔的高度及为答案(当然你也可以将b-a-1算出a等差数列的最后一项,从而算出a的原始高度,再算出雪的厚度)
[代码]
/* Name: Stone Monument Author: FZSZ-LinHua Date: 2018 06 10 Exec time: 1ms Memory usage: 256KB Score: 200 Algorithm: Brute-force */ # include "iostream" # include "cstdio" using namespace std; int a,b,now; int main(){ scanf("%d%d",&a,&b); now=b-a; //算出b等差数列的最后一项 printf("%d",(now+1)*now/2-b); //(now+1)*now/2为b塔的原始高度(等差数列求和公式) return 0; }
原文地址:https://www.cnblogs.com/FJ-LinHua/p/9164909.html