实验3

复制代码
 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 
 9 void print_spaces(int n); // 函数声明 
10 
11 void print_blank_lines(int n); // 函数声明 
12 
13 int main() {
14  int line, col, i;
15  char text[N] = "hi, April~";
16  
17  srand(time(0)); // 以当前系统时间作为随机种子
18 
19  
20  for(i = 1; i <= 10; ++i) {
21  line = rand() % 25;
22  col = rand() % 80;
23  print_text(line, col, text);
24  Sleep(1000); // 暂停1000ms
25 
26 }
27  return 0; 
28 }
29 
30 // 打印n个空格 
31 
32 void print_spaces(int n) {
33  int i;
34  
35  for(i = 1; i <= n; ++i)
36  printf(" ");
37 }
38 
39 // 打印n行空白行
40 
41 void print_blank_lines(int n) {
42  int i;
43  
44  for(i = 1; i <= n; ++i)
45  printf("\n");
46  } 
47 
48 // 在第line行第col列打印一段文本 
49 
50 void print_text(int line, int col, char text[]) {
51  print_blank_lines(line-1); // 打印(line-1)行空行 
52 
53  print_spaces(col-1); // 打印(col-1)列空格
54 
55  printf("%s", text);// 在第line行、col列输出text中字符串
56 
57 }

代码功能:随机生成行和列,打印10次"hi,April~",每次间隔1000ms

task2-1

复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n); // 函数声明
 4 
 5 int main() {
 6     int i, n;
 7 
 8     printf("Enter n: ");
 9     scanf("%d", &n);
复制代码
10 
11     for (i = 1; i <= n; ++i)
12         printf("%d! = %lld\n", i, fac(i));
13     system("pause");
14     return 0;
15 }
16 
17 // 函数定义
18 long long fac(int n) {
19     static long long p = 1;
20 
21     p = p * n;
22 
23     return p;
24 }
复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n); // 函数声明
 4 
 5 int main() {
 6     int i, n;
 7 
 8     printf("Enter n: ");
 9     scanf("%d", &n);
10 
11     for (i = 1; i <= n; ++i)
12         printf("%d! = %lld\n", i, fac(i));
13     system("pause");
14     return 0;
15 }
16 
17 // 函数定义
18 long long fac(int n) {
19     static long long p = 1;
20 
21     p = p * n;
22     printf("p = %lld\n", p);
23     return p;
24 }

task 2-2

复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int func(int, int); // 函数声明
 4 
 5 int main() {
 6  int k = 4, m = 1, p1, p2;
 7  p1 = func(k, m); // 函数调用
 8 
 9  p2 = func(k, m); // 函数调用
10 
11  printf("%d, %d\n", p1, p2);
12  system("pause");
13  return 0;
14 }
15 
16 // 函数定义
17 
18 int func(int a, int b) {
19  static int m = 0, i = 2;
20  i += m + 1;
21  m = i + a + b;
22  return m;
23 }

stastic:变量的值保留上一次调用的值

task 3

复制代码
 1 #include <stdio.h>
 2 
 3 long long func(int n); // 函数声明
 4 
 5 int main() {
 6  int n;
 7  long long f;
 8  while (scanf("%d", &n) != EOF) {
 9  f = func(n); // 函数调用
10 
11  printf("n = %d, f = %lld\n", n, f);
12  }
13 return 0;
14 }
15 
16 long long func(int n)
17 {
18     long long ans;
19     if (n==0)
20         ans=0;
21     else
22         ans=2*func(n-1)+1;
23     return ans;
24 }

task 4

复制代码
 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 int func(int n, int m)
13 {
14     int i,up=1,down=1,ans;
15     if(n>m && m!=0)
16          {for(i=n-m+1;i<=n;i++)
17               up=up*i;
18           for(i=1;i<=m;i++)
19               down=down*i;
20           ans=up/down;}
21     if(n==m)
22         ans=1;
23     if(n<m)
24         ans=0;
25     if(m==0)
26         ans=1;
27     return ans;
28 }
复制代码
 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 int func(int n, int m)
13 {
14     int i,ans;
15     if(n==m)
16         ans=1;
17     if(n<m)
18         ans=0;
19     if(m==0)
20         ans=1;
21     if(n>m &&m!=0)
22         ans=func(n-1,m)+func(n-1,m-1);
23     return ans;
24 }

task 5

复制代码
 1 # include < stdio.h>
 2 # include < stdlib.h>
 3 # include  <math.h>
 4 void hanoi(unsigned int n, char from, char temp,char to);
 5 void moveplate(unsigned int n, char from, char to);
 6 
 7 int main()
 8 {
 9     unsigned int n;
10     int    i;
11     while((scanf("%u",&n)!=EOF))
12     {
13         hanoi(n,'A','B','C');
14         i=pow(2.0,n*1.0)-1;
15         printf("一共移动了%d次\n",i);
16     }
17     return 0;
18 }
19 void hanoi(unsigned int n,char from, char temp, char to)
20 {
21     if(n==1)
22         moveplate(n,from,to);
23     else
24     {    hanoi(n-1,from,to,temp);
25         moveplate(n, from,to);
26         hanoi(n-1,temp, from,to);
27     }
28 }
29 void moveplate(unsigned int n, char from, char to)
30 {
31     printf("%u:%c-->%c\n",n,from,to);
32     
33 }

task 6

复制代码
 1 #include <stdio.h>
 2 #include <math.h>
 3 long func(long s);   // 函数声明
 4 
 5 int main()
 6 {
 7 
 8     long s, t;
 9 
10     printf("Enter a number: ");
11     while (scanf("%ld", &s) != EOF) {
12         t = func(s); // 函数调用
13         printf("new number is: %ld\n\n", t);
14         printf("Enter a number: ");
15     }
16 
17     return 0;
18 }
19 long func(long s)
20 {
21     int a,b=0,i=1;
22     long ans;
23     while(s!=0)
24         {
25             a=s%10;
26             if(a%2!=0&&a!=0)
27                {
28                    b=pow(10.0,i-1)*a+b;
29                    i=i+1;
30                }
31             s=s/10;
32 
33          }
34     return b;
35 }