C语言程序设计(第5版)习题解答-第4章

第4章

  1. 输出三个整数中的最大值
    代码如下
c 复制代码
#include <stdio.h>
int main()
{
    int a, b, c;
    int temp, max;
    printf("a = ");
    scanf("%d", &a);
    printf("b = ");
    scanf("%d", &b);
    printf("c = ");
    scanf("%d", &c);
    temp = a > b ? a : b;
    max = temp > c ? temp : c;
    printf("max = %d\n", max);
    return 0;
}
  1. 大赛
    代码如下
c 复制代码
#include <stdio.h>
#include <math.h>
#define MAX 1000
int main()
{
    int num, sqrt_num;
    printf("num = ");
    scanf("%d", &num);
    while (num > MAX || num < 0)
    {
        printf("error, input again: \n");
        scanf("%d", &num);
    }
    sqrt_num = sqrt(num);
    printf("sqrt_num = %d\n", sqrt_num);
    return 0;
}
  1. 程序描述分段函数
    代码如下
c 复制代码
#include <stdio.h>
int main()
{
    int x, y;
    printf("x = ");
    scanf("%d", &x);
    if(x < 1)
    {
        y  = x;
        printf_s("x = %3d, y = %3d\n", x, y);
    }
    else if (x < 10)
    {
        y = 2 * x - 1;
        printf_s("x = %3d, y = %3d\n", x, y);
    }
    else
    {
        y = 3 * x - 11;
        printf_s("x = %3d, y = %3d\n", x, y);
    }
    
    return 0;
}
  1. 对比两个程序
    程序代码如下
c 复制代码
/* 代码1 */
#include <stdio.h>
int main()
{
    int x, y;
    printf_s("x = ");
    scanf("%d", &x);
    y = -1;
    if(x != 0)
    {
        if(x > 0)
        {
            y = 1;
        }
    }
    else
    {
        y = 0;
    }
    printf_s("y = %d\n", y);
    return 0;
}

/* 代码2 */
int main()
{
    int x, y;
    printf_s("x = ");
    scanf("%d", &x);
    y = 0;
    if(x >= 0)
    {
        if(x > 0)
        {
            y = 1;
        }
    }
    else
    {
        y = -1;
    }
    printf_s("y = %d\n", y);
    return 0;
}

程序1不满足题目要求,输入x<0时,输出y=0;程序2不满足题目要求,输入x<0时,输出y=0

  1. 对成绩进行分级
    代码如下
c 复制代码
#include <stdio.h>
int main()
{
    float score;
    char grade;
    printf("score = ");
    scanf("%f", &score);
    while (score < 0 || score > 100)
    {
        printf("error, input again: \n");
        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';
            break;
    }
    printf("grade = %c\n", grade);
    return 0;
}
  1. 判断数据
    代码如下
c 复制代码
int main()
{
    int num, num_place;
    int num_1, num_2, num_3, num_4, num_5;
    printf_s("num = ");
    scanf("%d", &num);
    if(num > 9999)
    {
        num_place = 5;
    }
    else if(num > 999)
    {
        num_place = 4;
    }
    else if(num > 99)
    {
        num_place = 3;
    }
    else if(num > 9)
    {
        num_place = 2;
    }
    else
    {
        num_place = 1;
    }
    num_1 = num % 10;
    num_2 = num % 100 / 10;
    num_3 = num % 1000 / 100;
    num_4 = num % 10000 / 1000;
    num_5 = num / 10000;
    printf("num_1 = %d\n", num_1);
    printf("num_2 = %d\n", num_2);
    printf("num_3 = %d\n", num_3);
    printf("num_4 = %d\n", num_4);
    printf("num_5 = %d\n", num_5);
    printf("num_place = %d\n", num_place);
    switch(num_place)
    {
        case 5:
            printf("%d, %d, %d, %d, %d\n", num_5, num_4, num_3, num_2, num_1);
            printf_s("\n reverse order: ");
            printf("%d, %d, %d, %d, %d\n", num_1, num_2, num_3, num_4, num_5);
            break;
        case 4:
            printf("%d, %d, %d, %d\n", num_4, num_3, num_2, num_1);
            printf_s("\n reverse order: ");
            printf("%d, %d, %d, %d\n", num_1, num_2, num_3, num_4);
            break;
        case 3:
            printf("%d, %d, %d\n", num_3, num_2, num_1);
            printf_s("\n reverse order: ");
            printf("%d, %d, %d\n", num_1, num_2, num_3);
            break;
        case 2:
            printf("%d, %d\n", num_2, num_1);
            printf_s("\n reverse order: ");
            printf("%d, %d\n", num_1, num_2);
            break;
        case 1:
            printf("%d\n", num_1);
            printf_s("\n reverse order: ");
            printf("%d\n", num_1);
            break;
        default:
            break;
    }
    return 0;
}
  1. 企业发放奖金提成
    代码如下
