Question Description:
Input:
k = 1, n = 1
Output:
1
Explanation:
In [0, 1], we find that 1 appears 1 time (1).
Input:
k = 1, n = 12
Output:
5
Explanation:
In [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we find that 1 appears 5 times (1, 10, 11, 12) (note that there are two 1s in 11).
Analysis: When k=0, 0 starts to be one more than other numbers less than or equal to 10.
Code:
#include<iostream>
using namespace std;
int f(int ,int );
int main()
{
int i;
i=f(0,29);
cout<<i<<endl;
}
int f(int k,int n)
{
int count ;//作为返回值
if(k==0)
count=1;
for(int i=0;i<=n;i++)
{
int m=i;
while(m)
{
if(m%10==k)
{
count++;
}
m/=10;
}
}
return count;
}