hdu 1001 Sum Problem 的解题报告
2011-04-01 19:50
351 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1001
[align=left]Problem Description[/align]
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
[align=left]Input[/align]
The input will consist of a series of integers n, one integer per line.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
100
[align=left]Sample Output[/align]
1
5050
View Code
#include<stdio.h>
int main()
{
int a;
int i;
while(scanf("%d",&a)!=EOF)
{
int sum=0;
if(a==1) sum=1;
else
{
for( i=1;i<=a;i++)
{
sum=sum+i;
}
}
printf("%d",sum);
printf("\n\n");
}
return 0;
}
[align=left]Problem Description[/align]
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
[align=left]Input[/align]
The input will consist of a series of integers n, one integer per line.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
100
[align=left]Sample Output[/align]
1
5050
View Code
#include<stdio.h>
int main()
{
int a;
int i;
while(scanf("%d",&a)!=EOF)
{
int sum=0;
if(a==1) sum=1;
else
{
for( i=1;i<=a;i++)
{
sum=sum+i;
}
}
printf("%d",sum);
printf("\n\n");
}
return 0;
}
#include<stdio.h> int main() { int n,sum; //s=N*(N+1)/2 while(scanf("%d",&n)==1 ) { sum=0; if( n%2 ) sum=(n+1)/2*n; else sum=n/2*(n+1); printf("%d\n\n",sum); } return 0; }
相关文章推荐
- hdu 1001 Sum Problem 解题报告 数据范围
- hdu 2058 解题报告 - The sum problem
- The sum problem(hdu 2058)解题报告
- BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告
- JAVA hdu 1000(A + B Problem)&&hdu 1001(Sum Problem)
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1001)
- HDU 1001 Sum Problem 水题
- HDU 4407 Sum 解题报告(容斥原理)
- HDU 4716 A Computer Graphics Problem 解题报告
- HDU 1001 Sum Problem C/C++
- Codeforces Round #365 (Div. 2) Problem D.Mishka and Interesting sum 解题报告
- HDU 1001 Sum Problem JAVA
- hdu1001 Sum Problem
- 解题报告:HDU_6128:Inverse of sum (二次剩余)
- BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
- hdu 4704 Sum(费马小定理)解题报告
- BestCoder5 1001 Poor Hanamichi(hdu 4956) 解题报告
- hdu 5373 - The shortest problem 解题报告
- HDU 3666 THE MATRIX PROBLEM 解题报告(差分约束)
- hdu 1001 sum problem