由题意可知是让我们设计功能函数,本体思路为:从1 到 n一共n个数,分别对每个数一次由个位往高位判断是否为9,就是先求余,再整除。
源代码如下:
1 #include <stdio.h>
2
3 void count(int x);
4 int main()
5 {
6 int n;
7 printf("Please input a number:\n");
8 scanf("%d",&n);
9 count(n);
10 return 0;
11 }
12
13 void count(int x)
14 {
15 int i,tmp,b,count=0;
16
17 for(i = 1;i <= x;i++)
18 {
19 tmp=i;
20 while(tmp != 0)
21 {
22 b=tmp % 10;
23 tmp=tmp /10;
24 if(b==9)
25 {
26 count++;
27 }
28 }
29 }
30 printf("一共有%d个9\n",count);
31 }