实验3
task1
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void print_text(int line, int col, char text[]); // 函数声明 void print_spaces(int n); // 函数声明 void print_blank_lines(int n); // 函数声明 int main() { int line, col, i; char text[N] = "hi, November~"; srand(time(0)); // 以当前系统时间作为随机种子 for(i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; print_text(line, col, text); Sleep(1000); // 暂停1000ms } return 0; } // 打印n个空格 void print_spaces(int n) { int i; for(i = 1; i <= n; ++i) printf(" "); } // 打印n行空白行 void print_blank_lines(int n) { int i; for(i = 1; i <= n; ++i) printf("\n"); } // 在第line行第col列打印一段文本 void print_text(int line, int col, char text[]) { print_blank_lines(line-1); // 打印(line-1)行空行 print_spaces(col-1); // 打印(col-1)列空格 printf("%s", text); // 在第line行、col列输出text中字符串 }

line17-19
循环10次并随机赋给line0~24其中一个数,给col0~79其中一个数
void printf_spaces
打印n个空格
void printf_blank_lines
打印n行空白
void printf_text
利用随机生成的line与col 打印出line-1与col-1空白行与空格后,光标在第line行第col列打印出文本。
task2

1 // 利用局部static变量的特性,计算阶乘 2 3 #include <stdio.h> 4 long long fac(int n); // 函数声明 5 6 int main() { 7 int i, n; 8 9 printf("Enter n: "); 10 scanf("%d", &n); 11 12 for (i = 1; i <= n; ++i) 13 printf("%d! = %lld\n", i, fac(i)); 14 15 return 0; 16 } 17 18 // 函数定义 19 long long fac(int n) { 20 static long long p = 1; 21 p = p * n; 22 23 return p; 24 }

理论分析:p1 = 8,p2时,初值i = 3,m = 8,p2 = 17
1 // 练习:局部static变量特性 2 3 #include <stdio.h> 4 int func(int, int); // 函数声明 5 6 int main() { 7 int k = 4, m = 1, p1, p2; 8 9 p1 = func(k, m); // 函数调用 10 p2 = func(k, m); // 函数调用 11 printf("%d, %d\n", p1, p2); 12 13 return 0; 14 } 15 16 // 函数定义 17 int func(int a, int b) { 18 static int m = 0, i = 2; 19 20 i += m + 1; 21 m = i + a + b; 22 23 return m; 24 }

static 在局部函数里让某一变量始终存在,并且不被其他函数调用。
task3
1 #include <stdio.h> 2 long long func(int n); // 函数声明 3 4 int main() { 5 int n; 6 long long f; 7 8 while (scanf("%d", &n) != EOF) { 9 f = func(n); // 函数调用 10 printf("n = %d, f = %lld\n", n, f); 11 } 12 13 return 0; 14 } 15 16 // 函数定义 17 // 待补足。。。 18 long long func(int n){ 19 if(n == 0) 20 return 0; 21 if(n == 1) 22 return 1; 23 if(n > 1) 24 return 2*func(n - 1) + 1; 25 }

task4迭代
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 // 函数定义 14 // 待补足。。。 15 int func(int n, int m){ 16 int i,up = 1,down = 1,p = n; 17 for(i = 1;i <= m;i++){ 18 up = up*p; 19 p--; 20 down = down*i; 21 } 22 return up/down; 23 }

递归
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 // 函数定义 14 // 待补足。。。 15 int func(int n, int m){ 16 if(m == 0||m == n) 17 return 1; 18 if(n < m) 19 return 0; 20 else 21 return func(n - 1,m) + func(n - 1,m - 1); 22 }

task5
1 #include <stdio.h> 2 void hanoi(unsigned int n,char from,char temp,char to); 3 void moveplate(unsigned int n,char from,char to); 4 int count; 5 int main(){ 6 unsigned int n; 7 while(scanf("%d",&n) != EOF){ 8 count = 0; 9 hanoi(n ,'A','B','C'); 10 printf("%d\n",count); 11 } 12 return 0; 13 } 14 void hanoi(unsigned int n,char from,char temp,char to){ 15 if(n == 1) 16 moveplate(n,from,to); 17 else{ 18 hanoi(n-1,from,to,temp); 19 moveplate(n,from,to); 20 hanoi(n-1,temp,from,to); 21 } 22 23 } 24 25 void moveplate(unsigned int n,char from,char to){ 26 printf("%u:%c-->%c\n",n,from,to); 27 count++; 28 }

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 printf("new number is: %ld\n\n", t); 10 printf("Enter a number: "); 11 } 12 return 0; 13 } 14 long func(long s){ 15 int a,t,k; 16 t = 0; 17 do{ 18 a = s % 10; 19 if(a%2 != 0) 20 t = t*10 + a; 21 22 s = s/10; 23 }while(s != 0); 24 k = t; 25 t = 0; 26 do{ 27 a = k % 10; 28 t = t*10 + a; 29 k= k/10; 30 }while(k != 0); 31 return t; 32 }

task7

1 #include<stdio.h> 2 int s[10] = {0}; 3 void judge(int n){ //判断是否满足一个数是否由0~9构成且仅一次 4 int u,i; 5 do{ 6 u = n % 10; 7 s[u]++; 8 n /= 10; 9 }while(n != 0);//每位数字储存到数组s中,如果有一次对应s[u]加1 10 } 11 12 int main(){ 13 int n = 1,i; 14 while(1){ 15 judge(n*n); 16 judge(n*n*n); 17 if(s[0] == 1&&s[1] == 1&&s[2] == 1&&s[3] == 1&&s[4] == 1&&s[5] == 1&&s[6] == 1&&s[7] == 1&&s[8] == 1&&s[9] == 1){ 18 printf("%d",n); 19 break; 20 } 21 n++; 22 for(i = 0;i < 10;i++) 23 s[i] = 0; 24 } 25 return 0; 26 }

浙公网安备 33010602011771号