• 集美大学 - 2840 - 实验10和11 - 编程题


    实验10-1 圆形体体积计算器

    本题要求实现一个常用圆形体体积的计算器。计算公式如下:

    • 球体体积 V = 4 3 π r 3 V=\frac{4}{3}\pi r^3 V=34πr3,其中r是球体半径。
    • 圆柱体体积 V = π r 2 h V=\pi r^2h V=πr2h,其中r是底圆半径,h是高。
    • 圆锥体体积 V = 1 3 π r 2 h V=\frac{1}{3}\pi r^2 h V=31πr2h,其中r是底圆半径,h是高。

    输入格式:
    在每次计算之前,要求输出如下界面:

    1-Ball
    2-Cylinder
    3-Cone
    other-Exit
    Please enter your command:
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后从标准输入读进一个整数指令。

    输出格式:
    如果读入的指令是1或2或3,则执行相应的体积计算;如果是其他整数,则程序结束运行。

    • 当输入为1时,在计算球体体积之前,打印Please enter the radius:,然后读入球体半径,完成计算;
    • 当输入为2时,在计算圆柱体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算;
    • 当输入为3时,在计算圆锥体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算。

    计算结果在一行内输出,保留小数点后两位。

    输入样例:

    1
    2
    3
    2.4 3
    0
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出样例:

    1-Ball
    2-Cylinder
    3-Cone
    other-Exit
    Please enter your command:
    Please enter the radius:
    33.51
    1-Ball
    2-Cylinder
    3-Cone
    other-Exit
    Please enter your command:
    Please enter the radius and the height:
    18.10
    1-Ball
    2-Cylinder
    3-Cone
    other-Exit
    Please enter your command:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    #include
    
    #define pi 3.1415926535
    
    int main() {
        int x;
        double r;
        double h;
        printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
        while (scanf("%d", &x) != EOF) {
            if (!(x == 1 || x == 2 || x == 3)) break;
            if (x == 1) {
                printf("Please enter the radius:\n");
                scanf("%lf", &r);
                printf("%.2f\n", 4 * pi * r * r * r / 3);
            } else if (x == 2) {
                printf("Please enter the radius and the height:\n");
                scanf("%lf %lf", &r, &h);
                printf("%.2f\n", pi * r * r * h);
            } else if (x == 3) {
                printf("Please enter the radius and the height:\n");
                scanf("%lf %lf", &r, &h);
                printf("%.2f\n", pi * r * r * h / 3);
            }
            printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    实验11-1-1-字符串/冒泡排序 英文单词排序

    本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。

    输入格式:
    输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。

    输出格式:
    输出为排序后的结果,每个单词后面都额外输出一个空格。

    输入样例:

    blue
    red
    yellow
    green
    purple
    #
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出样例:

    red blue green yellow purple 
    
    • 1
    #include
    
    int zishu(char *s) {
        int cnt = 0;
        while (s[cnt] != '\0') cnt++;
        return cnt;
    }
    
    int main() {
        char s[20][11], ch;
        int i = 0, temp, j;
        scanf("%s", &s[i]);
        while (s[i][0] != '#') {
            i++;
            scanf("%s", &s[i]);
        }
        temp = i;
        for (i = 1; i < 21; i++) {
            for (j = 0; j < temp; j++) {
                if (zishu(s[j]) == i) printf("%s ", s[j]);
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    实验11-1-7-字符串 藏头诗

    本题要求编写一个解密藏头诗的程序。

    注:在 2022 年 7 月 14 日 16 点 50 分以后,该题数据修改为 UTF-8 编码。

    输入格式:
    输入为一首中文藏头诗,一共四句,每句一行。注意:一个汉字占三个字节。

    输出格式:
    取出每句的第一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。

    输入样例:

    一叶轻舟向东流
    帆稍轻握杨柳手
    风纤碧波微起舞
    顺水任从雅客流
    
    • 1
    • 2
    • 3
    • 4

    输出样例:

    一帆风顺
    
    • 1
    #include
    
    int main() {
        char s[4][100];
        int i;
        for (i = 0; i < 4; i++) {
            scanf("%s", &s[i]);
        }
        for (i = 0; i < 4; i++) {
            printf("%c%c", s[i][0], s[i][1]);
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    实验11-1-9-字符串 藏尾诗

    本题要求编写一个解密藏尾诗的程序。

    输入格式:
    输入为一首中文藏尾诗,一共四句。每句一行,但句子不一定是等长的,最短一个汉字,最长九个汉字。注意:一个汉字占两个字节。

    输出格式:
    取出每句的最后一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。

    输入样例:

    悠悠田园风
    然而心难平
    兰花轻涌浪
    兰香愈幽静
    
    • 1
    • 2
    • 3
    • 4

    输出样例:

    风平浪静
    
    • 1
    #include
    
    int zishu(char *a) {
        int cnt = 0;
        while (a[cnt] != '\0') cnt++;
        return cnt;
    }
    
    int main() {
        char s[4][20];
        int b[4];
        int i;
        for (i = 0; i < 4; i++) scanf("%s", &s[i]);
        for (i = 0; i < 4; i++) b[i] = zishu(s[i]);
        for (i = 0; i < 4; i++) printf("%c%c", s[i][b[i] - 2], s[i][b[i] - 1]);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    结构 复数四则运算

    本题要求编写程序,计算2个复数的和、差、积、商。

    输入格式:
    输入在一行中按照a1 b1 a2 b2的格式给出2个复数C1=a1+b1i和C2=a2+b2i的实部和虚部。题目保证C2不为0。

    输出格式:
    分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果的格式顺序输出2个复数的和、差、积、商,数字精确到小数点后1位。如果结果的实部或者虚部为0,则不输出。如果结果为0,则输出0.0。

    输入样例1:

    2 3.08 -2.04 5.06
    
    • 1

    输出样例1:

    (2.0+3.1i) + (-2.0+5.1i) = 8.1i
    (2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
    (2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
    (2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i
    
    • 1
    • 2
    • 3
    • 4

    输入样例2:

    1 1 -1 -1.01
    
    • 1

    输出样例2:

    (1.0+1.0i) + (-1.0-1.0i) = 0.0
    (1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i
    (1.0+1.0i) * (-1.0-1.0i) = -2.0i
    (1.0+1.0i) / (-1.0-1.0i) = -1.0
    
    • 1
    • 2
    • 3
    • 4
    #include
    #include
    
    void print(double a, double b) {       //打印结果
        if (fabs(a) >= 0.05) {        //由于结果是保存一位小数,这里的判断表达式就需要用这个
            printf("%.1lf", a);
            if (fabs(b) < 0.05) {        //要注意这里的格式,测试点3、4
                printf("\n");
            }
        }
        if (fabs(b) >= 0.05) {
            if (b > 0.0 && fabs(a) >= 0.05) {        //虚部>0且实部不为0时,输出正号
                printf("+");
            }
            printf("%.1lfi\n", b);
        }
        if (fabs(a) < 0.05 && fabs(b) < 0.05) {        //四舍五入都为0时输出0
            printf("0.0\n");
        }
    }
    
    void prin(double a1, double b1, double a2, double b2, char ch) {   //打印表达式
        printf("(%.1lf", a1);
        if (b1 >= 0) {            //虚部大于0时要人为的加一个‘+’
            printf("+");
        }
        printf("%.1lfi)", b1);
        printf(" %c ", ch);            //ch是传入的运算符号,这样每种运算都可以直接调用函数
        printf("(%.1lf", a2);
        if (b2 >= 0) {
            printf("+");
        }
        printf("%.1lfi) = ", b2);
    }
    
    int main() {
        double a1, b1, a2, b2;
        scanf("%lf %lf %lf %lf", &a1, &b1, &a2, &b2);
        double real, vir;      //实部,虚部
        //加法
        prin(a1, b1, a2, b2, '+');
        real = a1 + a2;
        vir = b1 + b2;
        print(real, vir);
    
        //减法
        prin(a1, b1, a2, b2, '-');
        real = a1 - a2;
        vir = b1 - b2;
        print(real, vir);
    
        //乘法
        prin(a1, b1, a2, b2, '*');
        real = a1 * a2 - b1 * b2;
        vir = b1 * a2 + a1 * b2;
        print(real, vir);
    
        //除法
        prin(a1, b1, a2, b2, '/');
        real = (a1 * a2 + b1 * b2) / (a2 * a2 + b2 * b2);
        vir = (b1 * a2 - a1 * b2) / (a2 * a2 + b2 * b2);
        print(real, vir);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    for 统计素数并求和

    本题要求统计给定整数M和N区间内素数的个数并对它们求和。

    输入格式:
    输入在一行中给出两个正整数M和N(1≤M≤N≤500)。

    输出格式:
    在一行中顺序输出M和N区间内素数的个数以及它们的和,数字间以空格分隔。

    输入样例:

    10 31
    
    • 1

    输出样例:

    7 143
    
    • 1
    #include
    #include
    
    int sushu(int x) {
        int i;
        if (x == 1) return 0;
        else if (x == 2) return 1;
        else {
            for (i = 2; i <= sqrt(x); i++) {
                if (x % i == 0) return 0;
            }
            return 1;
        }
    }
    
    int main() {
        int i, m, n;
        scanf("%d %d", &m, &n);
        int sum = 0, cnt = 0;
        for (i = m; i <= n; i++) {
            if (sushu(i) == 1) {
                cnt++;
                sum = sum + i;
            }
        }
        printf("%d %d", cnt, sum);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    嵌套循环 支票面额

    一个采购员去银行兑换一张y元f分的支票,结果出纳员错给了f元y分。采购员用去了n分之后才发觉有错,于是清点了余额尚有2y元2f分,问该支票面额是多少?

    输入格式:
    输入在一行中给出小于100的正整数n。

    输出格式:
    在一行中按格式y.f输出该支票的原始面额。如果无解,则输出No Solution。

    输入样例1:

    23
    
    • 1

    输出样例1:

    25.51
    
    • 1

    输入样例2:

    22
    
    • 1

    输出样例2:

    No Solution
    
    • 1
    #include
    
    int main() {
        int n;
        scanf("%d", &n);
        int y, f, cnt = 0;
        for (y = 0; y < 100; y++) {
            for (f = 0; f < 100; f++) {
                if (200 * y + 2 * f + n == 100 * f + y) {
                    cnt++;
                    printf("%d.%d", y, f);
                    break;
                }
            }
        }
        if (cnt == 0) printf("No Solution");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    嵌套循环 求特殊方程的正整数解

    本题要求对任意给定的正整数N,求方程 X 2 + Y 2 = N X^2+Y^2=N X2+Y2=N的全部正整数解。

    输入格式:
    输入在一行中给出正整数N(≤10000)。

    输出格式:
    输出方程 X 2 + Y 2 = N X^2+Y^2=N X2+Y2=N的全部正整数解,其中X≤Y。每组解占1行,两数字间以1空格分隔,按X的递增顺序输出。如果没有解,则输出No Solution

    输入样例1:

    884
    
    • 1

    输出样例1:

    10 28
    20 22
    
    • 1
    • 2

    输入样例2:

    11
    
    • 1

    输出样例2:

    No Solution
    
    • 1
    #include
    
    int main() {
        int n, i, j, cnt = 0;
        scanf("%d", &n);
        for (i = 1; i < 100; i++) {
            for (j = 1; j < 100; j++) {
                if ((i <= j) && (i * i + j * j == n)) {
                    printf("%d %d\n", i, j);
                    cnt++;
                }
            }
        }
        if (cnt == 0) printf("No Solution");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    vuedraggable影响点击事件的解决办法
    Java时间Api
    java毕业生设计新闻采编系统管理系统计算机源码+系统+mysql+调试部署+lw
    关于Python Ansible中 HOST_KEY_CHECKING不生效问题分析
    Cmake输出git内容方式
    Android终端硬件通讯总结(串口通讯、Usb Com、Usb、蓝牙、Wifi)
    【控制台】 \xce\xde\xb7\xa8\xb4\xf2\xbf\xaa
    react路由的匹配模式(模糊匹配、精确匹配)
    【计算机网络】传输层(四)—— TCP 可靠传输、流量控制、拥塞控制
    Ubuntu 下C++数字雨
  • 原文地址:https://blog.csdn.net/im34v/article/details/126557140