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;
    }
}
相关推荐
计算机小白一个2 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^2 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
李白同学3 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
楼台的春风4 小时前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式
大数据追光猿4 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!4 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉4 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生4 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴5 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing5 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode