c++数值统计
来源:百度知道 编辑:UC知道 时间:2024/06/03 07:42:07
题目描述
统计给定的n个数中,负数、零和正数的个数。
输入
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
输出
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
样例输入
6 0 1 2 3 -1 0
5 1 2 3 4 0.5
0
样例输出
1 2 3
0 0 5
以下是我写的输入的控制有些问题。请大家帮忙改下。
#include <iostream.h>
#include<string>
#include <stdio.h>
#include<math.h>
int main()
{
void shusr(int n);
int n;
while(scanf("%d",&n)!=EOF)
{
shusr(int (n));
}
return 0;
}
void shusr(int n)
{
int a=0, b=0, c=0;
float x;
while(n)
{
cin>>x;
if(x<0)
a++;
if(x==0)
b++;
else if(x>0)
c++;
n--;
}
cout<<a<<" "<<b<<" &
统计给定的n个数中,负数、零和正数的个数。
输入
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
输出
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
样例输入
6 0 1 2 3 -1 0
5 1 2 3 4 0.5
0
样例输出
1 2 3
0 0 5
以下是我写的输入的控制有些问题。请大家帮忙改下。
#include <iostream.h>
#include<string>
#include <stdio.h>
#include<math.h>
int main()
{
void shusr(int n);
int n;
while(scanf("%d",&n)!=EOF)
{
shusr(int (n));
}
return 0;
}
void shusr(int n)
{
int a=0, b=0, c=0;
float x;
while(n)
{
cin>>x;
if(x<0)
a++;
if(x==0)
b++;
else if(x>0)
c++;
n--;
}
cout<<a<<" "<<b<<" &
//如果提示说iostream出问题的话,在后面加上".h",你应该在代码中加入using namespace std;
#include <iostream>
#include<string>
#include <stdio.h>
#include<math.h>
using namespace std;
int main()
{
void shusr(int n);
int n;
while(scanf("%d",&n)!=EOF)
{
shusr(int (n));
}
return 0;
}
void shusr(int n)
{
int a=0, b=0, c=0;
float x;
while(n)
{
cin>>x;
if(x<0)
a++;
if(x==0)
b++;
else if(x>0)
c++;
n--;
}
cout<<a<<" "<<b<<" "<<c;
cout<<endl;
}