有一个c++程序不会编写,请高手指教!!!! 20
要求让输入一个整数n,输出从1到n中,出现0,1,2,……9的次数比如输入一个10,1输出2次,其他都为一次#include<iostream>usingnamespac...
要求 让输入一个整数n,输出从1到n中,出现0,1,2,……9的次数
比如 输入一个10,1输出2次,其他都为一次
# include <iostream>
using namespace std;
int main ()
{
int n, t, a,b,s0=0,s1=0, s2=0, s3=0, s4=0, s5=0, s6=0, s7=0, s8=0,s9=0;
cout<<"请输入一个正整数:";
cin>>n;
for (int i=1;i<=n;i++)
{
t=i;
b=t;
do
{
t=b;
a=t%10;
b=t/10;
switch (a){
case 0:
s0+=1;
break;
case 1:
s1+=1;
break;
case 2:
s2+=1;
break;
case3:
s3+=1;
break;
case 4:
s4+=1;
break;
case 5:
s5+=1;
break;
case 6:
s6+=1;
break;
case 7:
s7+=1;
break;
case 8:
s8+=1;
break;
default :
s9+=1;
break;
}
}
while (b!=0);
}
cout<<"出现0"<<s0<<"次。"<<endl;
cout<<"出现1"<<s1<<"次。"<<endl;
cout<<"出现2"<<s2<<"次。"<<endl;
cout<<"出现3"<<s3<<"次。"<<endl;
cout<<"出现4"<<s4<<"次。"<<endl;
cout<<"出现5"<<s5<<"次。"<<endl;
cout<<"出现6"<<s6<<"次。"<<endl;
cout<<"出现7"<<s7<<"次。"<<endl;
cout<<"出现8"<<s8<<"次。"<<endl;
cout<<"出现9"<<s9<<"次。"<<endl;
system ("pause");
return 0;
}
这是我写的,可以运行,但是运行结果错误,为什么???? 展开
比如 输入一个10,1输出2次,其他都为一次
# include <iostream>
using namespace std;
int main ()
{
int n, t, a,b,s0=0,s1=0, s2=0, s3=0, s4=0, s5=0, s6=0, s7=0, s8=0,s9=0;
cout<<"请输入一个正整数:";
cin>>n;
for (int i=1;i<=n;i++)
{
t=i;
b=t;
do
{
t=b;
a=t%10;
b=t/10;
switch (a){
case 0:
s0+=1;
break;
case 1:
s1+=1;
break;
case 2:
s2+=1;
break;
case3:
s3+=1;
break;
case 4:
s4+=1;
break;
case 5:
s5+=1;
break;
case 6:
s6+=1;
break;
case 7:
s7+=1;
break;
case 8:
s8+=1;
break;
default :
s9+=1;
break;
}
}
while (b!=0);
}
cout<<"出现0"<<s0<<"次。"<<endl;
cout<<"出现1"<<s1<<"次。"<<endl;
cout<<"出现2"<<s2<<"次。"<<endl;
cout<<"出现3"<<s3<<"次。"<<endl;
cout<<"出现4"<<s4<<"次。"<<endl;
cout<<"出现5"<<s5<<"次。"<<endl;
cout<<"出现6"<<s6<<"次。"<<endl;
cout<<"出现7"<<s7<<"次。"<<endl;
cout<<"出现8"<<s8<<"次。"<<endl;
cout<<"出现9"<<s9<<"次。"<<endl;
system ("pause");
return 0;
}
这是我写的,可以运行,但是运行结果错误,为什么???? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询