当前位置: 首页 > news >正文

实验作业3

试验任务1

1

 1 #include <stdio.h>
 2 char score_to_grade(int score);
 3 int main()
 4 {
 5     int score;
 6     char grade;
 7     while(scanf("%d",&score) !=EOF){
 8         grade=score_to_grade(score);
 9         printf("分数:%d,等级:%c\n\n",score,grade);
10     } 
11     return 0;
12  } 
13  char score_to_grade(int score){
14      char ans;
15      switch(score/10){
16          case 10:
17          case 9: ans='A';break;
18          case 8: ans='B'; break;
19          case 7: ans='C';break;
20          case 6: ans='D';break;
21          default: ans='E';
22      }
23      return ans;
24  }
View Code

捕获

2

(1)score_to_grade 函数的功能是将分数转换成等级;形参是整数型;返回值是字符型。

(2)首先没有定义ans;接着case后没有使用break;最后没有返回值。

试验任务2

1

 1 #include <stdio.h>
 2 
 3 int sum_digits(int n);  // 函数声明
 4 
 5 int main() {
 6     int n;
 7     int ans;
 8 
 9     while(printf("Enter n: "), scanf("%d", &n) != EOF) {
10         ans = sum_digits(n);    // 函数调用
11         printf("n = %d, ans = %d\n\n", n, ans);
12     }
13 
14     return 0;
15 }
16 
17 // 函数定义
18 int sum_digits(int n) {
19     int ans = 0;
20 
21     while(n != 0) {
22         ans += n % 10;
23         n /= 10;
24     }
25 
26     return ans;
27 }
View Code

捕获

2

(1)sum_diguts的功能是将整数的各位相加。

(2)能输出相同的结果;第一种用的是迭代思想;第二种用的是递归思想。

实验任务3

1

#include <stdio.h>int power(int x, int n);    // 函数声明int main() {int x, n;int ans;while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) {ans = power(x, n);  // 函数调用printf("n = %d, ans = %d\n\n", n, ans);}return 0;
}// 函数定义
int power(int x, int n) {int t;if(n == 0)return 1;else if(n % 2)return x * power(x, n-1);else {t = power(x, n/2);return t*t;}
}
View Code

捕获

2

(1)power的功能是计算x的n次幂。

(2)是的;

实验任务4

 

 1 #include <stdio.h>
 2 int is_prime(int n);
 3 int main()
 4 {
 5     int i,sum=0;
 6     printf("100以内的孪生素数:\n");
 7     for(i=2;i<=100;i++)
 8     {
 9         if(is_prime(i)&&is_prime(i+2))
10         {
11             sum=sum+1;
12             printf("%d %d\n",i,i+2);
13         }
14     }
15     printf("100以内的孪生素数共有%d个",sum);
16     return 0;
17     
18 }
19 int is_prime(int n){
20     int m;
21     for(m=2;m<n;m++)
22     {
23         if(n%m==0){
24             return 0;
25         }
26     }
27     return 1;
28 }
View Code

 

 捕获

试验任务5

1

 1 /******************************************************************************
 2 
 3 Welcome to GDB Online.
 4   GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
 5   C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
 6   Code, Compile, Run and Debug online from anywhere in world.
 7 
 8 *******************************************************************************/
 9 #include <stdio.h>
10 int func(int n, int m);   // 函数声明
11 
12 int main() {
13     int n, m;
14     int ans;
15 
16     while (scanf("%d%d", &n, &m) != EOF) {
17         ans = func(n, m);   // 函数调用
18         printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
19     }
20 
21     return 0;
22 }
23 
24 // 函数定义
25 // 待补足。。。(分别用迭代和递归实现)
26 int func(int n, int m)
27 {
28     int a, shang, xia, sum;
29     xia=1;
30     shang=1;
31     if(m!=0)
32     {for (a = 1; a <= m; a++)
33     {
34         xia *= a;
35         shang*=(n-m+a);
36     }
37     
38     sum = shang / xia;
39     return sum;
40     }
41     else
42     {
43         return 1;
44     }
45 
46 
47 }
View Code

Screenshot_20251025175446

2

 1 /******************************************************************************
 2 
 3 Welcome to GDB Online.
 4   GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
 5   C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
 6   Code, Compile, Run and Debug online from anywhere in world.
 7 
 8 *******************************************************************************/
 9 #include <stdio.h>
10 int func(int n, int m);   // 函数声明
11 
12 int main() {
13     int n, m;
14     int ans;
15 
16     while (scanf("%d%d", &n, &m) != EOF) {
17         ans = func(n, m);   // 函数调用
18         printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
19     }
20 
21     return 0;
22 }
23 
24 // 函数定义
25 // 待补足。。。(分别用迭代和递归实现)
26 int func(int n, int m)
27 {
28     int sum;
29     if(n<m)
30     {
31         return 0;
32     }
33     else if(m==0)
34     {
35         return 1;
36     }
37     else
38     {
39         sum=func(n-1,m)+func(n-1,m-1);
40         return sum;
41     }
42 
43 
44 }
View Code

