当前课程知识点:C Programming >  Chapter 5 Loop Structure >  5.5 Loop Structure Program example 2 >  5.5 Loop structure program example 2.mp4

返回《C Programming》慕课在线视频课程列表

5.5 Loop structure program example 2.mp4在线视频

下一节:【Source program】example 5. Output the following figure.

返回《C Programming》慕课在线视频列表

5.5 Loop structure program example 2.mp4课程教案、知识点、字幕

大家好,我是云南大学信息学院丁海燕老师

欢迎走进C语言程序设计课堂

今天我们接着讲解循环结构程序举例2

【例5】编程序,输出以下图形

使用双重循环实现

思路:

一共有4 行,每行由空格和星号组成

空格数按行增加,星号按行减少

变量 i 控制输出行数, 从1变化到4

变量 j 控制输出每行的空格和星号:

j 从1变化到 i,每次输出一个空格

j 从1变化到9 -2*i,每次输出一个星号

算法如图所示:

外循环,变量 i 控制输出行数,从1变化到4

内循环,顺序执行下列三个语句,

for语句j 从1变化到 i,每次输出一个空格

for语j 从1变化到9 -2*i,每次输出一个星号

输出一个换行符

程序如下:

{ int i,j;

for (i=1; i<=4; i++)

{ for (j=1; j<=i; j++)

printf(" ");

for (j=1;j<=9-2*i;j++)

printf("*");

printf(" ");} }

练习.打印如下的简单图案.

下面来看【例6】百鸡百钱问题

问题描述:一百个铜钱买一百只鸡

其中公鸡一只5 钱、母鸡一只3 钱

小鸡一钱3 只,问有多少种买法?

问题分析:

题意要求输出所有可能的方法

这是典型的穷举法

首先引入三个整数变量chick,hen,cock 分别

代表小鸡、母鸡和公鸡购买数量。考虑到一百个铜钱

顶多能买20 只公鸡,因此,cock 的取值范围

必在0~20 之间;hen 的变化范围应为0~(100-cock*5)/3

且满足条件: cock+hen+chick=100

cock*5+hen*3+chick/3=100

采用循环嵌套可解决此问题

程序描述:

