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;
    }
}
相关推荐
wjm0410062 小时前
贪心算法概述
算法·贪心算法
小白的登仙路2 小时前
进程间通讯
linux·c语言·进程间通讯
我搞slam2 小时前
全覆盖路径规划算法之BCD源码实现(The Boustrophedon Cellular Decomposition)
c++·算法·图搜索算法
Rossy Yan2 小时前
【C++数据结构——查找】二分查找(头歌实践教学平台习题)【合集】
开发语言·数据结构·c++·算法·查找·头歌实践教学平台·合集
埃菲尔铁塔_CV算法3 小时前
BOOST 在计算机视觉方面的应用及具体代码分析(二)
c++·人工智能·算法·机器学习·计算机视觉
Smark.4 小时前
(leetcode算法题)137. 只出现一次的数字 II
算法·leetcode
graceyun4 小时前
牛客网刷题 ——C语言初阶(6指针)——字符逆序
c语言·开发语言
DB_UP4 小时前
基于XGBoost的集成学习算法
算法·机器学习·集成学习
graceyun4 小时前
牛客网刷题 ——C语言初阶(5操作符)——OR76 两个整数二进制位不同个数
c语言·开发语言
刘大猫264 小时前
《docker基础篇:4.Docker镜像》包括是什么、分层的镜像、UnionFS(联合文件系统)、docker镜像的加载原理、为什么docker镜像要采用这种
人工智能·算法·计算机视觉