Classic C language procedure 100 cases (21-30) ----------------- Transfer from C Language Classic Forum

xiaoxiao2021-03-06  46

[Procedure 21] Topic: Monkey eats peach problem: The monkey took a few peaches on the first day, and immediately took half, it was not addictive, and I ate a few days in the morning and I took half of the peach. I ate one more. Every morning, I ate half of the previous day. When I wanted to eat again on the 10th, I saw only one peach. How much is it taken on the first day. 1. Program analysis: Take the method of reverse thinking, inference from the past. 2. Program source code: main () {Int day, x1, x2; day = 9; x2 = 1; while (day> 0) {x1 = (x2 1) * 2; / * The number of peaches on the first day It is the 2nd day of the peach number plus 1 after adding 1 * / x2 = x1; day--;} Printf ("Total IS% D / N", X1);} =========== ============================================================================================================================================================================================================= = [Program 22] Title: Two table tennis teams have played three people. The broth is A, B, C, and the team is X, Y, Z. The list of matches have been drawn. Some people inquire to play the list of games. a said that he doesn't be with x, c says that he does not share the X, Z ratio, please compile the program to find the list of three teams. 1. Program analysis: Method for judging the number of prime: Use a number to remove 2 to SQRT (this), if it can be tightened, indicate that this is not a prime number, but it is the number of prime numbers.

2. Program source code: main () {char i, j, k; / * i is a opponent, J is the opponent of B, k is the opponent of C * / for (i = 'x'; i <= ' Z '; i ) for (j =' x '; j <=' z '; j ) {IF (i! = j) for (k =' x '; k <=' z '; k ) {IF ( i! = k && j! = k) {if (i! = 'x' && k! = 'x' && k! = 'z') Printf ("ORDER IS A -% C / TB -% C / TC-- % C / N ", I, J, K);}}}} ================================= ============================= 【Program 23】 Title: Print the following pattern (diamond) ******* ******************** 1. Program analysis: First divide the graphics into two parts, one rule in the first four lines, the last three lines, using a double For cycle , The first layer control line, the second layer control column.

2. Program source code: main () {INT I, J, K; for (i = 0; i <= 3; i ) {for (j = 0; j <= 2-i; j ) printf ("" ); For (k = 0; k <= 2 * i; k ) Printf ("*"); printf ("/ n");} for (i = 0; i <= 2; i ) {for (J = 0; j <= i; j ) Printf (""); for (k = 0; k <= 4-2 * i; k ) printf ("*"); printf ("/ n");}} ============================================================================================================================================================================================================= ============ 【Program 24】 Topic: One Piece Sequence: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13 ... The sum of the first 20 items of this number. 1. Program analysis: Please seize the changes of molecular and denominator. 2. Program source code: main () {int N, t, number = 20; float a = 2, b = 1, s = 0; for (n = 1; n <= Number; n ) {s = s A / B; t = a; a = a b; b = t; / * This part is the key to the program, please reader guess T role * /} printf ("Sum IS% 9.6F / N", S );} =============================================== =============== 【程序 2 25】 Title: See Qi 1 2! 3! ... 20! And 1. Program Analysis: This procedure is just the accumulation Towerful.

2. Program source code: main () {float n, s = 0, t = 1; for (n = 1; n <= 20; n ) {t * = n; s = t;} printf ("1 2! 3! ... 20! =% E / N ", S);} ============================ ================================== 【程序 2 26】 Title: Use the recursive method to find 5 !. 1. Program analysis: recursive formula: fn = fn_1 * 4! 2. Source code: #include "stdio.h" main () {INT i; int fact (); for (i = 0; i <5; i PRINTF ("/ 40:% D! =% D / N", I, FACT (I));} INT FACT (J) INT J; {INT SUM; IF (J == 0) SUM = 1; ELSE SUM = J * FACT (J-1); return sum;} ===================================== =========================== 【程序 27] Title: Use the recursive function call mode, the 5 characters entered in the opposite order print it out.

1. Program analysis: 2. Source code: #include "stdio.h" main () {INT i = 5; Void palin (int N); Printf ("/ 40:"); PALIN (I); Printf "/ n");} void palin (n) int N; {char next; if (n <= 1) {next = getchar (); printf ("/ n / 0:"); PUTCHAR (NEXT);} Else {Next = getchar (); PALIN (N-1); Putchar (next);}} ============================= ================================== 【程序 2 28】 Title: There are 5 people sitting together, ask the fifth How old is your personal? He said that it is 2 years older than the 4th. Ask the fourth person, he said that it is 2 years older than the third person. Ask the third person, it is two years older than the second person. Ask the second person, saying that the first person is two years old. Finally, ask the first person, he said it was 10 years old. How old is the fifth person? 1. Program analysis: Use the recursive method, recursion is divided into two phases of rebuilding and recursive. If you want to know the fifth person, you need to know the number of years of the fourth person, push it to the first person (10 years old), and then push it back. 2. Program source code: age (n) int N; {INT C; if (n == 1) c = 10; ELSE C = Age (N-1) 2; Return (C);} main () { Printf ("% D", AGE (5));} ===================================== ========================== 【程序 2 29】 Title: give a positive intent of 5 digits, require: First, ask it is Several digits, second, reverse sequence print out numbers.

转载请注明原文地址:https://www.9cbs.com/read-79930.html

New Post(0)