/*
* 播布客教学视频_C学习笔记_8.2_统计1到100中9的个数(函数)
*
* 1.通过函数设计实现
* 2.注释的写法
* 紧贴代码上方
* 代码右侧(变量说明)
* 多行注释(函数上方)
*
* author: syt<sytshanli@163.com>
* create date: 2014.11.28
*/
#include<stdio.h>
/*
* count - count how many digit in num
* @num: the number from 1 to 100
* @digit: digit can be 0,1,....
*
* return value: the counter of digit in this num
*
* 统计函数,如果digit是0那么会出现bug,把while改成do-while(后面必须加分号)
*/
int count(int num,int digit)
{
int count = 0;
do
{
if(num % 10 == digit)
count++;
num /= 10;
}while(num != 0);
return count;
}
int main(void)
{
int i = 0;
int sum = 0; /*the sumary of 9*/
int max = 0; /*the max number to count*/
printf("sumary 9 from 1 to max\n");
scanf("%d",&max);
/*sumary 9 from 1 to max*/
/*for(i = 0;i <= 0;i++),counter(i,0),那么会出现bug */
for(i = 1;i <= max;i++)
{
sum += count(i,9);
}
printf("sum = %d\n",sum);
return 0;
}