hdu 1001 Sum Problem 的解题报告

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1001

Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).

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
1 100
 


Sample Output
1 5050
 

View Code
1 #include < stdio.h >
2   int main()
3 {
4   int a;
5 int i;
6 while (scanf( " %d " , & a) != EOF)
7 {
8 int sum = 0 ;
9 if (a == 1 ) sum = 1 ;
10 else
11 {
12 for ( i = 1 ;i <= a;i ++ )
13 {
14
15 sum = sum + i;
16 }
17 }
18 printf( " %d " ,sum);
19 printf( " \n\n " );
20
21 }
22 return 0 ;
23 }
24
#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)