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;
    }
}
相关推荐
聚客AI16 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v19 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工21 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
moonlifesudo1 天前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法