要求从键盘输入若干整数(数据个数应少于50),其值在0~4范围内,用-1作为结束的标志,统计同一整数的个数。
#include<iostream>
using namespace std;
int main()
{
int x[50];
int i=0,a=0,b=0,c=0,d=0,e=0,j;
cout<<"请输入其值在0~4的范围内的若干整数:";
while(x[i]!=-1)
{
i++;
cin>>x[i];
}
for(j=0;j<i;j++)
{
if(x[j]==0)
a++;
else if(x[j]==1)
b++;
else if(x[j]==2)
c++;
else if(x[j]==3)
d++;
else if(x[j]==4)
e++;
}
cout<<"0的个数:"<<a<<endl;
cout<<"1的个数:"<<b<<endl;
cout<<"2的个数:"<<c<<endl;
cout<<"3的个数:"<<d<<endl;
cout<<"4的个数:"<<e<<endl;
return 0;
}