数值统计
Problem Description
统计给定的n个数中,负数、零和正数的个数。
Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
Sample Input
6 0 1 2 3 -1 0
5 1 2 3 4 0.5
0
Sample Output
1 2 3
0 0 5
Author
lcy
Source
C语言程序设计练习(二)
解题思路
读入,判断正负,计数。注意可能存在的小数情况。
AC
#include<iostream>
using namespace std;
int main()
{
double temp;
int f, l, z;
int n;
while (cin >> n && n != 0) {
f = 0;
l = 0;
z = 0;
while (n--) {
cin >> temp;
if (temp < 0)f++;
else if (temp == 0)l++;
else z++;
}
cout << f << " " << l << " " << z << endl;
}
return 0;
}
2024.02.29
#include<stdio.h>
using namespace std;
int main() {
int num, neg, zero, pos;
float temp;
while (scanf("%d", &num) != EOF && num != 0) {
neg = 0;
zero = 0;
pos = 0;
for (int i = 0; i < num; i++) {
scanf("%f", &temp);
if (temp < 0)neg++;
else if (temp > 0)pos++;
else zero++;
}
printf("%d %d %d\n", neg, zero, pos);
}
return 0;
}