首页 > 其他分享 >实验三

实验三

时间:2024-10-23 20:31:39浏览次数:1  
标签:return int ans score 实验 printf include


task1

源代码

#include <stdio.h>

char score_to_grade(int score);  // 函数声明

int main() {
    int score;
    char grade;

    while(scanf("%d", &score) != EOF) {
        grade = score_to_grade(score);  // 函数调用
        printf("分数: %d, 等级: %c\n\n", score, grade);
    }

    return 0;
}

// 函数定义
char score_to_grade(int score) {
    char ans;

    switch(score/10) {
    case 10:
    case 9:   ans = 'A'; break;
    case 8:   ans = 'B'; break;
    case 7:   ans = 'C'; break;
    case 6:   ans = 'D'; break;
    default:  ans = 'E';
    }

    return ans;
}

运行结果

 

问题1:将分数整除10,判断等第,输入整型,返回字符型。

问题2:有问题,等第全为E,并且会报错,由于将只读字符型赋值给了字符型ans

task2

源代码

#include<stdio.h>

int sum_digits(int n);  // 函数声明

int main() {
    int n;
    int ans;

    while(printf("Enter n: "), scanf("%d", &n) != EOF) {
        ans = sum_digits(n);    // 函数调用
        printf("n = %d, ans = %d\n\n", n, ans);
    }

    return 0;
}

// 函数定义
int sum_digits(int n) {
    int ans = 0;

    while(n != 0) {
        ans += n % 10;
        n /= 10;
    }

    return ans;
}

 

运行结果

问题1:获得各个位上的数字,并相加,获得各位数字之和

问题2:可以实现。一种为迭代写法,另一种为递归写法。

task3

源代码

#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;
    }
}

 运行结果

问题1;power的功能是实现指数运算

问题2;是递归函数。

 

 

 

task4

源代码

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

运行结果

task5

源代码

 

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 void moveplate(unsigned int n, char from, char to) {
 5     printf("%u:%c-->%c\n", n, from, to);
 6 }
 7 void hanoi(unsigned int n, char from, char temp, char to) {
 8     if (n == 1)
 9         moveplate(n, from, to);
10     else {
11         hanoi(n - 1, from, to, temp);
12         moveplate(n, from, to);
13         hanoi(n - 1, temp, from, to);
14     }
15 
16 }
17 int main() {
18     unsigned int n;
19     while (scanf_s("%u", &n) != EOF) {
20         int m = 1;
21         hanoi(n, 'A', 'B', 'c');
22         m = pow(2, n);
23         printf("一共一移动了%d次\n", m-1);
24     }
25     system("pause");
26     return 0;
27 }

运行结果

task6

源代码(迭代)

 1 #include<stdio.h>
 2 int func(int n,int m){
 3     int a=1,b=1,c=1;
 4     for(int i=1;i<=n;i++){
 5         a=a*i;
 6     }
 7     for(int i=1;i<=m;i++){
 8         b=b*i;
 9     }
10     for(int i=1;i<=n-m;i++){
11         c=c*i;
12     }
13     return a/(c*b);
14 }
15 int main(){
16     int n,m;
17     int ans;
18     while(scanf("%d%d",&n,&m)!=EOF){
19         ans=func(n,m);
20         printf("n=%d,m=%d,ans=%d\n\n",n,m,ans);
21     }
22     return 0;
23 }

运行结果

源代码(递归)

 1 #include<stdio.h>
 2 int func(int n,int m){
 3     if(m>n)
 4     return 0;
 5     if(m==0||n==m)
 6     return 1;
 7     else{
 8         return func(n-1,m)+func(n-1,m-1);
 9     }
10 }
11 int main(){
12     int n,m;
13     int ans;
14     while(scanf("%d%d",&n,&m)!=EOF){
15         ans=func(n,m);
16         printf("n=%d,m=%d,ans=%d\n\n",n,m,ans);
17     }
18     return 0;
19 }

运行结果

task7

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 void print_charman(int n);
 4 int main(){
 5     int n;
 6     printf("Enter n:");
 7     scanf("%d",&n);
 8     print_charman(n);
 9 }
10 void print_charman(int n){
11     int m=0;
12     for(int i=n;i>=1;i--){
13         for(int j=0;j<m;j++){
14             printf("\t");
15         }
16         
17         for(int j=0;j<2*i-1;j++){
18             printf(" o\t");
19         }
20         printf("\n");
21         for(int j=0;j<m;j++){
22             printf("\t");
23         }
24         
25         for(int j=0;j<2*i-1;j++){
26             printf("<H>\t");
27         }
28 
29         printf("\n");
30         for(int j=0;j<m;j++){
31             printf("\t");
32         }
33         for(int j=0;j<2*i-1;j++){
34             printf("I I\t");
35         }
36         printf("\n");
37        
38         m++;
39     }
40 }

运行结果

 

标签:return,int,ans,score,实验,printf,include
From: https://www.cnblogs.com/hh0417blogs/p/18497831

相关文章

  • 实验二 类和对象_基础编程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)例举你能想到的一个后门进入到你系统中的可能......
  • 实验2 类和对象_基础编程1
    实验任务1代码:t.h:1#pragmaonce23#include<string>45classT{6public:7T(intx=0,inty=0);8T(constT&t);9T(T&&t);10~T();11voidadjust(intratio);12voiddisplay()const;13private......
  • 实验2 类和对象_基础编程1
    task1: t.h:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数void......
  • 实验三
    任务一验证性实验源码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:......