首页 > 其他分享 >实验3

实验3

时间:2023-10-31 23:33:54浏览次数:26  
标签:int long char 实验 func printf include

task1 源代码

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

 

task1 截屏

 

task2 源代码

 1 #include <stdio.h>
 2 
 3 long long fac(int n);
 4 
 5 int main()
 6 {
 7     int i, n;
 8     
 9     printf("Enter n: ");
10     scanf("%d",&n);
11     
12     for (i = 1;i <= n;++i)
13     {
14         printf("%d! = %lld\n", i, fac(i));
15     }
16     
17     return 0;
18 }
19 
20 
21 long long fac(int n)
22 {
23     static long long p = 1;
24      p *= n;
25      
26      return p;
27 }
 1 #include <stdio.h>
 2 
 3 int func(int, int);
 4 
 5 int main()
 6 {
 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 {
19     static int m = 0, i = 2;
20     
21     i += m + 1;
22     m = i + a + b;
23      
24      return m;
25 }

 

task2 截屏

 

 

task3 源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 long long func(int n);
 5 
 6 int main()
 7 {
 8     int n;
 9     long long f;
10     
11     while (scanf("%d",&n) != EOF)
12     {
13         f = func(n);
14         printf("n = %d, f = %lld\n", n, f);
15     }
16     
17     system("pause");
18     return 0;
19 }
20 
21 
22 long long func(int n)
23 {
24     long long f = 1;
25     
26     if (n == 0)
27         f = 0;
28     else
29         f = func(n-1)*2+1;
30     
31     return f;
32 }

 

task3 截屏

 

 

task4 源代码

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 int func(int n, int m);
 5 
 6 int main()
 7 {
 8     int n, m;
 9     
10     while(scanf("%d%d",&n,&m) != EOF)
11           printf("n = %d, m = %d, ans = %d\n", n, m, func(n,m));
12     
13     system("pause");
14     return 0;
15 }
16 
17 
18 int func(int n, int m)
19 {
20     int ans, ans1;
21     
22         if(n < m)
23         {
24             ans1 = 0;
25         }
26         else if ( m == 1)
27         {
28             ans1 = n;
29         }
30         else if( m == 0)
31         {
32             ans1 = 1;
33         }
34         else
35         {
36             ans1 = func(n-1, m)+func(n-1, m-1);
37         }
38     
39     return ans1;
40 }
 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 int func(int n, int m);
 5 
 6 int main()
 7 {
 8     int n, m;
 9     
10     while(scanf("%d%d",&n,&m) != EOF)
11           printf("n = %d, m = %d, ans = %d\n", n, m, func(n,m));
12     
13     system("pause");
14     return 0;
15 }
16 
17 
18 int func(int n, int m)
19 {
20     int i = 1, j = 1, part1 = 1, part2 = 1, ans;
21     
22     if (n < m){
23     
24     ans = 0;}
25     
26     else if(n >= m){
27     for(i = n-m+1;i <= n;i++)
28     {
29         part1 *= i;
30     }
31     for(j = 1;j <= m;j++)
32     {
33         part2 *= j;
34     }
35     }
36     
37     
38     ans = part1/part2;
39     
40     return ans;
41 }

 

task4 截屏

 

task5 源代码

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void ta(int n, char from, char temp, char to);
 5 void move(int n, char from, char to);
 6 
 7 static int count = 0;
 8 
 9 int main()
10 {
11     int n;
12     
13     while (scanf("%d",&n) != EOF)
14     {
15         ta(n, 'A', 'B', 'C');
16         printf("%d\n",count);
17         count = 0;
18     }
19     
20     system("pause");
21     return 0;
22 }
23 
24 
25 void ta(int n, char from, char temp, char to)
26 {
27     if( n == 1 )
28     {
29         move(n, from, to);
30     }
31     else
32     {
33         ta(n-1, from, to, temp);
34         move(n, from, to);
35         ta(n-1, temp, from, to);
36     }
37 }
38 
39 void move(int n, char from, char to)
40 {
41     printf("%d: %c --> %c\n", n, from, to);
42     ++count;
43 }

 

task5 截屏

 

 

task6 源代码

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>
 4 #define N 500
 5 
 6 int x[N];
 7 
 8 long func(long s);
 9 
10 int main()
11 {
12     long s, t;
13     
14     printf("Enter a number: ");
15     while (scanf("%ld",&s) != EOF)
16     {
17         t = func(s);
18         printf("New number is: %ld\n\n", t);
19         printf("Enter a number: ");
20     }
21     
22     system("pause");
23     return 0;
24 }
25 
26 
27 long func(long s)
28 {
29     long ans = 0, q;
30     int n = 0, count = 0, m, j = 1, i, ten = 1, p;
31     q = s;
32     
33     do{
34         q /= 10;
35         ++n;
36     }while(q != 0);
37     
38     for(i = 1;i <= n;i++)
39     {
40         m = s%10;
41         if (m%2 != 0)
42         {
43             x[j] = m;
44         
45             for(p = 2; p <= j;p++)
46             {
47                 ten *= 10;
48             }
49         
50             ans += x[j]*ten;
51             ten = 1;
52             j++;
53         }
54         
55         s = s/10;
56     }
57     
58     return ans;
59 }

 

task6 截屏

 

 

标签:int,long,char,实验,func,printf,include
From: https://www.cnblogs.com/7perch/p/17798398.html

相关文章

  • 软件设计实验4:抽象工厂模式
    实验4:抽象工厂模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解抽象工厂模式的动机,掌握该模式的结构;2、能够利用抽象工厂模式解决实际问题。 [实验任务一]:人与肤色使用抽象工厂模式,完成下述产品等级结构: 实验要求:1. 画出对应的类图; 2. 提交源代......
  • 软件设计实验5:建造者模式
    实验5:建造者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解建造者模式的动机,掌握该模式的结构;2、能够利用建造者模式解决实际问题。 [实验任务一]:计算机组装使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起......
  • 软件设计实验1:UML与面向对象程序设计原则
    实验1:UML与面向对象程序设计原则本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、掌握面向对象程序设计中类与类之间的关系以及对应的UML类图;2、理解面向对象程序设计原则。 [实验任务一]:UML复习阅读教材第一章复习UML,回答下述问题:面向对象程序设计中类与类的关......
  • 软件设计实验2:简单工厂模式
    实验2:简单工厂模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解简单工厂模式的动机,掌握该模式的结构;2、能够利用简单工厂模式解决实际问题。 [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参......
  • 软件设计实验3:工厂方法模式
    实验3:工厂方法模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解工厂方法模式的动机,掌握该模式的结构;2、能够利用工厂方法模式解决实际问题。 [实验任务一]:加密算法目前常用的加密算法有DES(DataEncryptionStandard)和IDEA(InternationalDataEncryption......
  • 实验三
    test1.c1#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_......
  • 实验3
    实验任务1实验任务2.1实验任务2.2 实验任务3实验任务4实验任务5 实验任务6 ......
  • 实验3 c语言函数应用编程
    task11源代码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)......
  • 实验3
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明int......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain()......