错哪了

木可大大 2008-09-22 11:25:43
Problem Description
Your task is to Calculate the sum of some integers.

Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

Sample Input
4 1 2 3 4
5 1 2 3 4 5
0

Sample Output
10
15
我的代码是
#include <stdio.h>
#include<stdlib.h>
int main()
{
int n,temp=0,sum=0;
scanf("%d",&n);
while(n--)
{
scanf("%d",&temp);
while(temp)
{
sum+=temp;
printf("%d\n",sum);

}
}
return 0;
}
错哪了?
...全文
230 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bargio_susie 2008-09-23
  • 打赏
  • 举报
回复
这样行不行?

#include <stdio.h>

int main()
{
int n,temp=0,sum=0;
int a[1024];
int i = 0 ,j;

while(1)
{
scanf("%d", &n);
if (n == 0)
{
printf("\noutput\n");
for(j=0; j<i; j++)
printf("%d\n", a[j]);
break;
}
while(n--)
{
scanf("%d", &temp);
sum += temp;
}
a[i++] = sum;
sum = 0;
}

return 0;
}
devil_zuiai 2008-09-23
  • 打赏
  • 举报
回复
while(temp)
{
sum+=temp;
printf("%d\n",sum);

}
这是一个死循环
ForestDB 2008-09-23
  • 打赏
  • 举报
回复

#include <stdio.h>

int main(void)
{
int i, j, k, s;

while(1)
{
scanf("%d", &i);
if(i == 0)
break;
s = 0;
for(j = 0; j < i; ++j)
{
scanf("%d", &k);
s += k;
}
printf("%d\n", s);
}

return 0;
}
  • 打赏
  • 举报
回复
貌似之前见过这个帖子
mifeixq 2008-09-23
  • 打赏
  • 举报
回复
经过测试,下面代码AC

#include <stdio.h>

int main()
{
while(1)
{
int n,temp=0,sum=0;
scanf("%d",&n);
if(!n)
break;
while(n--)
{
scanf("%d", &temp);
sum+=temp;
}
printf("%d\n", sum);
}
return 0;
}

Teaerror 2008-09-23
  • 打赏
  • 举报
回复
用TC编译是没有错的,但别说第2个循环是个死循环了,如果你输入的n是个负数,也是死循环。
oo_v_oo 2008-09-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bargio_susie 的回复:]
这样行不行?


C/C++ code#include <stdio.h>

int main()
{
int n,temp=0,sum=0;
int a[1024];
int i = 0 ,j;

while(1)
{
scanf("%d", &n);
if (n == 0)
{
printf("\noutput\n");
for(j=0; j<i; j++)
printf("%d\n", a[j]);
break;
}
while(n--)
{
sc…
[/Quote]
xiansizhe 2008-09-23
  • 打赏
  • 举报
回复
只答题不结贴..
terminatorbin 2008-09-23
  • 打赏
  • 举报
回复
#include <stdio.h> 
#include <stdlib.h>
int main()
{
int n,temp=0,sum=0;
scanf("%d",&n);
while(n--)
{
scanf("%d",&temp);
/*这里的while循环是一个死循环,如果输入一个不为0的数时,就不断的执行下去!由于temp的值没有改变,所以跳不出这个循环,导致出错*/
while(temp)
{
sum+=temp;
printf("%d\n",sum);

}

}
return 0;
}

楼主,应该学会一下怎么样调试程序才行,编程容易,查错、调试才难啊!
别外,不是很明白,你这道题的目的是什么!
可以,讲訹一下,程序要实现什么功能吗?
luxiaoxun 2008-09-22
  • 打赏
  • 举报
回复
you should output their sum in one line, and with one line of output for each line in input.
输出不符合要求
alfredsue 2008-09-22
  • 打赏
  • 举报
回复
while(temp)
{
sum+=temp;
printf("%d\n",sum);

}
你这循环是什么意思?这个循环能出去么?

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