2010年7月18日 星期日

[Timus Online Judge] 1068. Sum

This question is too simple.








Time Limit: 2.0 second
Memory Limit: 16 MB
Your task is to find the sum of all integer numbers lying between 1 and N inclusive.
Input
The input consists of a single integer N that is not greater than 10000 by it's absolute value.
Output
Write a single integer number that is the sum of all integer numbers lying between 1 and N inclusive.
Sample
input

-3

output

-5





#include
#include

int main(){
int i,num,sum;
scanf("%d",&num);

sum = 0;
if ( num > 0)
for(i = 1 ; i <= num ; i ++)
sum += i;
else
for(i = 1 ; i >= num ; i --)
sum+=i;

printf("%d",sum);

return 0;
}

Related Posts:

0 意見:

張貼留言