-
Lesson 1 - 最简单的C程序
-
Lesson 2 - 打印输出
-
Lesson 3 - 循环打印
-
Lesson 4 - 判断奇偶
-
Lesson 5 - 从1加到100求和
-
Lesson 6 - 乘法表
-
Lesson 7 - 求100以内的最大素数
-
Lesson 8 - 1到100有多少个9
-
Lesson 9 - 整型转字符串
-
Lesson 10 - 约瑟夫环
-
Lesson 11 - 求两个坐标点之间的距离
-
Lesson 12 - 判断机器存储是否小尾端
-
Lesson 13 - 对不起,你的车今天限行
-
Lesson 14 - 判断地图上某点是否有出路
-
Lesson 15 - 统计一个数二进制表示中1的个数
-
Lesson 16 - 字符串拷贝
-
Lesson 17 - 统计单词个数
-
Lesson 18 - 实现 printf
-
Lesson 19 - 命令解释器
-
Lesson 20 - 预处理器实现
-
Lesson 21 - 词法分析器实现
-
Lesson 22 - 猜数游戏
-
Lesson 23 - 五子棋
-
Lesson 24 - 超链接分析器
-
Lesson 25 - cp命令实现
-
Lesson 26 - ELF文件头分析器实现
-
Lesson 27 - 简单流处理器实现和正则表达式
-
Lesson 28 - 数学计算器实现
-
Lesson 29 - 数学计算器实现more命令实现
-
Lesson 30 - sort命令实现
-
Lesson 31 - ls -l命令实现
-
Lesson 32 - Bash项目
-
Lesson 33 - 动态数组实现
-
Lesson 34 - 约瑟夫环问题
-
Lesson 35 - 表达式求值问题
-
Lesson 36 - 广度优先解决迷宫问题
-
Lesson 37 - 词频统计器
-
Lesson 38 - 堆排序问题
-
Lesson 39 - 构造符号表
-
Lesson 40 - MyDictionary项目
-
Lesson 41 - BSearch 实现
-
Lesson 42 - QSort 实现
-
Lesson 43 - 深度优先解决迷宫问题
-
Lesson 44 - KMP 算法实现
-
Lesson 45 - 最长公共子序列(LCS)问题
-
Lesson 46 - Dijkstra 算法
-
Lesson 47 - Huffman Coding 算法
-
Lesson 48 - 地图导航项目
## Lesson 8 Find 9 in number 1 to 100 1到100有多少个9 /* * main.c - find how many digit 9 from 1 to 100 * * Copyright (C) AKAE - li ming limingth@gmail.com * */ #include <stdio.h>
/*
* find - calculate how many digit in num
* @num: the number we want to find
* @digit: the digit we search in num
*
* Return value: how many digit in this num
*
*/
int find(int num, int digit)
{
int counter = 0; /* the result of how many digit in num */
do {
/* get the last digit of num */
if (num % 10 == digit)
counter++;
/* get rid of the last digit */
num = num / 10;
} while (num != 0);
return counter;
}
int main(void)
{
int begin = 1; /* the begin number */
int end = 100; /* the end number */
int i = 0;
int sum = 0; /* the result of sumary */
/* calculate how many 9 in 1 to 100 */
for (i = begin; i <= end; i++) {
sum += find(i, 9);
}
printf("sum = %d \n", sum);
return 0;
}
copy
知识点
- 函数 Function
- 形参和实参
- 函数返回值
- 逻辑分解
- 注释的写法
课堂讨论
- 示例中的 0, 100 为何要用 begin, end 来定义,直接写在 for 循环中可以吗?
- find 中的 do-while 改成 while 可以吗?
- 为什么不写一个函数,直接就能计算出1-100中的9的个数?
课后练习
- 求1-100以内最大的素数,要求用设计一个函数实现。
- 用户输入两个数字,按从个位对齐的方式,找出这2个数在相同位置处数字也相同的个数。
例如:123 和 5173 这2个数字,位置相同数字也相同的个数是 2
名人名言
- Brian W.Kernighan (C Programming Language 一书合作者)
- ”I don't think that I have any special insight, but it has always seemed to me best to do something that you really enjoy doing. If you have a job that is fun, where you are eager to start in the morning and hate to quit in the evening, that's what you want.“
- 我不认为自己有任何特别的远见,但是对于我来说做自己喜欢的事情似乎是最好的。如果你有一份有趣的工作,在早上你非常渴望开始做,并且在傍晚的时候你又不愿停下来,那这就是你想要的。