{ int cock,hen,chick,count=0;

for(cock=0;cock<20;cock++)

for(hen=0;hen<33;hen++)

{ chick = 100 - cock-hen;

if ( (chick % 3 ==0) && (cock*5+hen*3+chick/3)==100)

{ count++;

printf("No.%d: cock=%d hen=%d chick=%d "

程序运行结果如图所示

请自行完成以下练习

1.用一元人民币兑换成1分、2分和5分硬币

共有多少种不同的兑换方法

2.有30个人,其中有男人、女人和小孩

3.在一家饭馆里吃饭共花了50先令,每个男人各花3先令

每个女人各花2先令,每个小孩各花1先令

问男人、女人和小孩各有几人?

例7.求 的近似值

解题思路:

–求 近似值的方法很多,本题是一种

–其他方法:

每项的分子都是1

后一项的分母是前一项的分母加2

第1项的符号为正,从第2项起

每一项的符号与前一项的符号相反

找到这个规律后,就可以用循环来处理了。

例如前一项的值是 ,则可以推出下一项为.

后一项的符号与上一项符号相反

在每求出一项后,检查它的绝对值是否>=10的负六次方

如果是,则还需要继续求下一项,直到某一项的值<=10的负六次方

则不必再求下一项了,算法的N-S结构图如下

程序如下:

{ int sign=1; double pi=0,n=1,term=1;

while(fabs(term)>=1e-6)

{ pi=pi+term;

n=n+2;

sign=-sign;

term=sign/n;

pi=pi*4;

printf("pi=%10.8f ",pi);

其中,fabs(term)是求绝对值的函数

输出pi=3.14159065 只保证前5位小数是准确的

如果想提高精度,把while(fabs(term)>=1e-6)

中的1e-6改为1e-8,则输出pi=3.14159263

例8. 译密码。为使电文保密,往往按一定规律

将其转换成密码,收报人再按约定的规律将其译回原文

例如,可以按以下规律将电文变成密码

将字母A变成字母E, a变成e,即变成

其后的第4个字母,W变成A, X变成B, Y变成C, Z变成D

字母按上述规律转换

非字母字符保持原状不变.如“China!”转换为Glmre!”

输入一行字符,要求输出其相应的密码

•解题思路:问题的关键有两个:

(1)决定哪些字符不需要改变,哪些字符需要改变

(2)如果需要改变,应改为哪个字符

–处理的方法是: 输入一个字符给字符变量c

先判定它是否为字母(包括大小写),若不是字母

不改变c的值;若是字母,则还要检查它是否’W’

到’Z’的范围内(包括大小写字母)。如不在此范围内

则使变量c的值改变为其后第4个字母

如果在’W’到’Z’的范围内

则应将它转换为A~D(或a~d)之一的字母

其中,输入一个字符给字符变量c,

用c=getchar(); 语句实现。

先判定它是否字母(包括大小写),语句为

if((c>='a' && c<='z') || (c>='A' && c<='Z'))

检查字符是否在’W’到’Z’的范围内(包括大小写字母)

用下列语句计算转换后的字符

if(c>='W' && c<='Z' || c>='w' && c<='z')

c=c+4-26;

else c=c+4;

问题的第2个关键是:

(2) 怎样使c改变为所指定的字母?

–办法是改变它的ASCII值

–例如字符变量c的原值是大写字母’A’

想使c的值改变为’E’,只需执行“c=c+4”即可

因为’A’的ASCII值为65,而’E’ 的ASCII值为69

二者相差4

程序如下:

char c;

c=getchar(); //输入第一个字符

while(c!=‘ ’)

{ if((c>=‘a’ && c<=‘z’) || (c>=‘A’ && c<=‘Z’))

{ if(c>='W' && c<='Z' || c>='w' && c<='z')

c=c-22;

else c=c+4; }

printf("%c",c);

c=getchar(); //输入下一个字符}

输入 China!,则输出Glmre!

可以进一步改进程序。把前后两个读入

字符的c=getchar()合并为一个

并且放在while语句的检查条件中

char c;

while((c=getchar())!=‘ ’)

{ if((c>=‘A’ && c<=‘Z’) || (c>=‘a’ && c<=‘z’))

{ c=c+4;

if(c>‘Z’ && c<=‘Z’+4 || c>‘z’)

c=c-26; }

printf("%c",c); }

好了,同学们

循环结构程序举例2我们就学习到这儿

下节课再见!

C Programming课程列表:

Chapter 1 Introduction

-1.1 The development and characteristics of C language

--1.The development and characteristics of C language .mp4

--1.1 Self test questions

-1.2 A simple C Language program

--1.2 A simple C Language program.mp4

--Discussion unit

--【Source program】 Example 1.1 output a line of text Hello, world! "

--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers

--1.2 Self test questions

-1.3 Program, Programming Language and C Program Running Steps

--1.3 Program, Programming Language and C Program Running Steps.mp4

--Discussion unit

--Running steps of C source program in CodeBlocks

--1.3 Self test questions

-Course references

-chapter 1 Self-Test

Chapter 2 Algorithm

-2.1 The Concept and Description of Algorithms

--2.1 The Concept and Description of Algorithms.mp4

--Discussion unit

--2.1 Self test questions

-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming

--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4

--Discussion unit

--2.2 Self test questions

-Chapter 2 Self test questions

Chapter 3 Programming in C

-3.1 Simple Structure and Identifier of C Language Program

--3.1 Simple Structure and Identifier of C Language Program.mp4

--【Source program】 Example 3.1 input two integers and output the sum of two numbers.

--【Source program】 Example 3.2 input two integers and output the average value.

--【Source program】 Example 3.3 output the value of the character variable.

--3.1 Self test questions

-3.2 Constants, Variables and Assignments

--3.2 Constants, Variables and Assignments.mp4

--Discussion unit

--3.2 Self test questions

-3.3 Arithmetic, assignment, increment and decrement operators

--3.3 Arithmetic, assignment, increment and decrement operators.mp4

--3.3 Self test questions

-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--【Source program】 example of sizeof

--3.4 Self test questions

-3.5 Input and Output Examples and Character Input and Output

--3.5 Input and Output Examples and Character Input and Output.mp4

--【Source program】 Example. Find the root of quadratic equation of one variable.

--【Source program】 Example 1. Successively output three characters of BOY.

--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen

--3.5 Self test questions

-3.6 Formatted output printf function

--3.6 Formatted output printf function.mp4

--【Source program】 Example 3.4 output of integer data.

--【Source program】 Example 3.5 real data output.

--【Source program】 Example 3.6 character data output.

--【Source program】 Example 3.7 uses %s to output a string.

--3.6 Self test questions

-3.7 Formatted input scanf function

--3.7 Formatted input scanf function.mp4

--Discussion unit

--【Source program】 Example 3.8 Input and output integer data

--【Source program】Example 3.9 Input and output single precision and double precision real data.

--【Source program】Example 3.10 input and output character data.

--【Source program】Example 3.11 input and output string.

--3.7 Self test questions

-3.8 Basic Data Types of C Language

--3.8 Basic Data Types of C Language.mp4

--3.8 Self test questions

-Chapter 3 Self test questions

-Operator and expression self test questions

Chapter 4 Selection Structure

-4.1 Relational operators, logical operators, and if statements

--4.1 Relational operators, logical operators, and if statements.mp4

--Discussion unit

--【source example】Example 4.1 Find the root of the quadratic equation of one variable.

--【Source program】Example 4.2 Input two real numbers and output them from small to large.

--4.1 Self test questions

-4.2 Switch Statement

--4.2 Switch Statement .mp4

--Discussion unit

--【Source program】Example 4.3 uses switch statement to implement simple menu program

--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.

--4.2 Self test questions

-4.3 Examples of Selection Structural Programs

--4.3 Examples of Selection Structural Programs.mp4

--【Source program】 Example 4.5 determines whether a year is a leap year.

--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.

--【Source program】Example 4.7 calculate the transportation cost for the user.

--4.3 Self test questions

-Chapter 4 Self test questions

Chapter 5 Loop Structure

-5.1 While and Do-While Statement

--5.1 While and Do…while statement.mp4

--Discussion unit

--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100

--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100

--5.1 Self test questions

-5.2 For Statement

--5.2 for statement.mp4

--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100

--5.2 Self test questions

-5.3 Change the State of Loop Execution and Nested Loop

--5.3 Change the state of loop execution and nested loop .mp4

--【Source program】example 5.4 collecting charity donations

--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.

--【Source program】multiplication table

--【Source program】example 5.6 Output the following 4*5 matrix.

--5.3 Self test questions

-5.4 Loop Structure Program example 1

--5.4 Loop structure program example 1.mp4

--【Source program】example 1

--【Source program】example 2

--【Source program】example 3

--【Source program】 Example 4

--5.4 Self test questions

-5.5 Loop Structure Program example 2

--5.5 Loop structure program example 2.mp4

--【Source program】example 5. Output the following figure.

--【Source program】example6. The problem of 100 chickens and 100 coins

--【Source program】example 7. Find the approximate value of PI

--5.5 Self test questions

-Chapter 5 Self test questions

Chapter 6 Batch Data Processing with Array

-6.1 Definition, Reference and Initialization of One-Dimensional Arrays

--6.1 Definition, Reference of One-Dimensional Arrays.mp4

--Discussion unit

--6.1 Self test questions

-6.2 One-Dimensional Array Programming

--6.2 One-Dimensional Array Programming.mp4

--【Source program】example 1. Fibonacci sequence

--【Source program】example 2. Find the maximum value and the minimum value.

--【Source program】example 3 exchanging array elements in reverse order.

--【Source program】example 4. Bubble sorting.

--6.2 Self test questions

-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays

--6.3 Definition, Reference of Two-Dimensional Arrays.mp4

--6.3 Self test questions

-6.4 Two-Dimensional Array Programming

--6.4 Two-Dimensional Array Programming.mp4

--【Source program】example 1. Get the average score of each subject.

--【Source program】Example 2. The elements of row and column of a 2D array are interchanged

--6.4 Self test questions

-6.5 Definition, initialization and input and output of character arrays

--6.5 Definition, initialization and input and output of character arrays .mp4

--Discussion unit

--【Source program】Example 6.6 Output a known string.

--【Source program】Example 6.7 Output a diamond.

--【Source program】Example 6.10 Sorting of strings.

--6.5 Self test questions

-6.6 String Processing Function

--6.6 String Processing Function.mp4

--6.6 Self test questions

-6.7 Character Array Programming

--6.7 Character Array Programming.mp4

--6.7 Self test questions

-Chapter 6 Self test questions

Chapter 7 Modular Programming with Functions

-7.1 Function Concept and How to Define and Call Functions

--7.1 Function Concept and How to Define and Call Functions.mp4

--【Source program】Example 7.1

--7.1 Self test questions

-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value

--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4

--Discussion unit

--【Source program】Example 7.2

--7.2 Self test questions

-7.3 Declarations of called functions and nested calls to functions

--7.3 Declarations of called functions and nested calls to functions .mp4

--【Source program】Example 7.4 use a function to find the sum of t

--7.3 Self test questions

-7.4 Recursive call to function

--7.4 Recursive call to function.mp4

--【Source program】Example 7.6. How old is the fifth student?

--【Source program】Example 7.7 Use recursion method to find n!

--7.4 Self test questions

-7.5 Array as function parameter (1)

--7.5 Array as function parameter (1).mp4

--Discussion unit

--【Source program】Example 7.10

--7.5 Self test questions

-7.6 Array as function parameter (2)

--7.6 Array as function parameter (2).mp4

--【Source program】Selection sorting.

--【Source program】Example 7.13 find the maximum of a 3×4 matrix.

--7.6 Self test questions

-7.7 Local and global variables, internal and external functions

--7.7 Local and global variables, internal and external functions.mp4

--【Source program】Example 7.14

--7.7 Self test questions

-7.8 The Survival Period of Variables and the Storage Mode of Local Variables

--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4

--【Source program】Example 7.17

--7.8 Self test questions

-7.9 Storage Categories of Global Variables

--7.9 Storage Categories of Global Variables.mp4

--7.9 Self test questions

-Chapter 7 Self test questions

Chapter 8 Pointer

-8.1 Pointer Concept, Definition and Reference of Pointer Variables

--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

--【Source program】Example 8.1

--8.1 Self test questions

--Discussion unit

-8.2 Pointer variables as function parameters

--8.2 Pointer variables as function parameters.mp4

--Discussion unit

--【Source program】Example 8.3. Exchange two data with function call.

--8.2 Self test questions

-8.3 Pointer of array element, operation of pointer and reference of array element by pointer

--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4

--【Source program】Point to array elements with pointer variables.

--8.3 Self test questions

-8.4 Using Array Name as Function Parameter

--8.4 Using Array Name as Function Parameter.mp4

--【Source program】Store array elements in reverse order.

--8.4 Self test questions

-8.5 Reference to multidimensional arrays by pointers

--8.5 Reference to multidimensional arrays by pointers.mp4

--【Source program】Pointer variable pointing to one-dimensional array.

--8.5 Self test questions

-8.6 Referencing strings through pointers

--8.6 Referencing strings through pointers.mp4

--【Source program】Reference string by pointer.

--8.6 Self test questions

-8.7 Character pointer as function parameter

--8.7 Character pointer as function parameter.mp4

--【Source program】Character pointer as function parameter.

--8.7 Self test questions

-8.8 Pointer pointing to function

--8.8 Pointer pointing to function.mp4

--【Source program】Pointer variable pointing to the function.

--8.8 Self test questions

-8.9 Functions that return pointer values

--8.9 Functions that return pointer values.mp4

--【Source program】Intercept substring.

--8.9 Self test questions

-8.10 Pointer arrays and multiple pointers

--8.10 Pointer arrays and multiple pointers.mp4

--【Source program】Sorting of strings.

--8.10 Self test questions

-8.11 Dynamic memory allocation and pointer variables pointing to it

--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4

--Discussion unit

--【Source program】Dynamic memory allocation.

--8.11 Self test questions

-Chapter 8 Self test questions

Chapter 9 Structure

-9.1 Define and use structural variables

--9.1 Define and use structural variables.mp4

--【Source program】Structure variable

--9.1 Self test questions

-9.2 Using structure arrays

--9.2 Using structure arrays.mp4

--Discussion unit

--【Source program】Structure array

--9.2 Self test questions

-9.3 Structure pointer

--9.3 Structure pointer.mp4

--【Source program】Structure pointer

--9.3 Self test questions

-Chapter 9 Self test questions

CodeBlocks Baidu online disk download

-CodeBlocks Baidu online disk download address

Final Exam

-Final Exam

--final exam

5.5 Loop structure program example 2.mp4笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。