谭浩强c语言第四章习题
4.4 有3个整数a, b, c,由键盘输入,输出其中最大的数。
# include <stdio.h>
# include <math.h>
int main ( )
{
int a, b, c, t;
printf ( "please input three integer:\n" ) ;
scanf ( "%d%d%d" , & a, & b, & c) ;
if ( a>= b)
{
t= a;
a= b;
b= t;
}
if ( a>= c)
{
t= a;
a= c;
c= t;
}
if ( b>= c)
{
t= b;
b= c;
c= t;
}
printf ( "the largest number is %d\n" , c) ;
return 0 ;
}
4.5 从键盘输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)。要求在输入数据后先对其进行检查是否为小于1000 的正数。若不是,则要求重新输入。
# include <stdio.h>
# include <math.h>
int main ( )
{
int a, b;
printf ( "请输入一个小于1000的正数:\n" ) ;
scanf ( "%d" , & a) ;
if ( a> 0 && a< 1000 )
{
b= sqrt ( a) ;
printf ( "平方根整数部分为 %d\n" , b) ;
}
else
printf ( "错误!请重新输入。" ) ;
return 0 ;
}
4.6 有一个函数,编写程序,输入x的值,输出y相应的值。
# include <stdio.h>
# include <math.h>
int main ( )
{
double x, y;
printf ( "请输入x的值\n" ) ;
scanf ( "%lf" , & x) ;
if ( x< 1 )
y= x;
else if ( x>= 1 && x< 10 )
y= 2 * x- 1 ;
else if ( x>= 10 )
y= 3 * x- 11 ;
printf ( "y=%.2lf" , y) ;
return 0 ;
}
4.8 给出一百分制成绩,要求输出成绩等级’A’、‘B’、‘C’、‘D’、‘E’。 90分以上为’A’,8089分为’B’,7079分为’C’ ,60~69分为’D’ ,60分以下为’E’。
# include <stdio.h>
# include <math.h>
int main ( )
{
float score;
int a;
printf ( "请输入分数:" ) ;
scanf ( "%f" , & score) ;
if ( score> 100 || score< 0 )
printf ( "error!" ) ;
else
a= score/ 10 ;
switch ( a)
{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 : printf ( "E" ) ; break ;
case 6 : printf ( "D" ) ; break ;
case 7 : printf ( "C" ) ; break ;
case 8 : printf ( "B" ) ; break ;
case 9 :
case 10 : printf ( "A" ) ; break ;
}
return 0 ;
}
4.9 给一个不多于5位的正整数,要求:①求出它是几位数;②分别输出每一位数字;③按逆序输出各位数字,例如原数为321,应输出123。 ①求出它是几位数; ②分别输出每一位数字; ③按逆序输出各位数字,例如原数为321,应输出123。
# include <stdio.h>
# include <math.h>
int main ( )
{
int a, b1, c1, c2, c3, c4, c5;
printf ( "请输入一个不多于5位的正整数:" ) ;
scanf ( "%d" , & a) ;
if ( a> 0 && a< 99999 )
{
b1= a/ 10 ;
if ( b1>= 1000 && b1< 10000 )
printf ( "数为5位数。\n" ) ;
else if ( b1>= 100 && b1< 1000 )
printf ( "数为4位数。\n" ) ;
else if ( b1>= 10 && b1< 100 )
printf ( "数为3位数。\n" ) ;
else if ( b1> 0 && b1< 10 )
printf ( "数为2位数。\n" ) ;
else
printf ( "数为1位数。\n" ) ;
c1 = a/ 10000 ;
c2 = ( a- c1* 10000 ) / 1000 ;
c3 = ( a- c1* 10000 - c2* 1000 ) / 100 ;
c4 = ( a- c1* 10000 - c2* 1000 - c3* 100 ) / 10 ;
c5 = a- c1* 10000 - c2* 1000 - c3* 100 - c4* 10 ;
if ( c1!= 0 )
printf ( "%d %d %d %d %d\n" , c1, c2, c3, c4, c5) ;
else if ( c1== 0 && c2!= 0 )
printf ( "%d %d %d %d\n" , c2, c3, c4, c5) ;
else if ( c1== 0 && c2== 0 && c3!= 0 )
printf ( "%d %d %d\n" , c3, c4, c5) ;
else if ( c1== 0 && c2== 0 && c3== 0 && c4!= 0 )
printf ( "%d %d\n" , c4, c5) ;
else
printf ( "%d\n" , c5) ;
if ( c1!= 0 )
printf ( "%d%d%d%d%d\n" , c5, c4, c3, c2, c1) ;
else if ( c1== 0 && c2!= 0 )
printf ( "%d%d%d%d\n" , c5, c4, c3, c2) ;
else if ( c1== 0 && c2== 0 && c3!= 0 )
printf ( "%d%d%d\n" , c5, c4, c3) ;
else if ( c1== 0 && c2== 0 && c3== 0 && c4!= 0 )
printf ( "%d%d\n" , c5, c4) ;
else
printf ( "%d\n" , c5) ;
}
else
printf ( "error!\n" ) ;
return 0 ;
}
4.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语句编写程序。
# include <stdio.h>
# include <math.h>
int main ( )
{
float I, bonus1, bonus2, bonus3, bonus4, bonus5, bonus6;
int aa;
printf ( "请输入利润I=" ) ;
scanf ( "%f" , & I) ;
bonus1 = I* 0.1 ;
bonus2 = 100000 * 0.1 + ( I- 100000 ) * 0.075 ;
bonus3 = 100000 * 0.1 + 100000 * 0.075 + ( I- 200000 ) * 0.05 ;
bonus4 = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + ( I- 400000 ) * 0.03 ;
bonus5 = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + ( I- 600000 ) * 0.015 ;
bonus6 = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + ( I- 1000000 ) * 0.01 ;
if ( I< 100000 )
printf ( "%.2f\n" , bonus1) ;
else if ( I> 100000 && I<= 200000 )
printf ( "%.2f\n" , bonus2) ;
else if ( I> 200000 && I<= 400000 )
printf ( "%.2f\n" , bonus3) ;
else if ( I> 400000 && I<= 600000 )
printf ( "%.2f\n" , bonus4) ;
else if ( I> 600000 && I<= 1000000 )
printf ( "%.2f\n" , bonus5) ;
else if ( I> 1000000 )
printf ( "%.2f\n" , bonus6) ;
aa = I/ 100000 ;
if ( aa> 10 )
aa= 10 ;
switch ( aa)
{
case 0 : printf ( "%.2f\n" , bonus1) ; break ;
case 1 : printf ( "%.2f\n" , bonus2) ; break ;
case 2 :
case 3 : printf ( "%.2f\n" , bonus3) ; break ;
case 4 :
case 5 : printf ( "%.2f\n" , bonus4) ; break ;
case 6 :
case 7 :
case 8 :
case 9 : printf ( "%.2f\n" , bonus5) ; break ;
case 10 : printf ( "%.2f\n" , bonus6) ; break ;
}
return 0 ;
}
4.11 输入4个整数,要求按由小到大的顺序输出。
# include <stdio.h>
# include <math.h>
int main ( )
{
int a[ 4 ] ;
int i, j, t;
printf ( "请输入四个整数:\n" ) ;
for ( i= 0 ; i< 4 ; i++ )
scanf ( "%d" , & a[ i] ) ;
for ( j= 0 ; j< 3 ; j++ )
for ( i= 0 ; i< 3 - j; i++ )
if ( a[ i] >= a[ i+ 1 ] )
{
t= a[ i] ;
a[ i] = a[ i+ 1 ] ;
a[ i+ 1 ] = t;
}
printf ( "从小到大排序为:" ) ;
for ( i= 0 ; i< 4 ; i++ )
printf ( "%d " , a[ i] ) ;
return 0 ;
}
4.12 有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为1,见图。这4个塔的高度为10m,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为零)。
# include <stdio.h>
# include <math.h>
int main ( )
{
float x, y, d0, d1, d2, d3;
int x1, y1, x2, y2, x3, y3, x4, y4, h;
printf ( "请输入坐标x,y:" ) ;
scanf ( "%f %f" , & x, & y) ;
x1 = 2 ;
y1 = 2 ;
x2 = - 2 ;
y2 = 2 ;
x3 = - 2 ;
y3 = - 2 ;
x4 = 2 ;
y4 = - 2 ;
d0 = sqrt ( pow ( x- x1, 2 ) + pow ( y- y1, 2 ) ) ;
d1 = sqrt ( pow ( x- x2, 2 ) + pow ( y- y2, 2 ) ) ;
d2 = sqrt ( pow ( x- x3, 2 ) + pow ( y- y3, 2 ) ) ;
d3 = sqrt ( pow ( x- x4, 2 ) + pow ( y- y4, 2 ) ) ;
if ( d0> 1 && d1> 1 && d2> 1 && d3> 1 )
printf ( "h=0" ) ;
else
printf ( "h=10" ) ;
return 0 ;
}