2008

统计给定的n个数中 负数 0 正数 出现的个数

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int n;
 6     while(scanf("%d",&n) )
 7     {
 8         int i,num;
 9         int a[3] = {0};
10         if(n == 0) break;
11         for(i = 0;i < n;i++)
12         {
13             scanf("%d",&num);
14             if(num < 0)
15                 a[0]++;
16             else if(num == 0)
17                 a[1]++;
18             else
19                 a[2]++;
20         }
21         printf("%d %d %d
",a[0],a[1],a[2]);
22     }
23     return 0;
24 }

参考c++

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int n, i, a, b, c;
 6     double x;
 7 
 8     while (scanf("%d", &n) , n)
 9     {
10         a = b = c = 0;
11         for (i = 0 ; i < n ; i++)
12         {
13             scanf("%lf", &x);
14             if (x > 0) c++;
15             else if (x < 0) a++;
16             else b++;
17         }
18         printf("%d %d %d
", a, b, c);
19     }
20 
21     return 0;
22 }
========================if i have some wrong, please give me a message, thx.========================
原文地址:https://www.cnblogs.com/ailx10/p/5326094.html