第五章习题

1.习题5.4

有 3 个整数 a、b、c,由键盘输入,输出其中最大的数。

c 复制代码
#include<stdio.h>

void main() {
    int a, b, c, max;
    printf("input a,b,c: \n");
    scanf("%d,%d,%d", &a, &b, &c);

    if (a < b) {
        max = b;
    }else {
        max = a;
    }
    if (max < c) {
        max = c;
    }
    printf("max=%d\n", max);
}

2.习题5.5

有一个函数:

写一段程序,输入想x,输出y。

c 复制代码
#include<stdio.h>

void main() {
    int x, y;
    printf("input x:\n");
    scanf("%d", &x);
    y = x < 1 ? x : x < 10 ? 2 * x - 1 : 3 * x - 11;
    printf("y=%d\n", y);
}

3.习题5.6

c 复制代码
#include<stdio.h>

void main() {
    float score;
    char grade;
    printf("input score:");
    scanf("%f", &score);
    switch ((int) (score / 10)) {
        case 10:
        case 9: grade = 'A';
            break;
        case 8: grade = 'B';
            break;
        case 7: grade = 'C';
            break;
        case 6: grade = 'D';
            break;
        default: grade = 'E';
    }
    printf("score=%f\ngrade=%c\n", score, grade);
}

4.习题5.7

给一个不多于 5 位的正整数,要求:

① 求出它是几位数;

② 分别输出每一位数字;

③ 按逆序输出各位数字,例如原数为 321,应输出 123。

c 复制代码
#include<stdio.h>

void main() {
    long int number;
    int ge, shi, bai, qian, wan, wei;
    printf("input number:\n");
    scanf("%ld", &number);
    wei = number > 9999 ? 5 : number > 999 ? 4 : number > 99 ? 3 : number > 9 ? 2 : 1;
    wan = number / 10000;
    qian = number % 10000 / 1000;
    bai = number % 1000 / 100;
    shi = number % 100 / 10;
    ge = number % 10;
    switch (wei) {
        case 5: printf("%d %d %d %d %d\n", wan, qian, bai, shi, ge);
            printf("逆序:");
            printf("%d%d%d%d%d\n", ge, shi, bai, qian, wan);
            break;
        case 4: printf("%d %d %d %d\n", qian, bai, shi, ge);
            printf("逆序:");
            printf("%d%d%d%d\n", ge, shi, bai, qian);
            break;
        case 3: printf("%d %d %d\n", bai, shi, ge);
            printf("逆序:");
            printf("%d%d%d\n", ge, shi, bai);
            break;
        case 2: printf("%d %d\n", shi, ge);
            printf("逆序:");
            printf("%d%d\n", ge, shi);
            break;
        case 1: printf("%d\n", ge);
            printf("逆序:");
            printf("%d\n", ge);
            break;
    }
}

5.习题5.8

c 复制代码
#include<stdio.h>

void main() {
    long i;
    float bonus, bon1, bon2, bon4, bon6, bon10;
    long c;
    bon1 = 100000 * 0.1;
    bon2 = bon1 + 100000 * 0.075;
    bon4 = bon2 + 200000 * 0.05;
    bon6 = bon4 + 200000 * 0.03;
    bon10 = bon6 + 400000 * 0.015;
    printf("input i:");
    scanf("%ld", &i);
#if(0)
    c = i / 100000;
    if (c > 10) {
        c = 10;
    }

    switch (c) {
        case 0: bonus = i * 0.1;
            break;
        case 1: bonus = bon1 + (i - 100000) * 0.075;
            break;
        case 2:
        case 3: bonus = bon2 + (i - 200000) * 0.05;
            break;
        case 4:
        case 5: bonus = bon4 + (i - 400000) * 0.03;
            break;
        case 6:
        case 7:
        case 8:
        case 9: bonus = bon6 + (i - 600000) * 0.015;
            break;
        case 10:
            bonus = bon10 + (i - 1000000) * 0.01;
    }
#endif
    if (i <= 100000)
        bonus = i * 0.1;
    else if (i <= 200000)
        bonus = bon1 + (i - 100000) * 0.075;
    else if (i <= 400000)
        bonus = bon2 + (i - 200000) * 0.05;
    else if (i <= 600000)
        bonus = bon4 + (i - 400000) * 0.03;
    else if (i <= 1000000)
        bonus = bon6 + (i - 600000) * 0.015;
    else
        bonus = bon10 + (i - 1000000) * 0.01;
    printf("奖金:%f\n", bonus);
}

6.习题5.9

输入4个整数,要求按由小到大的顺序输出

c 复制代码
#include<stdio.h>

void main() {
    int t, a, b, c, d;
    printf("input:\n");
    scanf("%d%d%d%d", &a, &b, &c, &d);
    printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);
    // 选择排序 
    if (a > b) {
        t = a;
        a = b;
        b = t;
    }
    if (a > c) {
        t = a;
        a = c;
        c = t;
    }
    if (a > d) {
        t = a;
        a = d;
        d = t;
    }
    if (b > c) {
        t = b;
        b = c;
        c = t;
    }
    if (b > d) {
        t = b;
        b = d;
        d = t;
    }
    if (c > d) {
        t = c;
        c = d;
        d = t;
    }
    printf("%d-->%d-->%d-->%d", a, b, c, d);
}

6.习题5.10

c 复制代码
#include <stdio.h>

int main() {
    int h = 10;
    float x, y, d1, d2, d3, d4;
    const float r_sq = 1.0; // 半径平方(r=1,r²=1)

    printf("input x y:\n");
    scanf("%f%f", &x, &y);

    // 计算到四个圆心的距离平方
    d1 = (x - 2) * (x - 2) + (y - 2) * (y - 2);    // (2,2)
    d2 = (x + 2) * (x + 2) + (y - 2) * (y - 2);    // (-2,2)
    d3 = (x + 2) * (x + 2) + (y + 2) * (y + 2);    // (-2,-2)
    d4 = (x - 2) * (x - 2) + (y + 2) * (y + 2);    // (2,-2)

    // 只要在任意一个圆内(距离平方 ≤ 半径平方),高度为10
    if (d1 > r_sq && d2 > r_sq && d3 > r_sq && d4 > r_sq) {
        h = 0;
    }

    printf("h=%d\n", h);
    return 0;
}
相关推荐
华清远见成都中心3 小时前
C 语言内存管理深度解析:malloc/free 与嵌入式堆栈分配策略
java·c语言·算法
努力努力再努力wz3 小时前
【MySQL 进阶系列】拒绝滥用root:从 mysql.user 到权限校验,带你彻底理解用户管理与授权机制!
android·c语言·开发语言·数据结构·数据库·c++·mysql
炸膛坦客4 小时前
嵌入式 - 数据结构与算法:(1-4)数据结构 - 单链表的两个核心缺点(引入循环/双向链表)
c语言·数据结构·链表
上弦月-编程6 小时前
高效编程利器:转移表技术解析
c语言·开发语言·数据结构·算法·排序算法
薇茗6 小时前
【初阶数据结构】 左右逢源的分支诗律 二叉树2
c语言·数据结构·算法·二叉树
2301_789015626 小时前
Linux基础指令(一)
linux·运维·服务器·c语言·开发语言·c++·linux指令
ZK_H7 小时前
观星者手记_开发日志1
c语言
zhangrelay7 小时前
三分钟云课实践速通--C/C++程序设计--
linux·c语言·c++·笔记·学习·ubuntu
努力努力再努力wz7 小时前
【MySQL 进阶系列】C/C++ 如何通过客户端库访问 MySQL?从连接原理到 API 调用流程详解(附完整demo代码)
服务器·c语言·数据结构·数据库·c++·b树·mysql