Description
Write a program that reads in ten numbers and then outputs the sum of all positive numbers, the sum of all nonpositive numbers, and the sum of all the ten numbers. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.
Input
Ten numbers
Output
The sum of the numbers greater than zero, the sum of the numbers less than zero, and the sum of all numbers, seperated by commas. Each value output should be rounded to 2 digits after the decimal point.
Sample Input
Sample Output
HINT
#include <cstdio>
int main()
{
float a[10];
while (scanf("%f",&a[0])!=EOF)
{
for (int i = 1; i < 10; i++)
scanf("%f", &a[i]);
float positive = 0, negative = 0, all = 0;
for (int i = 0; i < 10; i++)
{
all += a[i];
if (a[i] > 0)
positive += a[i];
else if (a[i] < 0)
negative += a[i];
}
printf("%.2f,%.2f,%.2f\n", positive, negative, all);
}
return 0;
}
本文介绍了一个程序设计任务,该程序能够读取十个数,并分别计算所有正数、所有非正数及全部数字的总和。输入的数字可以任意顺序给出,且只需要输入一次。最终输出三个总和,每个总和保留两位小数。
800

被折叠的 条评论
为什么被折叠?



