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
错误代码
#include <stdio.h>
int main(){
int n;
int t1=0,t2=0,t3=0;
while(scanf("%d",&n)){
if(n==0)break;
double a;
while(n--){
scanf("%lf",&a);
if(a>0)t1++;
else if(a==0)t2++;
else if(a<0)t3++;
}
printf("%d %d %d\n",t3,t2,t1);
}
return 0;
}
*_以上是错误的,输出的结果会叠加。
输出结果为
6 0 1 2 3 -1 0
1 2 3
5 1 2 3 4 0.5
1 2 8
0
注意 int t1=0,t2=0,t3=0的清空
正确代码
#include <stdio.h>
int main(){
int n;
int t1,t2,t3;
while(scanf("%d",&n)){
if(n==0)break;
double a;
int t1=0,t2=0,t3=0;
while(n--){
canf("%lf",&a);
if(a>0)t1++;
else if(a==0)t2++;
else if(a<0)t3++;
}
printf("%d %d %d\n",t3,t2,t1);
}
return 0;
}