Screenshot_20251025180103

 试验任务6

 1 /******************************************************************************
 2 
 3 Welcome to GDB Online.
 4   GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
 5   C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
 6   Code, Compile, Run and Debug online from anywhere in world.
 7 
 8 *******************************************************************************/
 9 #include <stdio.h>
10 #include <math.h>
11 
12 // 函数声明
13 // 待补足....
14 int gcd(int a, int b, int c);
15 
16 
17 int main() {
18     int a, b, c;
19     int ans;
20 
21     while (scanf("%d%d%d", &a, &b, &c) != EOF) {
22         ans = gcd(a, b, c);     // 函数调用
23         printf("最大公约数: %d\n\n", ans);
24     }
25 
26     return 0;
27 }
28 
29 
30 // 函数定义
31 // 待补足...
32 int gcd(int a, int b, int c) 
33 {
34     int i;
35     i =fmin(fmin(a, b),c);
36     for (; i > 0; i--)
37     {
38         if (a % i == 0 && b % i == 0 && c % i == 0)
39         {
40             return i;
41         }
42     }
43 }
View Code

Screenshot_20251026132524

试验任务7

 1 /******************************************************************************
 2 
 3 Welcome to GDB Online.
 4   GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
 5   C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
 6   Code, Compile, Run and Debug online from anywhere in world.
 7 
 8 *******************************************************************************/
 9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 // 函数声明
13 // 待补足
14 // xxx
15 void print_charman(int n);
16 
17 int main() {
18     int n;
19 
20     printf("Enter n: ");
21     scanf("%d", &n);
22     print_charman(n); // 函数调用
23        
24     return 0;
25 }
26 
27 // 函数print_charman定义
28 // 待补足
29 // xxx
30 void print_charman(int n)
31 {
32     int i,l,m,x,y;
33     for (i = n; i > 0; i--)
34     {
35         for (y = i; y <n; y++)
36         {
37         printf("\t");
38         }
39         for (l = 2*i-1; l > 0; l--)
40         {
41             printf(" 0\t");
42         }
43         printf("\n");
44         for (y = i; y <n; y++)
45         {
46         printf("\t");
47         }
48         for (m = 2*i-1; m > 0; m--)
49         {
50             printf("<H>\t");
51         }
52         printf("\n");
53         for (y = i; y <n; y++)
54         {
55         printf("\t");
56         }
57         for (x = 2*i-1; x > 0; x--)
58         {
59             printf("I I\t");
60         }
61         printf("\n");
62        
63     }
64 }
View Code

Screenshot_20251026142257

Screenshot_20251026142320

 

总结:在编写时可能有一些细小的改动并不容易;需要一些思维上的逻辑考虑

http://www.hskmm.com/?act=detail&tid=39391

相关文章:

  • ? #5
  • GitLab:代码管理 - 教程
  • 20232302 2025-2026-1《网络与系统攻防技术》实验三实验报告
  • MCP Router使用学习
  • fvm Flutter多版本管理安装与常用指令
  • 人生八要(摘抄)
  • 20232322 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 2025年内窥镜电缆线厂家权威推荐榜:B超线内窥镜电缆线,专业医疗线缆制造与定制化解决方案精选
  • 网络流题单
  • 2025年盐趣科研教育深度解析:从录取数据看科研背景如何撬动名校门槛
  • 2025年10月膜结构厂家推荐榜:双资质企业对比评测 ,
  • 2025年上海久宙集团:深度解析技术护城河与行业话语权
  • 2025 年 10 月门窗十大品牌综合实力权威推荐榜单,聚焦资质、案例、售后的十家机构深度解读
  • 2025 年 10 月门窗十大品牌综合实力权威推荐榜单,高性能,稳定性强的行业优选
  • 2025年唐卡装饰权威深度解析:家装行业新格局和品质承诺
  • 2025年欧那德语深度解析:十二年在线小班模式全透视
  • 2025 年 10 月蒸汽发生器厂家最新推荐,聚焦跨平台能力与售后体系的实用指南
  • 2025年欧那德语:深度解析其在线教学体系与师资配置
  • 2025年欧那德语权威解析:课程体系与师资全景盘点
  • 标签打印服务系统详细设计与实施文档
  • LLM-提示词
  • Transformer基本结构介绍
  • LLM-提示词(二)
  • LangChain简单介绍
  • 图像分类,从构建网络到断点续训,TF1版本实现
  • 图像分类,从网络构建到断点续训,TF2版本实现
  • C0427 【20251025】2025 CSP-J 联测5 总结
  • 20232427 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • (第七次)tensorflow与keras
  • 20232417 2025-2026-1 《网络与系统攻防技术》实验三实验报告