实验三

实验三

任务一

代码

 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 
10 int main(){
11     int line,col,i;
12     char text[N] = "hi,November~";
13     
14     srand(time(0));
15     
16         for(i = 1; i <= 10; ++i) {
17         line = rand() % 25;
18         col =  rand() % 80;
19         print_text(line, col, text);
20         Sleep(1000);  
21     }
22     
23     
24     
25     return 0;
26 }
27 
28 void print_spaces(int n){
29     int i;
30     
31     for(i = 1; i <= n;i++){
32         printf(" ");
33     }
34 }
35 
36 void print_blank_lines(int n){
37     int i;
38     
39     for(i = 1;i <= n;i++){
40         printf("\n");
41     }
42 }
43 
44 void print_text(int line,int col,char text[]){
45     print_blank_lines(line - 1);
46     print_spaces(col - 1);
47     printf("%s",text);
48 }

 

截图

任务二

代码

 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     printf("%lld\n",p);
22     p = p * n;
23 
24     return p;
25 }

 

 

截图

 

任务三

代码

 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 long long func(int n){
17     if(n == 1)
18     return 1;
19     else{
20         return 2*func(n-1) + 1;
21     }
22 }
23 
24 // 函数定义
25 // 待补足。。。

 

截图

任务四

代码

1.迭代

 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 i,j,k,l;
15     j = 1;
16     k = 1;
17     l = 1;
18     for(i = 1;i <= n;i++){
19         j *= i;
20     }
21     for(i = 1;i <= m;i++){
22         k *= i;
23     }
24     for(i = 1;i <= n-m;i++){
25         l *= i;
26     }
27     return j/(k*l);
28 }

截图

 

2.递归

 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     if(m == 0)
17     return 1;
18     if(m == 1)
19     return n;
20     if(m == n)
21     return 1;
22     else
23     return func(n - 1,m) + func(n-1,m-1);
24 
25 }

 

截图

任务五

代码

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

 

截图

任务六

代码

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

 

截图

 

任务七

代码

 

#include<stdio.h>
int pan(int m,int n);
int main(){
    int n;
    int j,k;
    while(scanf("%d",&n)!=EOF){
        j = n*n;
        k = n*n*n;
        if(pan(j,k))
        break;
        else
        printf("不行!\n");
    }
    printf("可以的");
    
    
    return 0;

}
int pan(int m,int n){
    int x,y,z;
    while(m!=0){
        x = m%10;
        while(n!=0){
            y = n%10;
            if(x == y)
            return 0;
            n /= 10;
        }
        x /= 10;
    }
    return 1;
}
 1 #include<stdio.h>
 2 int pan(int m,int n);
 3 int main(){
 4     int n;
 5     int j,k;
 6     while(scanf("%d",&n)!=EOF){
 7         j = n*n;
 8         k = n*n*n;
 9         if(pan(j,k) == 1){
10             printf("可以");
11             break;
12         }
13         
14         
15         else
16         printf("不行!\n");
17     }
18     
19 
20     return 0;
21 
22 }
23 int pan(int m,int n){
24     int x,y,z,j,k;
25     int q,w,e,r;
26     q = m;
27     e = m;
28     while(q != 0){
29         w = q%10;
30         while(e != 0){
31             r = e%10;
32             if(w == r)
33             return 0;
34             e /= 10;
35         }
36         q /= 10;
37     }
38     int u,i,o,p;
39     u = n;
40     o = n;
41         while(u != 0){
42         i = q%10;
43         while(o != 0){
44             p = o%10;
45             if(i == p)
46             return 0;
47             o /= 10;
48         }
49         u /= 10;
50     }
51     
52     while(m!=0){
53         x = m%10;
54         while(n!=0){
55             y = n%10;
56             if(x == y)
57             return 0;
58             n /= 10;
59         }
60         m /= 10;
61     }
62     return 1;
63 }

 以上不正确

 1 #include<stdio.h>
 2 #include<string.h>
 3 int pan(int m,int n);
 4 int main(){
 5     int n;
 6     int j,k;
 7     while(scanf("%d",&n)!=EOF){
 8         j = n*n;
 9         k = n*n*n;
10         if(pan(j,k) == 1){
11             printf("可以");
12             break;
13         }
14 
15         else
16         printf("不行!\n");
17     }
18 
19 
20     return 0;
21 
22 }
23 int pan(int m,int n){
24     int a,b,c,d;
25     int i,j;
26     i = 0;
27     j = 0;
28     char A[10];
29     a = m;
30     while(a != 0){
31         b = a%10;
32         A[i++] = b;
33         a /= 10;
34     }
35     int len = strlen(A);
36     for(i = 0;i < len-1;i++){
37         for(j = i+1;j < len;j++){
38             if(A[i] == A[j]){
39                 return 0;
40             }
41         }
42     }
43     i = 0;
44     j = 0;
45     c = n;
46     char B[10];
47     while(c != 0){
48         d = c%10;
49         B[i++] = d;
50         c /= 10;
51         
52     }
53     int len1 = strlen(B);
54     for(i = 0;i < len1-1;i++){
55     for(j = i+1;j < len1;j++){
56             if(B[i] == B[j]){
57                 return 0;
58             }
59         }
60     }
61     int e,f,g,h;
62     e = m;
63     f = n;
64     while(e != 0){
65         g = e%10;
66         while(f != 0){
67             h = f%10;
68             if(g == h)
69             return 0;
70             f /= 10;
71         }
72         
73         e /=10;
74     }
75     int flag,k;
76     for(i = 0;i <= 9;i++){
77         flag = 1;
78         for(j = 0;j < len;j++){
79             if(A[j] == i){
80                 flag = 0;
81                 break;
82             }
83         }
84         for(k = 0;k < len1;k++){
85             if(B[k] == i){
86                 flag = 0;
87                 break;
88             }
89         }
90         if(flag){
91             return 0;
92         }
93     }
94     return 1;
95     
96      
97 
98 }

 

截图

 

 

posted @ 2023-10-30 21:12  枯基Evan  阅读(13)  评论(0编辑  收藏  举报