首页 > 其他分享 >实验3

实验3

时间:2024-10-23 20:47:57浏览次数:1  
标签:10 return int printf char 实验 ans

任务1

源代码:

task1.c

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

 

 

运行结果:

问题1:

功能是将分数转换成一个描述等第的字符。形参是int类型。返回值是char字符类型

问题2:

有问题。一是在6,7,8,9的case中尝试把字符串赋给字符型变量ans,二是在6,7,8,9的case中没有break;语句

 

任务2

源代码:

task2.c

 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 int sum_digits(int n) {
18     int ans = 0;
19 
20     while(n != 0) {
21         ans += n % 10;
22         n /= 10;
23     }
24 
25     return ans;
26 }

 

运行结果:

问题1:

从整数n的个位到最高位上的数累加并返回

问题2:

能实现。一种通过循环迭代将每个数位取余出来累加;一种通过递归一层层除10,直到个位再层层返回累加。

 

任务3

源代码:

task3.c

 1 #include <stdio.h>
 2 
 3 int power(int x, int n);
 4 
 5 int main() {
 6     int x, n;
 7     int ans;
 8 
 9     while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) {
10         ans = power(x, n); 
11         printf("n = %d, ans = %d\n\n", n, ans);
12     }
13     
14     return 0;
15 }
16 
17 int power(int x, int n) {
18     int t;
19 
20     if(n == 0)
21         return 1;
22     else if(n % 2)
23         return x * power(x, n-1);
24     else {
25         t = power(x, n/2);
26         return t*t;
27     }
28 }

 

运行结果:

问题1:

计算任意int整数的非负整数次方的值

问题2:

是递归函数。其递归模式是先对n进行分类,若为0则直接返回1;为奇数则先提出一个x,调用自身计算x的n-1次方,即偶数次方;为偶数则调用自身计算x的n/2次方,再在层层返回时进行平方,减少递归层数。

数学公式模型:

 

任务4

源代码:

task4.c

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

运行结果:

 

任务5

源代码:

task5.c

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

运行结果:

任务6

源代码:

task6_1.c

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

 

task6_2.c

 1 #include <stdio.h>
 2 int func(int n, int m);   // 函数声明
 3 
 4 int main() {
 5     int n, m;
 6     int ans;
 7 
 8     while(scanf("%d%d", &n, &m) != EOF) {
 9         ans = func(n, m);   // 函数调用
10         printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
11     }
12         
13     return 0;
14 }
15 
16 // 函数定义
17 //递归 
18 int func(int n, int m)
19 {
20     if(m > n)
21     return 0;
22     
23     if(n%2 == 0&&m > n/2)
24     m = n-m;
25     else if(n%2 != 0&&m > (n-1)/2)
26     m = n-m;
27     
28     if(m == 0)
29     return 1;
30     else if(m == 1)
31     return n;
32     else
33     return func(n-1, m)+func(n-1, m-1);
34 }

 

运行结果:

task6_1.png

task6_2.png

 

任务7

源代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 // 函数声明
 5 // 待补足
 6 void print_charman(int n);
 7 
 8 int main() {
 9     int n;
10 
11     printf("Enter n: ");
12     scanf("%d", &n);
13     print_charman(n); // 函数调用
14        
15     return 0;
16 }
17 
18 // 函数print_charman定义
19 // 待补足
20 void print_charman(int n)
21 {
22     int i;
23     int j;
24     for(i = n;i >= 1;i --)
25     {
26         int a;
27         for(a = 1;a <= n-i;a ++)
28         printf("\t");
29         for(j = 1; j <= 2*i-1;j ++)
30         printf(" O \t");
31         printf("\n");
32         for(a = 1;a <= n-i;a ++)
33         printf("\t");
34         for(j = 1; j <= 2*i-1;j ++)
35         printf("<H>\t");
36         printf("\n");
37         for(a = 1;a <= n-i;a ++)
38         printf("\t");
39         for(j = 1; j <= 2*i-1;j ++)
40         printf("I I\t");
41         printf("\n");
42     }
43 }

 

运行结果:

 

标签:10,return,int,printf,char,实验,ans
From: https://www.cnblogs.com/yucyi/p/18498322

相关文章

  • 20222407 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    一、实验内容本次实验的目标在于运用多重加密、文件格式伪装、数据填充、加壳等技术方法达成恶意代码的免杀效果,生成恶意程序,并对其进行测试,以检验其能否成功躲避杀毒软件的检测。本次实验具体内容如下:1.正确使用msf编码器,使用msfvenom生成如jar之类的其他文件;2.能够使用veil,加壳......
  • 实验3
    任务1 源代码1#include<stdio.h>23charscore_to_grade(intscore);//函数声明45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);//函数调用......
  • 实验三
    task1源代码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,......
  • 实验二 类和对象_基础编程1
    实验任务一:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数voidadju......
  • 实验2
    task11#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12~T();......
  • # 20222402 2024-2025-1 《网络与系统攻防技术》实验二实验报告
    1.实验内容本周学习内容①Shellcode技术②后门概念:后门就是不经过正常认证流程而访问系统的通道。③后门案例:XcodeGhost等。④后门技术:狭义后门:特指潜伏于操作系统中专门做后门的一个程序,“坏人”可以连接这个程序,远程执行各种指令。管控功能实现技术自启动技术进程隐藏技......
  • 实验2:简单工厂模式
    [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。实验要求:1.画出对应的类图;2.提交源代码;3.注意编程规范。类图:  2、代......
  • 实验3
    task1点击查看代码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%......
  • 实验2:简单工厂模式
    [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。  1. 类图   2.源代码//抽象产品类:Person接口publicinterfaceP......
  • 20222404 2024-2025-1《网络与系统攻防》 实验二
    1.实验内容(一)本周课程内容了解后门概念,了解后门案例,后门会对系统安全造成的影响。对后门技术进行普及,包括各种进程隐藏技术。了解netcat、meterpreter,veil等常见工具。进一步学习shellcode注入的逻辑和多种情况。(二)问题回答(1)例举你能想到的一个后门进入到你系统中的可能......