1. n! Recursive Algorithm
# Include <stdio. h> int find (int n) {If (n = 1) {return 1 ;}else {return find (n-1) * n ;}} int main (void) {int N; printf ("Enter n! N value in: "); scanf (" % d ", & N); printf (" % d \ n ", find (n); Return 0 ;}
2. Fibonacci Series 1
# Include <stdio. h> int F (int n) {If (n <1) {return 0;} else if (n = 1 | n = 2) {return 1 ;} else {return F (n-1) + f (n-2) ;}int main (void) {int N, I; printf ("Enter the number of Fibonacci Series N :"); scanf ("% d", & N); for (I = 1; I <= N; I ++) printf ("% d", f (I )); printf ("\ n"); Return 0 ;}
3. Fibonacci Series 2
# Include <stdio. h> int N; int pre = 1; int after = 1; int result = 0;/* minimum time complexity */INT main (void) {int N, I; printf ("Enter the number of Fibonacci Series N:"); scanf ("% d", & N); n = N; printf ("1 1 "); for (I = 2; I <n; I ++) {result = pre + after; Pre = after; after = result; printf ("% d", result );} printf ("\ n"); Return 0 ;}
4. Fibonacci Series 3
# Include <stdio. h> int N; int pre = 1; int after = 1; int result = 0;/* recursive control loop Fibonacci series */int f (int n) {If (n> 2) {f (n-1); Result = pre + after; Pre = after; after = result; printf ("% d", result );} else {printf ("1 1");} return result;} int main (void) {int N; printf ("Enter the number of Fibonacci Series N :"); scanf ("% d", & N); n = N; F (n); printf ("\ n"); Return 0 ;}
5. Tower of Hanoi Problems
# Include <stdio. h> void hannuo (int n, char X, char y, char Z) {If (n = 1) printf ("% C-> % C \ n", X, z); else {hannuo (n-1, X, Z, Y); printf ("% C-> % C \ n", x, z); hannuo (n-1, y, x, Z) ;}} int main (void) {int N; printf ("Enter the number of floors in the Tower:"); scanf ("% d", & N ); printf (" 的: \ n"); hannuo (n, 'A', 'B', 'C'); Return 0 ;}
6. Eight queens
# Include <stdio. h> # define row 8 # define Col 8 # define num 8int A [row] [col]; int K = 0; int put (int n) {int I, J; // output eight Queen's board if (n> num) {printf ("---------- % 06d ---------- \ n", ++ K); for (I = 0; I <row; I ++) {for (j = 0; j <Col; j ++) printf ("% s", a [I] [J] = 1? "○": "●"); Printf ("\ n");} printf ("------------------------ \ n"); Return-1 ;} // try each position in the row for (j = 0; j <Col; j ++) {int flag = 0; for (I = 0; I <n-1; I ++) {// whether the same column if (a [I] [J] = 1) {flag = 1; break ;} // whether the left oblique angle if (J-(N-1-I)> = 0 & A [I] [J-(N-1-I)] = 1) {flag = 1; break;} // whether the right corner is if (J + (n-1-I) <Col & A [I] [J + (n-1-I)] = 1) {flag = 1; break ;}} if (flag = 0) {A [n-1] [J] = 1; if (put (n + 1) =-1) {A [n-1] [J] = 0 ;} elsereturn 0 ;}} return-1 ;}int main () {put (1); Return 0 ;}