task1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <windows.h> 5 #define N 80 6 void print_text(int line, int col, char text[]); // 函数声明 7 void print_spaces(int n); // 函数声明 8 void print_blank_lines(int n); // 函数声明 9 int main() { 10 int line, col, i; 11 char text[N] = "hi, November~"; 12 srand(time(0)); // 以当前系统时间作为随机种子 13 for (i = 1; i <= 10; ++i) { 14 line = rand() % 25; 15 col = rand() % 80; 16 print_text(line, col, text); 17 Sleep(1000); // 暂停1000ms 18 } 19 return 0; 20 } 21 // 打印n个空格 22 void print_spaces(int n) { 23 int i; 24 for (i = 1; i <= n; ++i) 25 printf(" "); 26 } 27 // 打印n行空白行 28 void print_blank_lines(int n) { 29 int i; 30 for (i = 1; i <= n; ++i) 31 printf("\n"); 32 } 33 // 在第line行第col列打印一段文本 34 void print_text(int line, int col, char text[]) { 35 print_blank_lines(line - 1); // 打印(line-1)行空行 36 print_spaces(col - 1); // 打印(col-1)列空格 37 printf("%s", text); // 在第line行、col列输出text中字符串 38 }
如下图生成(还有4个未显示)
代码功能:随机在某行某列打印“hi,November”,并在其之后的某行与某列再次打印,重复9次,共打印10个
task2.1
1 #include <stdio.h> 2 long long fac(int n); // 函数声明 3 int main() { 4 int i, n; 5 printf("Enter n: "); 6 scanf("%d", &n); 7 for (i = 1; i <= n; ++i) 8 printf("%d! = %lld\n", i, fac(i)); 9 return 0; 10 } 11 // 函数定义 12 long long fac(int n) { 13 static long long p = 1; 14 //printf("p = %lld\n", p); 15 p = p * n; 16 return p; 17 }
task2.2
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 7 p2 = func(k, m); // 函数调用 8 printf("%d, %d\n", p1, p2); 9 return 0; 10 } 11 // 函数定义 12 int func(int a, int b) { 13 static int m = 0, i = 2; 14 i += m + 1; 15 m = i + a + b; 16 return m; 17 }
1.首先在p1中,i=2+0+1=3,m=i+a+b=3+1+4=8
2.之后函数外面的m还是1,k还是4,但是函数里面的m是8,i是3
3.p2中,i=i+m+1=3+8+1=12,m=i+a+b=12+1+4=17
总结:局部static变量可在整个代码中循环,但是作用域只局限在局部
task3
1 #include <stdio.h> 2 long long func(int n); // 函数声明 3 int main() { 4 int n; 5 long long f; 6 while (scanf("%d", &n) != EOF) { 7 f = func(n); // 函数调用 8 printf("n = %d, f = %lld\n", n, f); 9 } 10 return 0; 11 } 12 13 long long func(int n) { 14 long long sum = 1; 15 if (n == 1) { 16 sum = 1; 17 } 18 else if (n > 1) { 19 sum = 2 * func(n - 1) + 1; 20 } 21 return sum; 22 }
task4
1.迭代:
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() { 4 int n, m; 5 while (scanf("%d%d", &n, &m) != EOF) 6 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 7 return 0; 8 } 9 10 int func(int n, int m) { 11 int sum1 = 1; 12 int sum2 = 1; 13 int sum3 = 1; 14 15 int s = n - m; 16 if (s < 0) { 17 return 0; 18 } 19 20 for (n; n > 0; n--) { 21 sum1 *= n; 22 } 23 24 for (m; m > 0; m--) { 25 sum2 *= m; 26 } 27 28 for (s; s > 0; s--) { 29 sum3 *= s; 30 } 31 32 int sum = sum1 / (sum2 * sum3); 33 34 return sum; 35 36 }
2.递归
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() { 4 int n, m; 5 while (scanf("%d%d", &n, &m) != EOF) 6 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 7 return 0; 8 } 9 10 int func(int n, int m) { 11 if (n < m) { 12 return 0; 13 } 14 int sum; 15 if (m == 0) { 16 return 1; 17 } 18 if (n == 1 && m == 1) { 19 sum = 1; 20 } 21 else if (m == 1) { 22 sum = n; 23 } 24 else { 25 sum = func(n - 1, m) + func(n - 1, m - 1); 26 } 27 28 return sum; 29 }
两者结果一样:
task5
1 #include <stdio.h> 2 3 void move(int n, char A, char B, char C); 4 5 int main() 6 { 7 int n; 8 char A, B, C; 9 while (scanf("%d", &n) != EOF) { 10 int step = 0; 11 move(n, 'A', 'B', 'C'); 12 for (n; n > 0; n--) { 13 step = 2 * step + 1; 14 } 15 printf("\n"); 16 printf("一共移动了%d次\n", step); 17 printf("\n"); 18 } 19 20 return 0; 21 } 22 23 void move(int n, char A, char B, char C) { 24 if (n == 1) { 25 printf("%d:%c --> %c\n", n, A, C); 26 } 27 else { 28 move(n - 1, 'A', 'C', 'B'); 29 printf("%d:%c --> %c\n", n, A, C); 30 move(n - 1, 'B', 'A', 'C'); 31 } 32 }
tip:
三柱汉诺塔移动n个盘的步数是(n-1)盘的步数的2倍多1;
step(n)=2*step(n-1)+1;
task6
1 #include <stdio.h> 2 #include <math.h> 3 long func(long s); // 函数声明 4 int main() { 5 long s, t; 6 printf("Enter a number: "); 7 while (scanf("%ld", &s) != EOF) { 8 t = func(s); // 函数调用 9 t = func(t); 10 printf("new number is: %ld\n\n", t); 11 printf("Enter a number: "); 12 } 13 return 0; 14 } 15 16 long func(long s) { 17 int n; 18 long sum = 0; 19 while (s > 0) { 20 n = s % 10; 21 if (n % 2 != 0) { 22 sum = sum * 10 + n; 23 } 24 s /= 10; 25 } 26 27 return sum; 28 }
task7
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() 4 { 5 int n = 1; 6 int flag = 0; 7 while (flag == 0) { 8 int n2 = n * n; 9 int n3 = n * n * n; 10 flag = func(n2, n3); 11 if (flag == 1) { 12 printf("result=%d\n", n); 13 printf("它的平方是%d*%d=%d\n", n, n, n2); 14 printf("它的立方是%d*%d*%d=%d\n", n, n, n, n3); 15 return 0; 16 } 17 else { 18 n++; 19 continue; 20 } 21 } 22 return 0; 23 } 24 25 int func(int n, int m) { 26 int k = 0; 27 int s; 28 int jugde[10]; 29 for (s = 0; s < 10; s++) { 30 jugde[s] = 1; 31 } 32 while (n > 0) { 33 s = n % 10; 34 switch (s) { 35 case 0: 36 jugde[s]--; 37 break; 38 case 1: 39 jugde[s]--; 40 break; 41 case 2: 42 jugde[s]--; 43 break; 44 case 3: 45 jugde[s]--; 46 break; 47 case 4: 48 jugde[s]--; 49 break; 50 case 5: 51 jugde[s]--; 52 break; 53 case 6: 54 jugde[s]--; 55 break; 56 case 7: 57 jugde[s]--; 58 break; 59 case 8: 60 jugde[s]--; 61 break; 62 case 9: 63 jugde[s]--; 64 break; 65 } 66 n /= 10; 67 } 68 while (m > 0) { 69 s = m % 10; 70 switch (s) { 71 case 0: 72 jugde[s]--; 73 break; 74 case 1: 75 jugde[s]--; 76 break; 77 case 2: 78 jugde[s]--; 79 break; 80 case 3: 81 jugde[s]--; 82 break; 83 case 4: 84 jugde[s]--; 85 break; 86 case 5: 87 jugde[s]--; 88 break; 89 case 6: 90 jugde[s]--; 91 break; 92 case 7: 93 jugde[s]--; 94 break; 95 case 8: 96 jugde[s]--; 97 break; 98 case 9: 99 jugde[s]--; 100 break; 101 } 102 m /= 10; 103 } 104 for (s = 0; s < 10; s++) { 105 if (jugde[s] != 0) { 106 k = 0; 107 break; 108 } 109 else { 110 k = 1; 111 } 112 } 113 return k; 114 }
标签:case,jugde,int,break,--,实验,func From: https://www.cnblogs.com/VitaminC114514/p/17798113.html