C程序设计谭浩强第五版课后答案 第四章习题答案
4. 有3个整数a, b, c,由键盘输入,输出其中最大的数。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main ( )
{
int a, b, c;
scanf ( "%d %d %d" , & a, & b, & c) ;
if ( a == b && a == c) {
printf ( "Three numbers are equal\n" ) ;
} else if ( a == b && a > c) {
printf ( "a and b are the largest number\n" , a) ;
} else if ( a == c && a > b) {
printf ( "a and c are the largest number\n" , a) ;
} else if ( b == c && b > a) {
printf ( "c and b are the largest number\n" , a) ;
} else if ( a > b && a > c) {
printf ( "a=%d is the largest number\n" , a) ;
} else if ( b > a && b > c) {
printf ( "b=%d is the largest number\n" , b) ;
} else {
printf ( "c=%d is the largest number\n" , c) ;
}
return 0 ;
}
5.从键盘输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)。要求在输入数据后先对其进行检查是否为小于1000 的正数。若不是,则要求重新输入。
#include <stdio.h>
#include <math.h>
int main ( )
{
float a, b;
scanf_s ( "%f" , & a) ;
if ( a >= 1000 || a < 0 ) {
printf ( "请输入小于1000的正数\n" ) ;
scanf_s ( "%f" , & a) ;
b = sqrt ( a) ;
}
else {
b = sqrt ( a) ;
}
printf ( "a=%4.0f, b=%4.0f\n" , a, b) ;
system ( "pause" ) ;
return 0 ;
}
6. 有一个函数,编写程序,输入x的值,输出y相应的值。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main ( )
{
int x, y;
scanf ( "%d" , & x) ;
if ( x < 1 )
{
y = x;
}
else if ( x >= 1 && x < 10 )
{
y = 2 * x - 1 ;
}
else
{
y = 3 * x - 11 ;
}
printf ( "y = %d\n" , y) ;
return 0 ;
}
7. 有一个函数
有人编写了以下两个程序,请分析它们是否能实现题目要求。不要急于上机运行程序,先分析两个程序的逻辑,画出它们的流程图,分析它们的运行情况。然后,上机运行程序,观察和分析结果。
#include <stdio.h>
int main ( )
{
int x, y;
printf ( "enter x:" ) ;
scanf ( "%d" , & x) ;
y= 0 ;
if ( x >= 0 )
if ( x > 0 ) y= 1 ;
else y= - 1 ;
printf ( "x=%d,y=%d\n" , x, y) ;
return 0 ;
}
8. 给出一百分制成绩,要求输出成绩等级’A’、‘B’、‘C’、‘D’、‘E’。 90分以上为’A’,8089分为’B’,7079分为’C’ ,60~69分为’D’ ,60分以下为’E’。
#include <stdio.h>
int main ( )
{
int score;
printf ( "enter score:" ) ;
scanf_s ( "%d" , & score) ;
if ( score >= 90 ) {
printf ( "A\n" ) ;
} else if ( score >= 80 && score < 90 ) {
printf ( "B\n" ) ;
} else if ( score >= 70 && score < 80 ) {
printf ( "C\n" ) ;
} else if ( score >= 60 && score < 70 ) {
printf ( "D\n" ) ;
} else {
printf ( "E\n" ) ;
}
system ( "pause" ) ;
return 0 ;
}
9. 给一个不多于5位的正整数,要求:①求出它是几位数;②分别输出每一位数字;③按逆序输出各位数字,例如原数为321,应输出123。
解(1)
#include <stdio.h>
int main ( )
{
int num;
printf ( "enter num:" ) ;
scanf_s ( "%d" , & num) ;
if ( num > 99999 || num < 0 ) {
printf ( "请输入0~99999之间的正数\n" ) ;
return - 1 ;
}
if ( num >= 10000 ) {
printf ( "5\n" ) ;
} else if ( num >= 1000 ) {
printf ( "4\n" ) ;
} else if ( num >= 100 ) {
printf ( "3\n" ) ;
} else if ( num >= 10 ) {
printf ( "2\n" ) ;
} else {
printf ( "1\n" ) ;
}
system ( "pause" ) ;
return 0 ;
}
解(2)
#include <stdio.h>
int main ( )
{
int num;
printf ( "enter num:" ) ;
scanf_s ( "%d" , & num) ;
if ( num > 99999 || num < 0 ) {
printf ( "请输入0~99999之间的数字\n" ) ;
return - 1 ;
}
if ( num / 10000 > 0 ) {
printf ( "%d " , num / 10000 ) ;
}
if ( num% 10000 >= 1000 ) {
printf ( "%d " , ( num % 10000 ) / 1000 ) ;
}
if ( num% 1000 >= 100 ) {
printf ( "%d " , ( num % 1000 ) / 100 ) ;
}
if ( num% 100 >= 10 ) {
printf ( "%d " , ( num % 100 ) / 10 ) ;
}
if ( num% 10 >= 0 ) {
printf ( "%d " , num % 10 ) ;
}
printf ( "\n" ) ;
system ( "pause" ) ;
return 0 ;
}
解(3)
#include <stdio.h>
int main ( )
{
int num;
printf ( "enter num:" ) ;
scanf_s ( "%d" , & num) ;
if ( num > 99999 || num < 0 ) {
printf ( "请输入0~99999之间的数字\n" ) ;
return - 1 ;
}
if ( num % 10 >= 0 ) {
printf ( "%d " , num % 10 ) ;
}
if ( num % 100 >= 10 ) {
printf ( "%d " , ( num % 100 ) / 10 ) ;
}
if ( num % 1000 >= 100 ) {
printf ( "%d " , ( num % 1000 ) / 100 ) ;
}
if ( num % 10000 >= 1000 ) {
printf ( "%d " , ( num % 10000 ) / 1000 ) ;
}
if ( num / 10000 > 0 ) {
printf ( "%d " , num / 10000 ) ;
}
printf ( "\n" ) ;
system ( "pause" ) ;
return 0 ;
}
10.企业发放的奖金根据利润提成。利润I低于或等于100000元的,奖金可提成10%;利润高于100000元,低于200000元(100000<I≤200000)时,低于100000元的部分按10%提成,高于100000元的部分,可提成7. 5%;200000<I≤400000时,低于200000元的部分仍按上述办法提成(下同)。高于200000元的部分按5%提成;400000<<I≤600000元时,高于400000元的部分按3%提成;600000<1≤1000000时,高于600000元的部分按1.5%提成;I>1000000时,超过1000000元的部分按1%提成。从键盘输入当月利润I,求应发奖金总数。要求:(1) 使用if语句编写程序。(2) 使用switch语句编写程序。
解(1)
#include <stdio.h>
int main ( )
{
double I, salary = 0 ;
printf ( "enter performance:" ) ;
scanf_s ( "%lf" , & I) ;
if ( I < 0 ) {
printf ( "请输入一个正数\n" ) ;
system ( "pause" ) ;
return - 1 ;
}
double salary1 = 100000 * 0.1 ;
double salary2 = ( 200000 - 100000 ) * 0.075 + salary1;
double salary3 = ( 400000 - 200000 ) * 0.05 + salary2;
double salary4 = ( 600000 - 400000 ) * 0.03 + salary3;
double salary5 = ( 1000000 - 600000 ) * 0.015 + salary4;
if ( I <= 100000 ) {
salary = I * 0.1 ;
} else if ( I > 100000 && I <= 200000 ) {
salary = salary1 + ( I - 100000 ) * 0.075 ;
} else if ( I > 200000 && I <= 400000 ) {
salary = salary2 + ( I - 200000 ) * 0.05 ;
} else if ( I > 400000 && I <= 600000 ) {
salary = salary3 + ( I - 400000 ) * 0.03 ;
} else if ( I > 600000 && I <= 1000000 ) {
salary = salary4 + ( I - 600000 ) * 0.015 ;
} else if ( I > 1000000 ) {
salary = salary5 + ( I - 1000000 ) * 0.01 ;
}
printf ( "salary:%f\n" , salary) ;
system ( "pause" ) ;
return 0 ;
}
解(2)
#include <stdio.h>
int main ( )
{
double I, salary = 0 ;
printf ( "enter performance:" ) ;
scanf_s ( "%lf" , & I) ;
if ( I < 0 ) {
printf ( "请输入一个正数\n" ) ;
system ( "pause" ) ;
return - 1 ;
}
double salary1 = 100000 * 0.1 ;
double salary2 = ( 200000 - 100000 ) * 0.075 + salary1;
double salary3 = ( 400000 - 200000 ) * 0.05 + salary2;
double salary4 = ( 600000 - 400000 ) * 0.03 + salary3;
double salary5 = ( 1000000 - 600000 ) * 0.015 + salary4;
int grade = I / 100000 ;
switch ( grade) {
case 0 :
salary = I * 0.1 ; break ;
case 1 :
salary = salary1 + ( I - 100000 ) * 0.075 ; break ;
case 2 : / / 会顺序执行到下一个break 处
case 3 :
salary = salary2 + ( I - 200000 ) * 0.05 ; break ;
case 4 :
case 5 :
salary = salary3 + ( I - 400000 ) * 0.03 ; break ;
case 6 :
case 7 :
case 8 :
case 9 :
salary = salary4 + ( I - 600000 ) * 0.015 ; break ;
default :
salary = salary5 + ( I - 1000000 ) * 0.01 ; break ;
}
printf ( "salary:%f\n" , salary) ;
system ( "pause" ) ;
return 0 ;
}
11. 输入4个整数,要求按由小到大的顺序输出。
#include <stdio.h>
int main ( )
{
int a, b, c, d;
int max_num;
scanf_s ( "%d %d %d %d" , & a, & b, & c, & d) ;
int tmp;
if ( a > b) {
tmp = a; a = b; b = tmp;
}
if ( a > c) {
tmp = a; a = c; c = tmp;
}
if ( a > d) {
tmp = a; a = d; d = tmp;
}
if ( b > c) {
tmp = b; b = c; c = tmp;
}
if ( b > d) {
tmp = b; b = d; d = tmp;
}
if ( c > d) {
tmp = c; c = d; d = tmp;
}
printf ( "%d %d %d %d\n" , a, b, c, d) ;
system ( "pause" ) ;
return 0 ;
}
12. 有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为1。这4个塔的高度为10m,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为零)。
#include <stdio.h>
#include <math.h>
void main ( )
{
int h;
double x, y, m, n, r;
printf ( "Please input a coordinate (x,y):" ) ;
scanf_s ( "%lf,%lf" , & x, & y) ;
if ( fabs ( x) > 3 || fabs ( y) > 3 ) {
h = 0 ;
printf ( "The height of the coordinate(%f,%f):h=%d\n" , x, y, h) ;
return 0 ;
}
m = fabs ( x) - 2 ; n = fabs ( y) - 2 ;
r = sqrt ( m * m + n * n) ;
if ( r > 1 )
h = 0 ;
else
h = 10 ;
printf ( "The height of the coordinate(%f,%f):h=%d\n" , x, y, h) ;
system ( "pause" ) ;
return 0 ;
}