1.习题5.4
有 3 个整数 a、b、c,由键盘输入,输出其中最大的数。
c
#include<stdio.h>
void main() {
int a, b, c, max;
printf("input a,b,c: \n");
scanf("%d,%d,%d", &a, &b, &c);
if (a < b) {
max = b;
}else {
max = a;
}
if (max < c) {
max = c;
}
printf("max=%d\n", max);
}
2.习题5.5
有一个函数:

写一段程序,输入想x,输出y。
c
#include<stdio.h>
void main() {
int x, y;
printf("input x:\n");
scanf("%d", &x);
y = x < 1 ? x : x < 10 ? 2 * x - 1 : 3 * x - 11;
printf("y=%d\n", y);
}
3.习题5.6

c
#include<stdio.h>
void main() {
float score;
char grade;
printf("input score:");
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';
}
printf("score=%f\ngrade=%c\n", score, grade);
}
4.习题5.7
给一个不多于 5 位的正整数,要求:
① 求出它是几位数;
② 分别输出每一位数字;
③ 按逆序输出各位数字,例如原数为 321,应输出 123。
c
#include<stdio.h>
void main() {
long int number;
int ge, shi, bai, qian, wan, wei;
printf("input number:\n");
scanf("%ld", &number);
wei = number > 9999 ? 5 : number > 999 ? 4 : number > 99 ? 3 : number > 9 ? 2 : 1;
wan = number / 10000;
qian = number % 10000 / 1000;
bai = number % 1000 / 100;
shi = number % 100 / 10;
ge = number % 10;
switch (wei) {
case 5: printf("%d %d %d %d %d\n", wan, qian, bai, shi, ge);
printf("逆序:");
printf("%d%d%d%d%d\n", ge, shi, bai, qian, wan);
break;
case 4: printf("%d %d %d %d\n", qian, bai, shi, ge);
printf("逆序:");
printf("%d%d%d%d\n", ge, shi, bai, qian);
break;
case 3: printf("%d %d %d\n", bai, shi, ge);
printf("逆序:");
printf("%d%d%d\n", ge, shi, bai);
break;
case 2: printf("%d %d\n", shi, ge);
printf("逆序:");
printf("%d%d\n", ge, shi);
break;
case 1: printf("%d\n", ge);
printf("逆序:");
printf("%d\n", ge);
break;
}
}
5.习题5.8

c
#include<stdio.h>
void main() {
long i;
float bonus, bon1, bon2, bon4, bon6, bon10;
long c;
bon1 = 100000 * 0.1;
bon2 = bon1 + 100000 * 0.075;
bon4 = bon2 + 200000 * 0.05;
bon6 = bon4 + 200000 * 0.03;
bon10 = bon6 + 400000 * 0.015;
printf("input i:");
scanf("%ld", &i);
#if(0)
c = i / 100000;
if (c > 10) {
c = 10;
}
switch (c) {
case 0: bonus = i * 0.1;
break;
case 1: bonus = bon1 + (i - 100000) * 0.075;
break;
case 2:
case 3: bonus = bon2 + (i - 200000) * 0.05;
break;
case 4:
case 5: bonus = bon4 + (i - 400000) * 0.03;
break;
case 6:
case 7:
case 8:
case 9: bonus = bon6 + (i - 600000) * 0.015;
break;
case 10:
bonus = bon10 + (i - 1000000) * 0.01;
}
#endif
if (i <= 100000)
bonus = i * 0.1;
else if (i <= 200000)
bonus = bon1 + (i - 100000) * 0.075;
else if (i <= 400000)
bonus = bon2 + (i - 200000) * 0.05;
else if (i <= 600000)
bonus = bon4 + (i - 400000) * 0.03;
else if (i <= 1000000)
bonus = bon6 + (i - 600000) * 0.015;
else
bonus = bon10 + (i - 1000000) * 0.01;
printf("奖金:%f\n", bonus);
}
6.习题5.9
输入4个整数,要求按由小到大的顺序输出
c
#include<stdio.h>
void main() {
int t, a, b, c, d;
printf("input:\n");
scanf("%d%d%d%d", &a, &b, &c, &d);
printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);
// 选择排序
if (a > b) {
t = a;
a = b;
b = t;
}
if (a > c) {
t = a;
a = c;
c = t;
}
if (a > d) {
t = a;
a = d;
d = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
if (b > d) {
t = b;
b = d;
d = t;
}
if (c > d) {
t = c;
c = d;
d = t;
}
printf("%d-->%d-->%d-->%d", a, b, c, d);
}
6.习题5.10

c
#include <stdio.h>
int main() {
int h = 10;
float x, y, d1, d2, d3, d4;
const float r_sq = 1.0; // 半径平方(r=1,r²=1)
printf("input x y:\n");
scanf("%f%f", &x, &y);
// 计算到四个圆心的距离平方
d1 = (x - 2) * (x - 2) + (y - 2) * (y - 2); // (2,2)
d2 = (x + 2) * (x + 2) + (y - 2) * (y - 2); // (-2,2)
d3 = (x + 2) * (x + 2) + (y + 2) * (y + 2); // (-2,-2)
d4 = (x - 2) * (x - 2) + (y + 2) * (y + 2); // (2,-2)
// 只要在任意一个圆内(距离平方 ≤ 半径平方),高度为10
if (d1 > r_sq && d2 > r_sq && d3 > r_sq && d4 > r_sq) {
h = 0;
}
printf("h=%d\n", h);
return 0;
}