杭电ACM1001

来源:互联网 发布:网络追踪命令 编辑:程序博客网 时间:2024/05/22 15:15
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
 

Input
The input will consist of a series of integers n, one integer per line.
 

Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
 

Sample Input
1100
 

Sample Output
15050
 

Author
DOOM III
其实博主刚看见这题的时候真的是一脸懵逼,后来....(⊙o⊙)…百度了答案.......好像就是一个前n项求和
当然啦,这都只是说的容易做着难呐,读懂题,有想法才是关键
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
if(n%2==1)
{
cout<<(1+a)/2*a<<endl<<endl;
}
else
{
cout<<a/2*(a+1)<<endl<<endl;
}
}
return 0;
}

原创粉丝点击