播布客教学视频_C学习笔记_8.2_统计1到100中9的个数(函数)

/*
 *	播布客教学视频_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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值