【水】HDU2008 数值统计

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/32768 K (Java/Others

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

Hint
JGShining
Source
  C语言程序设计练习(二)  
Related problem
2010 2000 2001 2005 2012

这道题还是循环以及分支统计问题。代码如下:

#include <iostream>
using namespace std;
int main ()
{
    int n;
    while (cin >> n ,n!=0)
    {
        int s1=0,s2=0,s3=0;
        double a;
        for(int i=0;i<n;i++)
        {
            cin >> a;
            if(a <0)
                s1++;
            else if (a > 0)
                s3 ++;
            else
                s2++;
        }
        cout<<s1<<" " <<s2 <<" "<<s3<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41627235/article/details/82786473