问题
一本书的页数为 N ,页码从 1 开始编起,请你求出全部页码中,用了多少个 0,1,2…9 。
1.分析问题
- 已知:页数为N
- 未知:用了多少个0,1,2…9。
- 关系:计数
2.定义变量
//二、数据定义
int n,a[10]={
0},temp;
3.输入数据
//三、数据输入
cin>>n;
4.数据计算
里层通过短除法获得当前数的值,并将它统计到数组中。然后通过外层循环将数不断减小,取得每一个数。
//四、数据计算
while(n>0){
temp=n;
while(temp>0){
int i=temp%10;
a[i]+=1;
temp/=10;
}
--n;
}
5.输出结果
#include<iostream>
using namespace std;
int main(){
//一、分析问题
//已知:页数为N
//未知:用了多少个0,1,2…9。
//关系:计数
//二、数据定义
int n,a[10]={
0},temp;
//三、数据输入
cin>>n;
//四、数据计算
while(n>0){
temp=n;
while(temp>0){
int i=temp%10;
a[i]+=1;
temp/=10;
}
--n;
}
//五、输出结果
for(int i=0;i<10;i++){
cout<<a[i]<<endl;
}
return 0;
}