Classic C procedure 100 cases (10-20)
Topic: Classical question: There is a bunny, from the third month after birth, a bunny every month, the bunny is born again to the rabbit after the third month, if the rabbit is not dead, ask What is the total number of rabbits every month?
Program analysis: Rabbit's laws are numbered 1, 1, 2, 3, 5, 8, 13, 21 ....
Source code: main () {long f1, f2; int i; f1 = f2 = 1; for (i = 1; i <= 20; i ) {Printf ("% 12LD% 12LD", F1, F2); IF (i% 2 == 0) Printf ("/ n"); / * Control output, four * / f1 = f1 f2 each line; / * added to the third month * / F2 = f1 f2; / * Add up two months to the third month * /}}
Title: How many of the numbers between 101-200, and output all the numbers.
Program analysis: Method for judging the number of prime: Use a number to remove 2 to SQRT (this), if it can be removed, the number is not the number of ps.
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);}
Topic: Print all "Narcies", the so-called "number of daffodils" refers to a three-digit number, and its digital cubes and is equal to the number itself. For example: 153 is a "number of daffodils" because 153 = 1 triple square 5 three times.
Program analysis: Using the FOR cycle to control 100-999 numbers, each number is broken down, ten, one hundred.
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");
Title: Decompose a positive integer. 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. 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);}
Title: Use the nested of the conditional operator to complete this question: Academic performance> = 90 students use a to represent A, 60-89, expressed by B, 60, below, is expressed.
Program analysis: (a> b)? A: b This is the basic example of the conditional operator.
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);}
Title: Enter two positive integers M and N, ask its maximum number of conventions and minimum common multiple.
Program analysis: Using the rolling method.
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) / * Using Rolling Method until B is 0 * / {TEMP = a% B; A = B; B = Temp;} Printf ("Gongyueshu:% D / N", A); Printf ("gongbeishu:% d / n", Num1 * Num2 / a);}
Topic: Enter a line of characters, statistics, respectively, in English letters, spaces, numbers, and other characters.
Program analysis: Use the While statement, the condition is not the input character is not '/ n'. 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 (" Allin All: Char =% D Space =% D DIGIT =% D Others =% D / N ", Letters, Space, Digit, Others;} Title: S = a aa aaa aaaa aa .. .a's value, where A is a number. For example, 2 22 222 2222 22222 (there are 5 numbers at this time), and several numbers are added to the keyboard control.
Program analysis: The key is to calculate the value of each item.
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);
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.
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 = Si; k [n] = i;}} f (s == 0) {printf ("% d is a wanshu", J ); For (i = 0; I printf ("% d,", k [i]); Printf ("% d / n", k [n]);}}}
Title: One goal is free from 100 meters high, and the height of the anti-jump back will be half a half; if it falls, how many meters have been elapsed in the 10th floor? How high is the 10th rebound?
Program analysis: See the comments below