Classic C procedure 100 case == 11--20

xiaoxiao2021-03-06  46

[Program 11] Topic: Classical question: There is a pair of rabbits, from the third month after birth, a bunny every month, the bunny is born again to the third month after each month, if the rabbit Don't die, how much is the total number of rabbits every month? 1. Program analysis: Rabbit's laws are numbered 1, 1, 2, 3, 5, 8, 13, 21 .... 2. Data source code: main () {long f1, f2; int in; f1 = f2 = 1; for (i = 1; i <= 20; i ) {Printf ("% 12LD% 12LD", F1, F2); if (i% 2 == 0) Printf ("/ n"); / * Control output, each line four * / f1 = f1 f2; / * added to the third month * / f2 = f1 f2; / * added to the third two months to assign a value to the third Month * /}} ============================================== ================= 【Program 12】 Title: How many of the numbers between 101-200, and output all the numbers. 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: #include "math.h" main () {INT M, I, K, H = 0, Leap = 1; Printf ("/ n"); for (m = 101; m <= 200 ; M ) {k = SQRT (M ​​ 1); for (i = 2; i <= k; i ) IF (m% i == 0) {leap = 0; Break;} if (leap) {printf "% -4d", m); h ; if (h% 10 == 0) Printf ("/ n");} leap = 1;} printf ("/ nthe total IS% D", H);} = ============================================================================================================================================================================================================= =========== 【Program 13】 Title: Print all "Number of daffodils", the so-called "Number of Narcies" refers to a three-digit number, and its digital cubes and equal mids the number itself.

For example: 153 is a "number of daffodils" because 153 = 1 triple square 5 three times. 1. Program analysis: Use the for loop to control 100-999 numbers, each number is broken down, ten, one hundred. 2. Program source code: main () {INT I, J, K, N; Printf ("'Water Flower'Number IS:"); for (n = 100; n <1000; n ) {i = N / 100 ; / * Decompose hundreds of * / j = n / 10% 10; / * Decompose ten * / k = n% 10; / * Decompose a bit * / if (i * 100 j * 10 k == i * i * i j * j * j k * k * k) {printf ("% - 5d", n);}}}} printf ("/ n");} ======= ============================================================================================================================================================================================================= ===== [Program 14] Title: A positive integer is decomposed. For example: input 90, print 90 = 2 * 3 * 3 * 5. Program analysis: Differential factor of N, first find a minimum rigid number K, then complete: (1) If this number is just equal to N, the process of dividing factor is over, printing out . (2) If n <> k, but N can be removed by K, the value of K should be printed, and the use of N is divided by K's business as a new positive integer you n, repeatedly performing the first step. (3) If N cannot be divided by K, use K 1 as a value of K, repeat the first step.

2. Program source code: / * zheng int is divid Yinshu * / main () {Int n, i; printf ("/ nplease input a number: / n"); scanf ("% d", & n); printf "% d =", N); for (i = 2; i <= n; i ) {while (n! = i) {if (n% i == 0) {printf ("% d *", i ); N = n / i;} else break;}} printf ("% d", n);} ========================== ============================================== 【Procedure 15】 Title: Nested the conditional operator To do this: Academic performance> = 90 students use a to represent, 60-89 points, b, 60, clicker, and c. 1. Program analysis: (a> b)? A: b This is the basic example of the conditional operator. 2. Program source code: main () {int score; char grade; printf ("please input a score / n"); scanf ("% d", & score); grade = score> = 90? 'A' :( Score> = 60? 'b': 'c'); Printf ("% D Belongs TO% C", score, grade);} =================== =================================================== 【program 16】 Title: Enter Two positive integers m and n, seeking their maximum number of conventions and the least common multiple. 1. Program analysis: Using the rolling method. 2. Program source code: main () {Int A, B, Num1, Num2, Temp; Printf ("please input two number: / n"); scanf ("% D,% D", & Num1, & num2); if (Num1

{TEMP = NUM1;

Num1 = NUM2;

Num2 = TEMP;

}

A = Num1; b = Num2;

While (B! = 0) / * Usage with rolling, until B is 0 * /

{

TEMP = a% B;

A = B;

B = TEMP;

}

Printf ("Gongyueshu:% D / N", A); Printf ("Gongbeishu:% D / N", Num1 * Num2 / A);

}

============================================================================================================================================================================================================= ============

[Program 17]

Topic: Enter a line of characters, statistics, respectively, in English letters, spaces, numbers, and other characters.

1. Program analysis: Use the While statement, the condition is not the input character is not '/ n'.

2. Program source code:

#include "stdio.h"

Main ()

{char C;

INT letters = 0, Space = 0, DIGIT = 0, OTHERS = 0;

Printf ("Please Input Some Characters / N);

While (c = getchar ())! = '/ n')

{

IF (c> = 'a' && c <= 'z' || c> = 'a' && c <= 'z')

Letters ;

ELSE IF (c == '')

Space ;

ELSE IF (C> = '0' && c <= '9')

Digit ;

Else

Others ;

}

Printf ("All in all: char =% d Space =% D Digit =% D =% d / n", letters,

Space, Digit, Others;

}

============================================================================================================================================================================================================= ============

[Program 18]

Title: See the value of s = a aa aaa aaaa aa ... a, where A is a number. For example, 2 22 222 2222 22222 (at this time

There are 5 digits plus a total of 5 numbers, and several digitally controlled.

1. Program analysis: The key is to calculate the value of each item.

2. Program source code:

Main ()

{

INT A, N, Count = 1;

Long Int Sn = 0, TN = 0;

Printf ("Please Input a and N / N");

Scanf ("% D,% D", & A, & n); Printf ("A =% D, N =% D / N", A, N);

While (count <= n)

{

TN = Tn a;

SN = SN TN;

A = a * 10;

COUNT;

}

Printf ("A AA ... =% LD / N", SN);

}

============================================================================================================================================================================================================= ============

[Program 19]

Title: A number is almost equal to its factor, this number is called "full". For example 6 = 1 2 3. Programming

Find all the completed within 1000.

1. Program analysis: Please refer to the program <- Previous program 14.

2. Program source code:

Main ()

{

Static Int K [10];

INT I, J, N, S;

For (j = 2; j <1000; j )

{

N = -1;

s = j;

For (i = 1; i

{

IF ((j% i) == 0)

{N ;

S = S-I;

K [n] = i;

}

}

IF (s == 0)

{

Printf ("% d is a wanshu", j);

For (i = 0; i

Printf ("% d,", k [i]);

Printf ("% d / n", k [n]);

}

}

}

============================================================================================================================================================================================================= ============

[Program 20]

Title: One goal is free from 100 meters high, and each time it falls back to the original height half; then fall, ask it

How many meters have been elapsed in the 10th floor? How high is the 10th rebound?

1. Program analysis: See the comments below

2. Program source code:

Main ()

{

Float Sn = 100.0, HN = SN / 2;

Int n;

For (n = 2; n <= 10; n )

{

Sn = SN 2 * HN; / * Number of meters in the first place * /

HN = hn / 2; / * Nth anti-jump height * /

}

Printf ("Total of ROAD IS% F / N", SN);

Printf ("Tenth IS% F meter / n", hn);

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

New Post(0)