当前课程知识点:C Programming > Chapter 5 Loop Structure > 5.4 Loop Structure Program example 1 > 5.4 Loop structure program example 1.mp4
大家好,我是云南大学信息学院丁海燕老师
欢迎走进C语言程序设计课堂
今天我们讲解循环结构程序举例
下面来看例一
按每行输出5个数的形式输出
斐波那契数列Fibonacci数列的前20项
思路:斐波那契数列的前几项是
1、1、2、3、5、8、13、21、34、…
此数列的变化规律是
fn=1
n=1的时候
n=2的时候fn=1
n>2的时候 fn=fn-1 + fn-2
这是一种递推算法
应采用循环实现
设变量f1、f2和f3,并为f1和f2赋初值1
令f3=f1+f2得到第3项;
将f1←f2, f2←f3
再求f3=f1+f2得到第4项
依此类推求第5项、第6项…
算法和程序如下:
#define N 20
main( )
{ int i,f1,f2,f3;
f1=f2=1;
printf(" %8d%8d",f1,f2);
for (i=3; i<=N; i++)
{ f3=f1+f2;
f1=f2;
f2=f3;
printf("%8d",f3);
if (i%5==0) printf(" ");} }
Fibonacci数列的前20项如图所示
下面来看例二
判断输入的某个数m是否为素数
若是素数,输出“YES”,若不是,输出“NO”。
思路:素数是指只能被1和它本身整除的数
如5、7、11、17、…等。
分别用2、3、…,m-1尝试能否整除整数m
如果m能被某个数整除,则m就不是素数
这是一种穷举算法
设除数为j,从2循环到m-1
算法和程序如下:
#include "math.h"
main( )
{ int j,n;
scanf("%d",&n); 输入n
for (j=2; j<=n-1; j++) //表示除数j的取值从2到n-1
if (n%j==0) break; /*
如果n能被某个数整除,则n就不是素数
退出循环。否则j增加1,判断下一个除数j能否整除n. */
printf("%d ",m);
if (j==n)
printf("YES ");
对于穷举法来说,为了提高程序的效率
就要减少尝试次数。
程序改进:其实n不必被2~(n-1)范围内的各整数去除
只须将n被2~√n之间的整数除即可
例如,判断17是否是素数,只须将17被2,3,4除即可
如都除不尽,n必为素数,这样做可大大减少循环次数
提高执行效率
为了方便,可以定义一个整型变量
k(其值为√n的整数部分),如果n不能被2~√n之间的
整数整除,则在完成最后一次循环后,i还要加1
因此i=k+1,然后才终止循环,在循环之后
判别i的值是否大干或等于k+1
若是,则表明n未曾被2~k之间的任一
整数整除过,因此输出“素数”。
程序如下:
{ int j,n,k;
scanf("%d",&n); //输入n的值
k=sqrt(n);
求n的根号
for (j=2; j<=k; j++) //用2到k的数去除n
if (n%j==0) break; //若发生j可以整除n,则不是素数
printf("%d ",n);
if (j>=k+1) printf("YES ");
else printf("NO ");
程序中 k=sqrt(n); k的值为√n的整数部分
if (n%j==0) break; 表示如果n能被
2~√n之间的整数整除,则n不是素数直接退出循环
程序运行结果如图所示,输入7,输出YES。
思考:如何输出100~200中所有的素数
在上题基础上,只要增加一个外循环
先后对100~200间的全部素数一一进行判定即可
也就是用一个嵌套的for循环即可处理,程序如下:
for(i=100;i<=200;i++)
{ k=sqrt(i);
for (j=2; j<=k; j++)
if (i%j==0) break;
if (j==k+1)
printf("%d ",i); }
printf(" ");}
下面来看例3
用牛顿迭代法求方程 2x三次方+4x方-7x-6=0
在x=1.5附近的根。
思路:设xn为一个接近xa的近似根
过(xn, f(xn)) 点做切线,切线方程为:
f'(Xn)=f(Xn)/Xn-Xn+1
X n+1 = X n - f(Xn) / f'(Xn)
这个公式称为牛顿迭代公式
算法基本步骤如下
1、先设一个方程近似根x0
求出方程f的值和方程导数f1的值;
f=2x0三次方+4x0平方-7x0-6
f1=6x0平方+8x0-7
2用迭代公式x=x0-f/f1进行迭代
求出比x0更接近方程根的x;
3、当|x-x0|大于某个很小的数时(如10-6)
认为未找到,此时将x→x0,再次求f、f1,并迭代
又求出一个新的更接近方程根的x;
4 、一直到 |x-x0|≤10的负六次时得到方程近似根:x或x0。
这是一种迭代算法
用循环实现
算法如下:
x赋初值
x0=x
计算f
计算f1
计算x=x0-f/f1
当 |x-x0|>10的负六次时,重复执行以上4个步骤
输出x
程序如下:
main( )
{ float x,x0,f,f1;
x=1.5;
do { x0=x;
f=2*x0*x0*x0+4*x0*x0-7*x0-6; //计算f的值
f1=6*x0*x0+8*x0-7; //计算f1的值
x=x0-f/f1; //计算x
} while(fabs(x-x0)>1e-6);
//若|x-x0|≤10的负六次方,重复计算更接近方程根的x
printf("%f ",x); //输出x}
当精度为1.5时,程序运行结果为1.539441
下面来看例四【例4】编程序求2~10000以内的完全数
完全数:一个数的因子
(除了这个数本身)之和等于该数本身
例如:6的因子是1、2、3,因子和 1+2+3=6
因此 6 是完全数
思路:
设定i从2变到10000,对每个i找到其因子和s;
判定 i=s?若相等,则i为完全数,否则不是
使用穷举算法
用双层循环实现
算法如图所示:
外循环i从2变到10000,对每个i找到其因子和s
内循环j从1变到i-1,判断i除以j余数是否为0
是,则说明j是i的因子,用s=s+j累加因子和
如果余数不为0,则执行j++,取下一个除数
再次执行内循环
内循环结束后,接着判断i==s是否相等
若相等则i为完全数,否则i不是完全数
程序如下:
{ int i,j,s;
for (i=2; i<=10000; i++)
{ s=0;
for (j=1; j
if (i%j==0)
s+=j;
if (i==s)
printf("%6d ",s);}}
程序运行结果如图所示
2~10000以内的完全数有6,28,496和8128
好了,同学们
循环结构程序举例我们就学习到这儿
下节课再见!
-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
--【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
--Running steps of C source program in CodeBlocks
--1.3 Self test questions
-chapter 1 Self-Test
-2.1 The Concept and Description of Algorithms
--2.1 The Concept and Description of Algorithms.mp4
--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
--2.2 Self test questions
-Chapter 2 Self test questions
-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
--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
--【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
-4.1 Relational operators, logical operators, and if statements
--4.1 Relational operators, logical operators, and if statements.mp4
--【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
--【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
-5.1 While and Do-While Statement
--5.1 While and Do…while statement.mp4
--【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
--【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
--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
-6.1 Definition, Reference and Initialization of One-Dimensional Arrays
--6.1 Definition, Reference of One-Dimensional Arrays.mp4
--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
--【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
-7.1 Function Concept and How to Define and Call Functions
--7.1 Function Concept and How to Define and Call Functions.mp4
--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
--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
--【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
-8.1 Pointer Concept, Definition and Reference of Pointer Variables
--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4
--8.1 Self test questions
-8.2 Pointer variables as function parameters
--8.2 Pointer variables as function parameters.mp4
--【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
--【Source program】Dynamic memory allocation.
--8.11 Self test questions
-Chapter 8 Self test questions
-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
--【Source program】Structure array
--9.2 Self test questions
-9.3 Structure pointer
--【Source program】Structure pointer
--9.3 Self test questions
-Chapter 9 Self test questions
-CodeBlocks Baidu online disk download address
-Final Exam
--final exam