In programming, we often encounter the situation that we need to use the same rule to process repeatedly, which is the cycle problem. Turbo Pascal can be implemented in different loop ways, and there are three commonly used loops: for, repeat and while.
The first section for loop
The for loop is an automatic counting loop.
[Example 3. 1] Try to print out the natural number of 1~20.
Solution: ① each number of 1~20 is represented by a, and a is also used to count the number of control cycles;
② Let A start from 1;
③ output a;
④ Automatic counting (plus 1), if it does not exceed the specified cycle range, repeat step ③, otherwise the cycle ends.
Pascal program:
Program exam12;
Var a: bytes;
begin
For a:= 1 to 20 do
writeln(a);
Readln
End.
Write TELN (a) for a: = 1 to 20 in the program; This is a for loop statement.
The for loop statement has two formats:
(1) for loop variable: = initial value to final value do statement;
(2) For loop variables: = initial value downto final value do statement;
The initial value of the (1) format is less than or equal to the final value, and the value of the loop variable changes incrementally according to the automatic addition of 1;
(2) The initial value of the format is greater than or equal to the final value, and the value of the loop variable changes gradually by automatically subtracting 1. The for loop is a counting loop (increase 1 or decrease 1).
For example, if the program [Example 3. 1] is changed to a countdown (decrement) loop, a natural number of 20 ~ 1 will be output:
Program exam 31;
Var a: bytes;
begin
For a:=20 downto 1 do.
writeln(a);
Readln
End.
[Example 3.2] Print out even numbers from 30 to 60. ]
Solution:
Method 1:
= 1 \* GB3 ① let a represent all numbers from 30 to 60, which can be listed by the for loop;
= 2 \* GB3 ② Use the formula a mod 2=0 to screen out the even output.
Pascal program:
Program ex32
Var a: integer;
begin
For a do: = 30 to 60
If (a mod 2=0) then writeln (a);
Readln
End.
In this program, the loop statement after the for loop is a conditional branch statement.
Method 2: We know that in the formula a=2*n, if n takes the natural number 1, 2, 3, …, then A gets the even numbers 2, 4, 6, … in turn. So, if you want to get an even number from 30 to 60, just let n in the above formula take a natural number from 15 to 30. So this problem can also be handled according to the following steps:
= 1 \* GB3 ① Let n represent all natural numbers from 15 to 30, which can be listed by the for loop;
= 2 \* GB3 ② Use formula a := 2*n to find the even number;
= 3 \* GB3 ③ Output the result to the screen.
Pascal program:
Program ex32
begin
For n := 15 to 30 do.
begin
a:= 2 * n;
writeln(a);
End;
Readln
End.
[Example 3.3] Sum of natural numbers: Write a program to sum the natural numbers from 1 to 100.
Solution: ① Let s = 0;;
(2) Let A represent a natural number between 1 and 100 and list it circularly;
③ Use the formula S:=S+a to accumulate these natural numbers into S one by one;
= 4 \* GB3 ④ After the loop ends, S is the sum of natural numbers from 1 to 100, and can be output.
Pascal program:
Program ex33
Var s, a: integer;
begin
s:= 0;
For a := 1 to 100 do
S:= S+a;
Writeln('S= ',S);
Readln
End.
[Example 3.4] A two-digit number X is exchanged with a ten-digit number to get a new number Y. At this time, Y is just 36 larger than X. Please find out all these two digits by programming.
Solution: ① List all two digits with a for loop, where x is a cyclic variable;
② Use formula a:= x div 10 to separate the ten digits of x;
③ Use formula b:= x mod 10 to separate the unit digits of X;
④ use the formula y:= b* 10+a to synthesize a new number y;
= 5 \* GB3 ⑤ Use the formula y-x=36 to filter out the qualified number X and output it.
Pascal program:
Program ex34
begin
For x := 10 to 99 do.
begin
a:= x div 10;
b:= x mod 10;
y:= b * 10+a;
If y-x=36, then writeln (x);
End;
Readln
End.
[Example 3.5] Cut the integer 3025 into two numbers, 30 and 25, and then square the sum of these two numbers. The calculation result is (30+25)2=3025 equals the original number. Find all four numbers that meet this condition.
Solution: Let the qualified four-digit number be n, which should be a complete square number, represented by (a*a).
(1) In order to ensure that N=(a*a) is in the range of four digits (1000 ~ nine thousand nine hundred and ninety-nine), it can be determined that A is in the cycle of 32 ~ 99;
(2) calculate n = a * a and split the four-digit n into two numbers n 1 and N2;
(3) If the condition (N 1+N2) * (N 1+N2) = n is satisfied, then n is output.
Pascal program:
Program test 35;
Var N, a, x, n 1, n2: integer;
begin
For a:=32 to 99 do
begin
n:= a * a;
N 1:= N div 100; {Take the first two digits of four digits}
N2:= N-N 1 * 100; {Take the last two digits of four digits}
x:= n 1+N2;
If x*x=N, then writeln (n);
End;
Readln
End.
[Example 3.6] The rectangular pattern below is printed with "*".
*********
*********
*********
*********
Solution: ① The legend * * * given above has 4 lines, and we can use a loop to control the changes of lines;
(2) There are 9 columns in each row. We can set another cycle in the previous cycle to control the changes of columns.
Pascal program:
Program ex36
begin
For a := 1 to 4 do {change the outer loop control line}
begin
For b := 1 to 9 do {change inner loop control column}
Write ('*');
Writeln{ output lines with "*" and then wrap}
End;
Readln
End.
Every value of the loop pair A in the program contains an inner loop with a degree of b = (1 ~ 9), and the outer loop of A contains an inner loop of B, which is called nesting for loops. The nesting form is as follows:
For a:=n 1 to n2 do.
For b:=m 1 to m2, make a loop statement;
[Example 3.7] Print out the multiplication table of 99:
Solution: Let A be the multiplicand with the range of1~ 9; B is a multiplier with the range of1~ a; The multiplication formula is a * b = (product of a and b), then
a = 1:b = 1 ~ a 1 * 1 = 1
a = 2:b = 1 ~ a 2 * 1 = 2 2 * 2 = 4
a = 3:b = 1 ~ a 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
a = 4:b = 1 ~ a 4 * 1 = 4 4 * 2 = 8 4 * 3 = 13 4 * 4 = 16
: :
a = 9 b = 1 ~ a 9 * 1 = 9 9 * 2 = 18…9 * 9 = 8 1
(1) As can be seen from the above decomposition, * * has 9 lines, and the number of "lines" here changes from 1 to 9 the same as that of A, so A can be used to control the cycle of "lines";
⑵ The number of times of multiplication in each "row" is related to the range of B, and B controls the "inner" cycle in each "row";
(3) The inner ring is contained in the innermost layer. After executing the inner loop of each line, the next line will execute the loop in the new line, and each line has the same (b = 1 ~ a) inner loop.
In other words, every "row" must execute the inner loop of the "row". The "line" referred to here can be understood as an abstract line, not necessarily a specific corresponding line, but a processing "block".
Pascal program:
Program test 37;
Variables a, b: bytes;
begin
For a:= 1 to 9 do {outer loop}
begin
For b:= 1 to a do {inner loop}
Write (a,' *', b,' =', a*b, ":3);
writing
End;
Readln
End.
According to this format, you can also realize multi-layer circular nesting, for example:
For a:=n 1 to n2 do.
For b:=m 1 to m2 do.
For c:=k 1 to k2, make a loop statement;
[Example 3.8] How many combinations are there to choose any three cards from seven playing cards? Please program and output all combinations.
Solution: We take out three sheets at a time, namely A, B, C, B and C, and take values from the range of 1 ~ 7 through triple circulation; To eliminate duplicate numbers, please use (a-b) * (b-c) * (a-c).
Pascal program:
Program test 38;
const n = 7;
Var a, b, c, t: integer;
begin
t:= 0;
For a:= 1 to n do
For b:= 1 to n do
For c:= 1 to n do
if(a-b)*(b-c)*(a-c)& lt; & gt then 0
begin
Inc(t);
Written (A: 3, B: 3, C: 3)
End;
Writeln (total:, t: 5);
readln
End.
Mathematically, a natural number which is not divisible by other numbers except 1 is called a prime number. Now use the keyboard to input a natural number n, and program to judge whether n is a prime number. If so, output "Yes", otherwise output "No".
Solution: According to the definition, for a given natural number n, it is only necessary to judge whether there is a third natural number besides 1 and itself.
① Cycle k from 1 to n;
② According to whether N mod K is 0, the divisor of k can be counted;
③ If the divisor of n exceeds 2, it is determined that n is not a prime number.
Pascal program:
Program test 39;
Var n, m, k, t: integer;
begin
Write ('n =');
ReadLn(N);
t:= 0;
For k:= 1 to N do {outer loop}
If N mod k=0, then t: = t+1; {If n is odd}
If t>2 then writes "No"
Else writeln ("Yes");
Readln
End.
The variable yse in the program belongs to Boolean type. There are only two Boolean values:
True (true) false (false)
Boolean values have the same function as whether the conditional judgment result is true (the condition is true) or false (the condition is not true), and are often used in conditional statements and loop statements.
If yes (t mod 7 = 0), thenwriteln is used in the above program to print 7 prime numbers per line, and the Boolean variable Yes in the program is true and logically expressed as prime numbers; When the value of relation (t mod 7=0) is true, it means that the output prime number of this line is already 7; Connecting these two "conditions" with and is a Boolean operation.
Pascal *** has four logical operators:
(1) and (and) are both true, and the result value is true; Otherwise it is false; ;
② As long as one of the two conditions of OR (or) is true; The result value is true; ; Otherwise it is false; ;
③ When the logical values of XOR and XOR are different, the result value is true; ; Otherwise it is false; ;
④ When the NOT condition is true, the result value is false; ; Otherwise it is true; ; (reverse)
Exercise 3. 1:
1. Print out the square table of 1 to 20.
2. Print out odd numbers between 100 and 200.
3. The chicken and the rabbit are in the same cage (completed by the for loop program)
The express train and the local train go to the same place. Express fare 18 yuan, slow fare 13. 5 yuan, * * * sold 400 tickets, which is 5940 yuan. How many tickets do you want for the express train and the local train? .
5. Find the sum of four digits divisible by 5.
6. Fill in two figures in the following formula to make the equation hold.
□3*6528=3□*8256
7. There is a three-digit number, and the sum of its digits is 1 1 times, which is exactly equal to itself. Please program and find this three-digit number.
8. In natural numbers, if a three-digit number is equal to the cubic sum of its own digits, it is called daffodil number. For example: 153= 13+53+33, then 153 is a narcissus. Find all the daffodils.
9. The program prints the following patterns:
Parallelogram isosceles three-solution rhombus
****** * *
****** *** ***
****** ***** *****
****** ******* ***
****** ********* *
10. Program and print the following patterns:
1
222
33333
4444444
555555555
1 1. There are three kinds of postcards: one for each set, and the price is 2 yuan; The second one is for each set, and the price is 4 yuan; The third type is 9 pieces per set, and the price is 2 yuan. At present, 100 yuan is used to buy 100 postcards, and each postcard must have at least one set. How many sets of these three postcards should I buy? Please output all purchase plans.
12. If someone wants to change one yuan into five cents, two cents and one penny, how many exchange schemes are there under the condition that there is at least one of each of the three kinds of change? Make these plans.
13.
14. Output all prime numbers within 100, with 5 displayed on each line.
15. The sum, difference, product and quotient of two natural numbers A and B add up to 243. Find the numbers a and b ..
16. Buy 100 chickens for 100 yuan: I have 100 yuan now and want to buy 100 chickens, a rooster 3 yuan, a hen 1 yuan and three chickens 1 yuan. If you want to buy at least 1 rooster, hen and chicken, please program and find that you have just used up 65438.
If the answer is adopted, the experience value and wealth value can be improved simultaneously.