一本书的页码从自然数1开始顺序编码直到自然数n,书中的页码按照通常的习惯编排,每一个页码都有不含多余的前导数字 。例如,第六页用6表示二不是06或006,编写一个程序,对给定的书的总页码,计算出该书的全部页码中分别用到多少次的数字
#include<iostream>
using namespace std;
int main()
{
int n, i, t;
int count[10] = { 0 };
cin >> n;
for (i = 1; i <= n; i++)
{
t = i;
while (t)
{
count[t % 10]++;
t /= 10;
}
}
for (i = 0; i < 10; i++)
{
cout << "i" << ":" << count[i] << " ";
if (i == 4)
cout << endl;
}
cout << endl;
return 0;
}