首页 > 其他分享 >实验三

实验三

时间:2023-10-30 21:22:57浏览次数:23  
标签:10 return int while 实验 printf include

实验三

任务一

代码

 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 }

 

截图

 

 

标签:10,return,int,while,实验,printf,include
From: https://www.cnblogs.com/kujievan1/p/17798422.html

相关文章

  • 实验三
    实验1每隔100ms在随机显示字符串 实验2 一致 实验3    实验4   实验5   实验六    实验七  #include<stdio.h>intfunc(intn2,intn3);intmain(){ intn=0; intflag=0; while(1){ intn2,n3; n2=n*n;......
  • 实验三 计算机九班周天意202383290419
    一、实验目的1.能正确使用c语法规则定义、声明、调用函数2.能正确编写递归函数3.针对具体问题场景,能合理抽象出独立的功能模块,正确定义函数并使用,使得代码更具可读性、可维护性4.针对具体问题场景,能正确、合理使用全局变量和局部static变量,解决实际问题二、实验准备实验前......
  • 实验3
    task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_blank_l......
  • 实验3 C语言函数应用编程
    一、实验目的能正确使用c语法规则定义、声明、调用函数能正确编写递归函数针对具体问题场景,能合理抽象出独立的功能模块,正确定义函数并使用,使得代码更具可读性、可维护性针对具体问题场景,能正确、合理使用全局变量和局部static变量,解决实际问题二、实验准备函数定义、声......
  • 实验三
    #include<stdio.h>inti=0;voidhanoi(unsignedintn,charfrom,chartemp,charto);voidmoveplate(unsignedintn,charfrom,charto);intmain(){unsignedintn;while(scanf("%u",&n)!=EOF){i=0;......
  • 实验3_C语言函数应用编程
    1.task_11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn)......
  • 实验3
    实验1代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn)......
  • 实验三
    实验一结果,会在随机的某一行(<25)某一列出现输出实验二结果:实现阶乘,static保证每一次循环中p的值会随之而改变,相当于我们只初始化一次p这个变量、实验三#include<stdio.h>#include<stdio.h>longlongfunc(intn);//函数声明intmain(){intn;longlongf;......
  • 实验3
    task1.c实现功能:延迟打印,以实现游戏运行的效果1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(intn);......
  • 大数据实验
       ......