关于c语言的一道题

来源:百度知道 编辑:UC知道 时间:2024/06/30 09:18:29
#include<stdio.h>
void main()
{
int i,n,sum=0;
scanf("%d", &n);
for(i=1;i<=n;i++)
sum+=i;
printf("%d\n\n",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
1

5050

以上,原题

没有语法错误,具体到有没有算法错误要看题目的具体要求。

考虑到可能会为n赋值为0,所以应该将for循环中的i=1改为i=0

没有任何错误啊
你把错误发出来看看

程序可以执行,是求你输入的数字之前的所有数的和
你说答案错了?你写这个程序的想得到什么结果?

应该是你的输出格式不对吧,去掉printf里面的一个\n试试