數值統計
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
Sample Output
1 2 3
0 0 5
Author
lcy
Source
C語言程式設計練習(二)
解題思路
讀入,判斷正負,計數。注意可能存在的小數情況。
AC
#include<iostream>
using namespace std;
int main()
{
double temp;
int a, b, c, 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;
}