首页 > 其他分享 >实验3

实验3

时间:2024-10-25 17:34:23浏览次数:1  
标签:return int printf char 实验 ans include

task.1 代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
char score_to_grade(int x);
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'; break;
    }
    return ans;
}

图片:

问题一:功能:将输入分数转化为相应等级返回,形参类型:int,返回值:char

问题二:有问题,当case进行结果时不会停止,导致ans值总以E返回

task.2

代码:

#include<stdio.h>
int sum_digits(int x);
int main()
{
    int x,y;
    while(printf("Enter n:"),scanf("%d",&x)!=EOF)
    {
        y=sum_digits(x);
        printf("x=%d,y=%d\n",x,y);
    }
    return 0;
}

int sum_digits(int x) {
    int ans=0;
    while(x!=0)
    {
        ans+=x%10;
        x/=10;
    }
    return ans;
}

图片

问题一:将输入数字每一分位上的数字相加

问题二:能,利用递归函数不断将数字每一分位数字剥离相加

task.3

代码:

#include<stdio.h>
int power(intx, inty);
int main()
{
    int x, n, 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(x, 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:将x进行n次方相乘

问题2:是,y=x^n

task.4

代码:

#include<stdio.h>
#include<stdlib.h>
int is_prime(int x);
int main()
{
    printf("100以内的素数:\n");
    int x = 1, y = 3, ans1, ans2, s = 0;
    while (x <= 100)
    {
        ans1 = is_prime(x);
        ans2 = is_prime(y);
        if (ans1 == 1 && ans2 == 1)
        {
            printf("%d %d\n", x, y);
            s++;
        }
        x++;
        y++;
    }
            
    printf("100以内的孪生素数有:%d", s);
    return 0;
}
int is_prime(int x)
{
    int n = 2;
    if (x == 1)
        return 0;
    while (n <= x / 2)
    {
        if (x % n != 0)
            n++;
        else
        {
            return 0;
            break;
        }
    }
    return 1;
}

图片

task.5

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void move(int n, char from, char to);
void hanoi(int n, char from, char temp, char to);
int s = 0;
int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        hanoi(n, 'A', 'B', 'C');
        printf("一共移动了%d次", s);
    }
    return 0;
}
void hanoi(int n, char from, char temp, char to)
{
    if (n == 1)
    {
        move(n, from, to);
    }
    else
    {
        hanoi(n - 1, from, to, temp);
        move(n, from, to);
        hanoi(n - 1, temp, from, to);
    }
    
}
void move(int n, char from, char to)
{
    printf("%d:%c->%c\n", n, from, to);
    s++;
}

task.6

代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int func(int n, int m);
int main() {
    int n, m;
    int ans;
    while (scanf("%d%d", &n, &m) != EOF) {
        ans = func(n, m); 
        printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
    }
    return 0;
}
int func(int n, int m)
{
    int s;
    int t = n - m + 1;
    for (s = 1; n >= t; n--)
        s *= n;

    for (; m >= 1; m--)
        s /= m;
    return s;
}
/*int func(int n, int m)
{
    int s = 0;
    if (m > n)
        s = 0;
    else if (m == n)
        s = 1;    
    else if (m > 0 && n > 1)
        s = func(n - 1, m) + func(n - 1, m - 1);
    else
        s = 1;
    return s;
}
*/

图片

task.7

代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void print_charman(int n);
int a;
int main() {
    int n;
    printf("Enter n: ");
    scanf("%d", &n);
    a = n;
    print_charman(n); 
    return 0;
}
void print_charman(int n)
{
    if (n > 0)
    {
        int s, z;
        for (s = 2 * n - 1; s > 0; s--)
            printf(" O \t");
        printf("\n");
        for (z = a - n ; z > 0; z--)
            printf("\t");
        for (s = 2 * n - 1; s > 0; s--)
            printf("<H>\t");
        printf("\n");
        for (z = a - n; z > 0; z--)
            printf("\t");
        for (s = 2 * n - 1; s > 0; s--)
            printf("I I\t");
        printf("\n\n");
        for (z=a-n+1; z > 0; z--)
            printf("\t");
        print_charman(n - 1);
    }
}

图片

 

标签:return,int,printf,char,实验,ans,include
From: https://www.cnblogs.com/frogwithtrousers/p/18499749

相关文章

  • 实验三
    任务一源代码1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;6while(scanf("%d",&score)!=EOF){7grade=score_to_grade(score);8printf("分数:%d,等级:%c\......
  • 实验3
    任务11#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf(&......
  • 实验2 类和对象_基础编程1
    实验任务一代码t.h1#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12......
  • 算法设计实验6
    p1249有一个8*8的棋盘,行号、列号均为0-7,一个特殊放个的位置是(5,6),给出采用L形骨牌覆盖其他全部方格的一种方案1#include<ostream>2#include<iostream>3#defineMAX_SIZE84usingnamespacestd;5intk;6intx,y;7intboard[MAX_SIZE][MAX_SIZE];8int......
  • 实验三 C语言函数应用编程
    一、实验目的 能正确使用C语法规则定义,声明,调用函数能正确编写递归函数针对具体问题场景,合理抽象出独立的功能模块,正确定义函数并使用,使得代码更具可读性,可维护性针对具体问题场景,能正确,合理使用全局变量和局部static变量,解决实际问题二、实验准备 1,函数定义,声明,调用的语......
  • 实验3:工厂方法模式
    [实验任务一]:加密算法目前常用的加密算法有DES(DataEncryptionStandard)和IDEA(InternationalDataEncryptionAlgorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。实验要求:1.画出对应的类图;2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相......
  • 20222326 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    1.实验内容实验具体内容正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧正确使用msf编码器,使用msfvenom生成如jar之类的其他文件veil,加壳工具使用C+shellcode编程通过组合应用各种技术实现恶意代码免杀用另一电脑实测,在杀软开启的情况下,可......
  • kali一句话木马实验——远程控制(保姆级教程,新手小白也可以操作)
    1.打开apache;在终端中使用管理员权限输入serviceapache2start注:这个是为了打开80端口;打开之后可以使用nmapIP扫描查看80端口是否开放输入虚拟机IP地址,可以在物理机或者虚拟机浏览器打开apache;2.输入ifconfig查看虚拟机IP;3.在桌面找到home,在里面FileSystem目录下找......
  • 实验二
    任务一:1t.h2#pragmaonce34#include<string>56//类T:声明7classT{8//对象属性、方法9public:10T(intx=0,inty=0);//普通构造函数11T(constT&t);//复制构造函数12T(T&&t);//移动构造函......
  • YOLOv11全网最新创新点改进系列:一文掌握YOLOv11评估指标,学会判断实验是否达到发文水平
    YOLOv11全网最新创新点改进系列:一文掌握YOLOv11评估指标,学会判断实验是否达到发文水平!所有改进代码均经过实验测试跑通!截止发稿时YOLOv10已改进40+!自己排列组合2-4种后,考虑位置不同后可排列组合上千万种!改进不重样!!专注AI学术,关注B站up主:Ai学术叫叫兽er!购买相关资料后畅享......