实验3
任务1
源代码
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 } // 打印n个空格 21 void print_spaces(int n) { 22 int i; 23 for(i = 1; i <= n; ++i) 24 printf(" "); 25 } // 打印n行空白行 26 void print_blank_lines(int n) { 27 int i; 28 for(i = 1; i <= n; ++i) 29 printf("\n"); 30 } 31 // 在第line行第col列打印一段文本 32 void print_text(int line, int col, char text[]) { 33 print_blank_lines(line-1); // 打印(line-1)行空行 34 print_spaces(col-1); // 打印(col-1)列空格 35 printf("%s", text); // 在第line行、col列输出text中字符串 36 }
结果

任务2
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 long long fac(int n) { 12 static long long p = 1; 13 printf("p = %lld\n", p); 14 p = p * n; 15 return p; 16 }
结果

任务3
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 11 return 0; 12 } 13 long long func(int n){ 14 15 long long sum=1; 16 if(n==1) 17 sum=1; 18 else 19 sum=2*func(n-1)+1; 20 21 return sum; 22 23 }
运行结果

任务4
1 #include <stdio.h> 2 3 int func(int n, int m); 4 5 int main() { 6 int n, m; 7 while(scanf("%d%d", &n, &m) != EOF) 8 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 9 return 0; 10 } 11 int func(int n ,int m){ 12 long long ans; 13 if(n-1==m) 14 ans=1; 15 else if(m==1) 16 ans=n; 17 else if(n<m) 18 ans=0; 19 else if(m==0) 20 ans=1; 21 else 22 ans=func(n-1,m)+func(n-1,m-1); 23 24 return ans; 25 26 27 }
运行结果

任务5
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 void hanoi(unsigned int n, char from, char temp, char to); 5 void move(unsigned int n, char from, char to); 6 int a; 7 int main() 8 { 9 unsigned int n; 10 while (scanf("%d", &n) != EOF) 11 { 12 hanoi(n, 'A', 'B', 'C'); 13 printf("共移动了%d次\n", a); 14 a = 0; 15 } 16 return 0; 17 } 18 void hanoi(unsigned int n, char from, char temp, char to) 19 { 20 a++; 21 if (n == 1) 22 move(n, from, to); 23 else 24 { 25 26 hanoi(n - 1, from, to, temp); 27 move(n, from, to); 28 hanoi(n - 1, temp, from, to); 29 30 } 31 } 32 void move(unsigned int n, char from, char to) 33 { 34 printf("%u:%c-->%c\n", n, from, to); 35 }
运行结果

实验6
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <math.h> 4 long func(long s); // 函数声明 5 int main() { 6 long s, t; 7 printf("Enter a number: "); 8 while (scanf("%ld", &s) != EOF) { 9 t = func(s); // 函数调用 10 printf("new number is: %ld\n\n", t); 11 printf("Enter a number: "); 12 } 13 return 0; 14 } 15 long func(long s) 16 { 17 int i, k, d = 1, sum = 0; 18 int a[10]; 19 int b[10]; 20 for (i = 0, k = 0; i < 10; i++) 21 { 22 a[i] = s % 10; 23 if (a[i] % 2 != 0) 24 { 25 b[k] = a[i]; 26 k++; 27 } 28 s /= 10; 29 } 30 for (i = 0; i < k; i++) 31 { 32 sum += b[i] * d; 33 d *= 10; 34 } 35 return sum; 36 }
运行结果


浙公网安备 33010602011771号