简单的加法算sum,但我怎么提交都不对!
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
15050你说我怎么正好能知道输入2行后回车就输出结果呢,如果输入3行数字我就不计算了和了吗?这是杭州电子科技大学acm的第1001,谁用java提交成功过给发上来看看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
15050你说我怎么正好能知道输入2行后回车就输出结果呢,如果输入3行数字我就不计算了和了吗?这是杭州电子科技大学acm的第1001,谁用java提交成功过给发上来看看http://acm.hdu.edu.cn/showproblem.php?pid=1001
int main(void)
{
int sum,n;
while((scanf("%d",&n))!=EOF)
{
if((n&1) == 0)
sum=n/2*(n+1);
else
sum=(n+1)/2*n;
printf("%d\n\n",sum);
}
return 0;
}
看下FAQ