配色: 字号:
《C语言程序设计(第2版)》第04章循环结构
2023-05-24 | 阅:  转:  |  分享 
  
第 四 章 循环结构4.1 用格里高利公式求π的近似值 (while语句)4.2 统计一个整数的位数 (do-while语句)4.3 判
断素数 (break 和 continue 语句)4.4 求1!+2!+...+100! (循环嵌套)4.5 循环结构程序设计本章
要点什么是循环? 为什么要使用循环? 如何实现循环?实现循环时,如何确定循环条件和循环体?怎样使用while 和do-while语
句实现次数不确定的循环?while 和do-while语句有什么不同?如何使用break语句处理多循环条件?如何实现多重循环?4.
1 用格里高利公式求π的近似值例4-1使用格里高利公式求π的近似值,要求精确到最后一项的绝对值小于10–4。4.1.1 程序解
析4.1.2 while语句 #include int main(void){ int denominat
or, flag; double item, pi; flag = 1; denominator = 1
; item = 1.0; pi = 0; while(fabs (item) >= 0.0001) {
item = flag 1.0 / denominator; pi = pi + item;
flag = -flag; denominator = denominator +2; } pi = pi
4; printf ( “pi = %f\n”, pi); return 0;}4.1.1 程序解析-求π的近似
值pi = 3.141613 item=0.0 ?fabs(item) < 0.00014.1.2 while 语句while (
条件) 循环体语句; 循环条件循环体一条语句while 语句和for语句都是在循环前先判断条件while 语句说
明表达式1;while (表达式2) { for的循环体语句; 表达式3;}把for语句改写成while语句for(表
达式1; 表达式2; 表达式3) 循环体语句while 和 for 的比较for (i = 1; i <= 10; i+
+) sum = sum + i; i = 1; 循环变量赋初值while (i <= 10){ 循
环条件 sum = sum + i; i++; 循环变量的改变}循环体例4-2 从键
盘输入一批学生的成绩,计算平均成绩,并统计不及格学生的人数。分析:求累加和确定循环条件不知道输入数据的个数,无法事先确定循环次数用
一个特殊的数据作为正常输入数据的结束标志,比如选用一个负数作为结束标志。循环的三种常见的控制方式(计数控制的循环、条件控制的循环、
标记控制的循环) #include Int main(void){ int num; double gra
de, total; num = 0; total = 0; printf(“Enter grades: \n"); scan
f("%lf", &grade); / 输入第1个数/ while (grade >= 0) { /
输入负数,循环结束 / total = total + grade; num++;
if(grade<60) count++; scanf (“%lf”, &grade); } if(n
um != 0) { printf(“Grade average is %.2f\n", total/num);
printf("Number of failures is %d\n", count); } els
e printf(" Grade average is 0\n"); return 0;}Enter gr
ades: 67 88 73 54 82 -1Grade average is 72.80Number of failures i
s 14.2 统计一个整数的位数例4-3 从键盘读入一个整数,统计该数的位数。4.2.1 程序解析4.2.2 do - whi
le语句 int main(void){ int count, number; count = 0;printf("Enter
a number: ");scanf ("%d", &number) ;if (number < 0) number = -n
umber; do { number = number / 10; count ++; } while
(number != 0);printf("It contains %d digits.\n", count); ret
urn 0;}4.2.1 程序解析-统计一个整数的位数Enter a number: 12534It contains 5 di
gits.Enter a number: -99It contains 2 digits.Enter a number: 0It
contains 1 digits.while (number != 0) { number = number / 10
; count ++;}4.2.2 do - while 语句do { 循环体语句} while (表达式)先循环后判断
真假表达式循环体语句do-while的下一条语句 while 是先判别条件,再决定是否循环;do-while 是先至少循环一次,然
后再根据循环的结果决定是否继续循环。while 和 do-while 的比较4.3 判断素数例4-4 输入一个正整数m,判断它是否
为素数。4.3.1 程序解析4.3.2 break语句 和continue语句4.3.1 程序解析-判断素数算法:除了1和m,
不能被其它数整除。设 i 取值 [2, m-1] 如果m不能被该区间上的任何一个数整除,即对每个i,m%i 都不为0,则m是素数
只要找到一个i,使m%i为0,则m肯定不是素数m %2 %3 %4
%5 %(m-1)不是素数 || =0 =0是素数 && !=0
!=0 m不可能被大于 m/2 的数整除 i 取值 [2, m-1] 、 [2, m/2] 、 [2, ]f
or(i = 2; i <= m/2; i++) if(m % i == 0) break;if(i > m/2) pr
intf("yes\n")else printf("no\n”);int main(void){ int i, m; print
f(“Enter a number: "); scanf ("%d", &m); for (i = 2; i <= m/2; i
++) if (m % i == 0) break; if (i > m/2&&m!=1 )
printf("%d is a prime number! \n", m); else prin
tf("No!\n"); }例4-4源程序-判断素数Enter a number: 9NoEnter a numbe
r: 1111 is a prime number!for (i = 2; i <= m/2; i++) if
(m % i == 0) printf("No!\n"); else printf("%d is a prime numb
er! \n", m); 循环条件?循环的结束条件?4.3.2 break 语句while(exp){ 语句1 if (e
xpb) break; 语句2}for (i = 2; i <= m/2; i++) if (m
% i == 0) break; if (i > m/2 ) printf("Yes"); else printf("
No!\n"); 当循环有多个出口时:表示循环条件区分结束条件for(i = 2; i <= m/2; i++) if(m%
i == 0){ printf("No!\n"); break; }printf("Yes"
); continue 语句while(exp){ 语句1 if (expb) continue; 语句2}跳过c
ontinue后面的语句,继续下一次循环break 和 continue#include "stdio.h"int main(vo
id){ char c; int i; for (i = 0; i < 10; i++) { c = get
char(); if (c == ''\n'') break; putchar(c);
}}abc↙efgh ↙123 ↙abcabcefgh1continue;break 和 continue例如 以下程序段输出
100~200之间所有能被3整除的数。for(i = 100;i <= 200;i++) { if( i % 3 != 0 )
continue; printf(“%d ”,i);}for(i = 100;i <= 200;i++) if(i
% 3 == 0) printf(“%d ”,i);例4-5 简单的猜数游戏。最多允许猜5次。# inclu
de int main(void){ int mynumber = 38;
/ 计算机指定被猜的数 / int count = 0,yournumber; f
or(count = 1;count <= 5;count++) { printf("Input your
number: "); scanf("%d", &yournumber); if(yo
urnumber == mynumber) { printf("Ok! you are right
! \n"); break; / 已猜中,游戏结束,终止循环
/ } else if(yournumber > mynumber )
printf("Sorry! your number is bigger than my number!
\n"); else printf("Sorry! your number
is smaller than my number! \n"); } printf("Game is over!\n
"); return 0; }4.4 求1! + 2! + …. + 100!for (i = 1; i <= 10
0; i++){ item = i ! sum = sum + item;}4.4.1 程序解析调用函数 fact(
i) 计算 i 的阶乘4.4.2 嵌套循环用循环计算 i 的阶乘#include double fact (in
t n); int main(void){ int i; double sum; sum = 0; for(i
= 1; i <= 100; i++ ) sum = sum + fact (i); printf("1! +
2! + 3! + … + 100! = %e\n", sum); return 0;}double fact (int
n){ int i; double result = 1; for (i = 1; i <= n; i
++) result = result i ; return result ; }4.4.1 程序解析
求1! + 2! + …. + 100!1! + 2! + … + 100! = 9.426900e+157 4.4.2 嵌套循
环for (i = 1; i <= 100; i++){ item = i ! sum = sum + item;}f
or(i = 1; i <= 100; i++) { item = 1; for (j =
1; j <= i; j++) item = item j; sum = sum + item; }#includ
e int main(void){ int i, j; double item, sum;
/ item 存放阶乘 / sum = 0; for(i = 1; i <= 100; i++) {
item = 1; / 每次求阶乘都从1开始
/ for (j = 1; j <= i; j++) / 内层循环算出 item = i! /
item = item j; sum = sum + item; } printf("
1! + 2! + 3! + … + 100! = %e\n", sum);}例4-7 源程序内层循环的初始化for(i = 1;
i <= 100; i++) { item = 1; for (j = 1; j <=
i; j++) item = item j; sum = sum + item; }求1! + 2! + ….
+ 100!item = 1;for(i = 1; i <= 100; i++){ for (j = 1; j <=
i; j++) item = item j; sum = sum + item; }求1! + 1!
2! + …… + 1!2!……100! 分析嵌套循环的执行过程for(i = 1; i <= 100; i++) {
item = 1; for (j = 1; j <= i; j++) item =
item j; sum = sum + item; } 外层循环变量 i 的每个值内层循环变量 j 变化一个轮次; 内
外层循环变量不能相同分别用 i 和 jfor (i = 1; i <= 100; i++) for (j = 1; j <=
i; j++) printf ("%d %d\n", i, j );4.5 循环结构程序设计循环程序的实现要
点:归纳出哪些操作需要反复执行? 循环体这些操作在什么情况下重复执行? 循环条件选用合适的循环语句for while do-
while循环具体实现时考虑(循环条件):事先给定循环次数,首选for通过其他条件控制循环,考虑while或do-while循环语
句的选择if(循环次数已知) 使用for语句else / 循环次数未知 / if (循环条件在进入循环时
明确) 使用while语句 else / 循环条件需要在循环体中明确 / 使用do-while语句#inclu
de int main(void){ int i, mark, max, n; printf("E
nter n: "); scanf ("%d", &n); printf("Enter %d marks: ", n
); scanf ("%d", &mark); / 读入第一个成绩 / max = mar
k; / 假设第一个成绩是最高分 / for (i = 1; i < n; i++ ){
scanf ("%d", &mark); if (max < mark)
max = mark; } printf("Max = %d\n", max); return 0;}例
4-8 输入一批学生的成绩,求最高分(for)maxmarkEnter n: 5Enter 5 maks:67 88 73 54
82Max = 88Enter n: 0#include int main(void){ int mark,
max; printf(“Enter marks:"); scanf ("%d", &mark); / 读
入第一个成绩 / max = mark; / 假设第一个成绩最高分 / while (mark
>= 0){ if(max < mark) max = mark ;
scanf ("%d", &mark ); }; printf("Max = %d\n", max); ret
urn 0;}例4-8 输入一批学生的成绩,求最高分(while)Enter marks:67 88 73 54 82 -1Max
= 88Enter marks:-1#include int main(void){ int mark,
max; max = -1; / 给max赋一个小初值 / printf(“Enter marks
: "); do{ scanf ("%d", &mark ); if (max < ma
rk) max = mark; } while(mark >= 0); p
rintf("Max = %d\n", max);}例4-8 输入一批学生的成绩,求最高分(do-while)Enter mark
s: 67 88 73 54 82 -1Max = 88Enter marks: -1例4-9 逆序问题。将一个正整数逆序输出确
定:循环条件和循环体(循环不变式) 12345 5 4 3 2 112345 % 10 =
5 12345 / 10 = 1234 1234 % 10 = 4 1234 / 10 = 123
123 % 10 = 3 123 / 10 = 12 12 % 10 = 2
12 / 10 = 1 1 % 10 = 1 1 / 10 = 0 结束循环不变式
x%10 x=x/10循环结束条件 x==0scanf( “%d”, &x);while (x != 0){
digit = x %10; x = x/10 ; printf( "%d ", digit);}用do-w
hile实现?例4-10 求100以内的全部素数,每行输出10个for (m = 2; m <= 100; m++) if
(m是素数) printf( "%d", m);n = sqrt(m);for(i = 2; i <= n; i++)
if(m % i == 0) break;if(i > n) printf("yes\n")else printf("no\n”
);for (m = 2; m <= 100; m++){ n=sqrt(m); for(i = 2; i <= n;
i++) if(m % i == 0) break; if(i > n) printf("%d", m)
}例4-10 源程序#include #include int main(void){
int count, i, m, n; count = 0; for (m = 2; m <= 100;
m++){ n = sqrt(m); for (i = 2; i <= n; i++)
if(m % i == 0) break; if(i > n){ / 如果m是素数 /
printf("%6d", m); count++; /
每行10个的处理 / if (count %10 == 0) printf(“\n”);
} }} 2 3 5 7 11 13 17 19 23
29 31 37 41 43 47 53 59 61 67 71 73
79 83 89 97 例4-11 求Fibonacci序列:1,1,2,3,5,8,13,… 1, 1
, 2, 3, 5, 8, 13, ……x1 x2 x x1 x2 xx1 = x2 = 1
;x = x1 + x2;x1 = x2;x2 = x;x1 = 1;x2 = 1;printf ("%6d%6d", x1, x
2 ); / 输出头两项 /for (i = 1; i <= 8; i++){ /
循环输出后8项 / x = x1 + x2; / 计算新项 /
printf("%6d", x); x1 = x2;
/ 更新x1和x2 / x2 = x; }例4-12古典算术问题-搬砖头某地需要搬运砖块,已知
男人一人搬3块,女人一人搬2块,小孩两人搬一块。问用45人正好搬45块砖,有多少种搬法?for (men = 0; men <=
45; men++) for (women = 0; women <= 45; women++) for (chil
d = 0; child <= 45; child++) if ((men+women+child==45)
&& (men3+women2+child0.5==45)) printf("men=%d
women=%d child=%d\n", men, women, child);}例4-12 源程序(2)for (men = 0; men <= 15; men++)for (women = 0; women <= 22; women++){ child = 45 – women – men; if (men 3 + women 2 + child 0.5 == 45) printf("men=%d women=%d child=%d\n", men, women, child);}for (men = 0; men <= 45; men++) for (women = 0; women <= 45; women++) for (child = 0; child <= 45; child++) if ((men+women+child==45) && (men3+women2+child0.5==45)) printf("men=%d women=%d child=%d\n", men, women, child);}比较循环次数本章总结循环结构以及循环执行机制:while语句do-while语句for语句break语句与continue语句嵌套循环的使用循环结构程序的综合设计几个常用的算法正确理解 while语句和 do-while语句的执行机制;掌握 break 和 continue 语句的作用方式;掌握嵌套循环的执行机制与设计方法;能合理运用循环语句熟练编写循环结构类的程序; 熟练掌握几个常用的算法;
献花(0)
+1
(本文系小磊老师首藏)