当前位置: 首页 > article >正文

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

第4章

  1. 输出三个整数中的最大值
    代码如下
#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. 大赛
    代码如下
#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. 程序描述分段函数
    代码如下
#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. 对比两个程序
    程序代码如下
/* 代码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. 对成绩进行分级
    代码如下
#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. 判断数据
    代码如下

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. 企业发放奖金提成
    代码如下
// 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. 四个整数排序
    代码如下
// 采用依次比较法
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. 求建筑高度
    代码如下
// 根据该点到圆心的距离进行判断
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;
    }
}

http://www.kler.cn/a/468621.html

相关文章:

  • 【微服务与K8S】
  • Python编程实例-机器学习中的Hinge Loss编程实现
  • 条件期望窥探
  • 深入解析-正则表达式
  • OpenKit 介绍
  • Unity性能优化总结
  • stm32HAL库使LED闪烁
  • ArcGIS中怎么把数据提取到指定范围(裁剪、掩膜提取)
  • RabbitMQ-基本使用
  • ChatGPT 主流模型GPT-4/GPT-4o mini的参数规模是多大?
  • 学习扩散模型的完整指南(前提知识、DDPM、稳定扩散、DreamBooth等)
  • php有两个数组map比较 通过id关联,number可能数量变化 比较他们之间增加修改删除
  • 【机器学习:二、线性回归模型】
  • 前端(API)学习笔记(CLASS 4):进阶
  • Unity3D 如何做好项目性能优化详解
  • 面试题 2024/12 28 29
  • 微服务组件——利用SpringCloudGateway网关实现统一拦截服务请求,避免绕过网关请求服务
  • Python入门教程 —— 面向对象进阶
  • Go语言的 的反射(Reflection)基础知识
  • 基于伪分布式模式部署Hadoop集群
  • 开源模型迎来颠覆性突破:DeepSeek-V3与Qwen2.5如何重塑AI格局?
  • 流光效果
  • docker从下载到Python项目打包到容器中运行(解决下拉超时问题)
  • 【three.js】Shader着色器
  • 如何弥补开源大语言模型解决推理任务的不足
  • 深度 SEO 优化