c 复制代码
// if语句
double get_bonus_if(int profit)
{
    double bonus, bon1, bon2, bon4, bon6, bon10;
    bon1 = 100000 * 0.1;
    bon2 = bon1 + 100000 * 0.075;
    bon4 = bon2 + 100000 * 0.05;
    bon6 = bon4 + 100000 * 0.03;
    bon10 = bon6 + 400000 * 0.015;
    if (profit <= 100000)
    {
        bonus = profit * 0.1;
    }
    else if (profit <= 200000)
    {
        bonus = bon1 + (profit - 100000) * 0.075;
    }
    else if (profit <= 400000)
    {
        bonus = bon2 + (profit - 200000) * 0.05;
    }
    else if (profit <= 600000)
    {
        bonus = bon4 + (profit - 400000) * 0.03;
    }
    else if (profit <= 1000000)
    {
        bonus = bon6 + (profit - 600000) * 0.015;
    }
    else
    {
        bonus = bon10 + (profit - 1000000) * 0.01;
    }
    return bonus;
}

// switch语句
double get_bonus_switch(int profit)
{
    double bonus;
    int branch = profit / 100000;
    if (branch > 10)
        branch = 10;
    switch (branch)
    {
    case 0:
        bonus = profit * 0.1;
        break;
    case 1:
        bonus = 100000 * 0.1 + (profit - 100000) * 0.075;
        break;
    case 3:
        bonus = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05;
        break;
    case 5:
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03;
        break;
    case 9:
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015;
        break;
    case 10:
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01;
        break;
    }
    return bonus;
}
#include "funcs.h"

int main()
{
    printf_s("the bonus is:%.2f\n", get_bonus_if(234000));
    printf_s("the bonus is:%.2f\n", get_bonus_switch(234000));
    return 0;
}
  1. 四个整数排序
    代码如下
c 复制代码
// 采用依次比较法
void sort_fornums(int a, int b, int c, int d)
{
    int temp;
    if (a > b)
    {
        temp = a;
        a = b;
        b = temp;
    }
    if (c > d)
    {
        temp = c;
        c = d;
        d = temp;
    }
    if (a > c)
    {
        temp = a;
        a = c;
        c = temp;
    }
    if (b > d)
    {
        temp = b;
        b = d;
        d = temp;
    }
    if (b > c)
    {
        temp = b;
        b = c;
        c = temp;
    }
    printf("%d %d %d %d\n", a, b, c, d);
}
  1. 求建筑高度
    代码如下
c 复制代码
// 根据该点到圆心的距离进行判断
int get_height(int x, int y)
{
    int x1 = 2, y1 = 2, x2 = -2, y2 = 2, x3 = 2, y3 = -2, x4 = -2, y4 = -2;
    float d1, d2, d3, d4;
    d1 = (x - x1) * (x - x1) + (y - y1) * (y - y1);
    d2 = (x - x2) * (x - x2) + (y - y2) * (y - y2);
    d3 = (x - x3) * (x - x3) + (y - y3) * (y - y3);
    d4 = (x - x4) * (x - x4) + (y - y4) * (y - y4);
    if (d1 > 1 && d2 > 1 && d3 > 1 && d4 > 1)
    {
        return 0;
    }
    else
    {
        return 10;
    }
}
相关推荐
雾月5511 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
杜小暑1 小时前
动态内存管理
c语言·开发语言·动态内存管理
YuforiaCode1 小时前
第十二届蓝桥杯 2021 C/C++组 直线
c语言·c++·蓝桥杯
知来者逆2 小时前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊2 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北2 小时前
力扣-160.相交链表
算法·leetcode·链表
拾忆-eleven2 小时前
C语言实战:用Pygame打造高难度水果消消乐游戏
c语言·python·pygame
爱研究的小陈3 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing3 小时前
二叉树的最大宽度计算
算法·面试
再睡一夏就好3 小时前
Linux常见工具如yum、vim、gcc、gdb的基本使用,以及编译过程和动静态链接的区别
linux·服务器·c语言·c++·笔记