您的位置:首页 > 编程语言 > C语言/C++

(PTA详解)L1-003. 个位数统计

2017-12-13 16:03 302 查看


思路:本题的坑点是不超过1000位的整数,所以也便于后期的求出现的个数,采用字符数组开1000空间方式。代码就比较简单了,相信大家能看懂。

代码如下:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
char a[1000];

while(cin>>a)
{
int n0=0,n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0;//赋初值
int m;
m=strlen(a);//求出字符数组长度
for(int i=0;i<m;i++)
{
if(a[i]=='0')
{
n0++;
}
else if(a[i]=='1')
{
n1++;
}
else if(a[i]=='2')
{
n2++;
}
else if(a[i]=='3')
{
n3++;
}
else if(a[i]=='4')
{
n4++;
}
else if(a[i]=='5')
{
n5++;
}
else if(a[i]=='6')
{

n6++;
}
else if(a[i]=='7')
{
n7++;
}
else if(a[i]=='8')
{
n8++;
}
else if (a[i]=='9')
{
n9++;
}
}
if(n0)
{
cout<<"0:"<<n0<<endl;
}
if(n1)
{
cout<<"1:"<<n1<<endl;
}
if(n2)
{
cout<<"2:"<<n2<<endl;
}
if(n3)
{
cout<<"3:"<<n3<<endl;
}
if(n4)
{
cout<<"4:"<<n4<<endl;
}
if(n5)
{
cout<<"5:"<<n5<<endl;
}
if(n6)
{
cout<<"6:"<<n6<<endl;
}
if(n7)
{
cout<<"7:"<<n7<<endl;
}
if(n8)
{
cout<<"8:"<<n8<<endl;
}
if(n9)
{
cout<<"9:"<<n9<<endl;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ oj PTA