task1 code
1 include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <windows.h> 5 #define N 80 6 7 void print_text(int line, int col, char text[]); // 函数声明 8 void print_spaces(int n); // 函数声明 9 void print_blank_lines(int n); // 函数声明 10 11 int main() { 12 int line, col, i; 13 char text[N] = "hi, November~"; 14 15 srand(time(0)); // 以当前系统时间作为随机种子 16 17 for (i = 1; i <= 10; ++i) { 18 line = rand() % 25; 19 col = rand() % 80; 20 print_text(line, col, text); 21 Sleep(1000); // 暂停1000ms 22 } 23 24 return 0; 25 } 26 27 // 打印n个空格 28 void print_spaces(int n) { 29 int i; 30 31 for (i = 1; i <= n; ++i) 32 printf(" "); 33 } 34 35 // 打印n行空白行 36 void print_blank_lines(int n) { 37 int i; 38 39 for (i = 1; i <= n; ++i) 40 printf("\n"); 41 } 42 43 // 在第line行第col列打印一段文本 44 void print_text(int line, int col, char text[]) { 45 print_blank_lines(line - 1); // 打印(line-1)行空行 46 print_spaces(col - 1); // 打印(col-1)列空格 47 printf("%s", text); // 在第line行、col列输出text中字符串 48 }
task1 result
描述:随机空出一定空格或行,打印hi, November
task2_1code
1 #include <stdio.h> 2 long long fac(int n); // 函数声明 3 4 int main() { 5 int i, n; 6 printf("Enter n: "); 7 8 scanf("%d", &n); 9 for (i = 1; i <= n; ++i) 10 printf("%d! = %lld\n", i, fac(i)); 11 return 0; 12 } 13 // 函数定义 14 long long fac(int n) { 15 static long long p = 1; 16 p = p * n; 17 18 return p; 19 }
task2_1 result
task2_2 code
1 #include <stdio.h> 2 int func(int, int); // 函数声明 3 int main() { 4 int k = 4, m = 1, p1, p2; 5 p1 = func(k, m); // 函数调用 6 p2 = func(k, m); // 函数调用 7 printf("%d, %d\n", p1, p2); 8 return 0; 9 } 10 // 函数定义 11 int func(int a, int b) { 12 static int m = 0, i = 2; 13 i += m + 1; 14 m = i + a + b; 15 return m; 16 }
task2_2 result
总结:static变量只会初始化一次,之后不会初始化。
task3 code
1 #include <stdio.h> 2 long long func(int n); 3 int main() { 4 int n; 5 long long f; 6 7 while (scanf("%d", &n) != EOF) { 8 f = func(n); 9 printf("n = %d, f = %lld\n", n, f); 10 } 11 12 return 0; 13 } 14 15 long long func(int n) { 16 17 if (n == 0) 18 return 0; 19 else 20 return 2 * func(n - 1) + 1; 21 }
task3 result
task4 code1迭代
1 #include <stdio.h> 2 int func(int n, int m); 3 4 int main() { 5 int n, m; 6 7 while (scanf("%d%d", &n, &m) != EOF) 8 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 9 10 return 0; 11 } 12 13 int func(int n, int m) { 14 int up = 1, down1 = 1, down2 = 1; 15 int i; 16 int ans; 17 18 if (n < m) 19 return 0; 20 else { 21 for (i = 1; i <= n; i++) 22 up *= i; 23 for (i = 1; i <= m; i++) 24 down1 *= i; 25 for (i = 1; i <= (n - m); i++) 26 down2 *= i; 27 return up / (down1 * down2); 28 } 29 }
code1 result
code2递归
1 #include <stdio.h> 2 int func(int n, int m); 3 4 int main() { 5 int n, m; 6 7 while (scanf("%d%d", &n, &m) != EOF) 8 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 9 10 return 0; 11 } 12 13 int func(int n, int m) { 14 if (n < m) 15 return 0; 16 else if (n == m || m == 0) 17 return 1; 18 else 19 return func(n - 1, m) + func(n - 1, m - 1); 20 }
code2 result
task5 code
1 #include<stdio.h> 2 3 void hanoi(unsigned n, char from, char temp, char to); 4 void moveplate(unsigned n, char from, char to); 5 int i = 0; 6 int main() { 7 unsigned n; 8 while (scanf("%u", &n) != EOF) { 9 hanoi(n, 'A', 'B', 'C'); 10 printf("一共移动了%d次\n", i); 11 i = 0; 12 } 13 14 return 0; 15 } 16 17 void hanoi(unsigned n, char from, char temp, char to) { 18 if (n == 1) 19 moveplate(n, from, to); 20 else { 21 hanoi(n - 1, from, to, temp); 22 moveplate(n, from, to); 23 hanoi(n - 1, temp, from, to); 24 } 25 } 26 27 void moveplate(unsigned n, char from, char to) { 28 printf("%u: %c-->%c\n", n, from, to); 29 i++; 30 }
task5 result
task code
1 #include <stdio.h> 2 #include <math.h> 3 long func(long s); 4 5 int main() { 6 long s, t; 7 8 printf("Enter a number: "); 9 while (scanf("%ld", &s) != EOF) { 10 t = func(s); 11 printf("new number is: %ld\n\n", t); 12 printf("Enter a number: "); 13 } 14 15 return 0; 16 } 17 18 long func(long s) { 19 int i = 1; 20 int ans = 0; 21 22 while (s > 0) { 23 if ((s % 10) % 2 != 0) { 24 ans = ans + (s % 10) * i; 25 i *= 10; 26 s /= 10; 27 } 28 else 29 s /= 10; 30 } 31 32 return ans; 33 }
task6 result