谭浩强C程序设计第四版第四章课后答案

谭浩强C程序设计第四版第四章课后答案
1.算数运算:a+b
关系运算:a > b
逻辑运算:a&&b
2.真为1假为0
3.(1)a+b > c 为真 && b == c 为假,值为0
(2)&&优先级大于||,a>0为真,式子为1;
(3)||运算,最后为1,式子值为1;
(4)&&运算只要出现1个0,式子为0
(5)!(a+b)为0 + c - 1 >0为真1
b+c/2 为真式子值为1
4.三个整数中的最大数

#include 

int main()
{
	int a,b,c;
	scanf("%d,%d,%d",&a,&b,&c);
	int max;
	max = (a > b) ? ((a > c) ? a : c) : ((b > c)? b : c);
	printf("max = %d",max);
	return 0;
}

5.输出平方根

#include 
#include 

int main()
{
	float a;
	scanf("%f",&a);
	
	while( a >= 1000)
	{
		printf("大于1000,重新输入:\n");
		scanf("%f",&a);
	}
	printf("平方根为%d\n",(int)sqrt(a));

	return 0;

}

6.if语句实现输出函数值

#include 

int main()
{
	float x,y;
	scanf("%f",&x);
	if ( x < 1)
	{
		y = x;
	}
	else if ( x < 10 )
	{
		y = 2 * x - 1.0;
	}
	else
		y = 3 * x - 11.0;

	printf("input x = %5.2f,output y = %5.2f",x,y);
	return 0;
}

7.两个函数都不能实现功能:
(1)函数中if没有花括号,else总是跟着最近的那一个if没有考虑x = 0的情况
(2)没有考虑小于0的情况
8.输出等级switch 语句

#include 

int main()
{
	int score;
	printf("enter your score:");
	scanf("%d",&score);
	printf("Your level is : ");
	int level = score / 10;
	switch(level)
	{
	case 10:
	case 9:
		printf("A\n");
		break;
	case 8:
		printf("B\n");
		break;
	case 7:
		printf("C\n");
		break;
	case 6:
		printf("D\n");
		break;
	default:
		printf("E\n");
		break;
	}
	return 0;
}

9.逆序输出:按部就班

#include 

int main()
{
	int a;
	scanf("%d",&a);
	if ( a / 10000 )
	{
		printf("%d 是 5 位数。\n",a);
		printf("个位数为:%d\n",a%10);
		printf("十位数为:%d\n",a/10%10);
		printf("百位数为:%d\n",a/100%10);
		printf("千位数为:%d\n",a/1000%10);
		printf("万位数为:%d\n",a/10000);
		printf("按逆序输出为:%d%d%d%d%d\n",a%10,a/10%10,a/100%10,a/1000%10,a/10000);
	}
	else if ( a / 1000)
	{
		printf("%d 是 4 位数。\n",a);
		printf("个位数为:%d\n",a%10);
		printf("十位数为:%d\n",a/10%10);
		printf("百位数为:%d\n",a/100%10);
		printf("千位数为:%d\n",a/1000);
		printf("按逆序输出为:%d%d%d%d\n",a%10,a/10%10,a/100%10,a/1000);
	}
	else if (a / 100)
	{
		printf("%d 是 3 位数。\n",a);
		printf("个位数为:%d\n",a%10);
		printf("十位数为:%d\n",a/10%10);
		printf("百位数为:%d\n",a/100);
		printf("按逆序输出为:%d%d%d\n",a%10,a/10%10,a/100);
	}
	else if (a/10)
	{
		printf("%d 是 2 位数。\n",a);
		printf("个位数为:%d\n",a%10);
		printf("十位数为:%d\n",a/10);
		printf("按逆序输出为:%d%d\n",a%10,a/10);
	}
	else
	{
		printf("%d 是 1 位数。\n",a);
		printf("个位数为:%d\n",a);
		printf("按逆序输出为:%d\n",a);
	}
	return 0;
}

10.企业奖金发放

#include 

int main()
{
	int I0 = 100000,I;
	double ward;
	scanf("%d",&I);

	double r1 = 0.1,r2 = 0.075,r3 = 0.05,r4 = 0.03,r5 = 0.015,r6 = 0.01;
	//if语句实现:
	if ( I < I0)
	{
		ward = I * r1;
	}
	else if ( I <= 2 * I0)
	{
		ward = I0 * r1 + ( I - I0 )*r2;
	}
	else if ( I <= 4 * I0)
	{
		ward = I0 * r1 +  I0 * r2 + ( I - 2 * I0 )*r3;
	}
	else if ( I <= 6 * I0)
	{
		ward = I0 * r1 +  I0 * r2 + 2 * I0 * r3 + ( I - 4 * I0) * r4;
	}
	else if ( I <= 10 * I0)
	{
		ward = I0 * r1 + I0 * r2 + 2 * I0 * r3 + 2 * I0 * r4 + ( I - 6 * I0) * r5;
	}
	else
		ward = I0 * r1 + I0 * r2 + 2 * I0 * r3 + 2 * I0 * r4 + 4 * I0 * r5 + ( I - 10 * I0 ) * r6;

	//switch语句实现:
	switch(I/I0)
	{
	case 0:
		ward = I * r1;
		break;
	case 1:
		ward = I0 * r1 + ( I - I0 )*r2;
		break;
	case 2:
	case 3:
		ward = I0 * r1 +  I0 * r2 + ( I - 2 * I0 )*r3;
		break;
	case 4:
	case 5:
		ward = I0 * r1 +  I0 * r2 + 2 * I0 * r3 + ( I - 4 * I0) * r4;
		break;
	case 6:
	case 7:
	case 8:
	case 9:
		ward = I0 * r1 + I0 * r2 + 2 * I0 * r3 + 2 * I0 * r4 + ( I - 6 * I0) * r5;
		break;
	default:
		ward = I0 * r1 + I0 * r2 + 2 * I0 * r3 + 2 * I0 * r4 + 4 * I0 * r5 + ( I - 10 * I0 ) * r6;
	}
	printf("ward = %8.2f",ward);
	return 0;


}

11.四个数顺序排列:找出最大,找出最小,排列剩下两个

#include 

int main()
{
	int a,b,c,d;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	int t;
	//a最小
	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;
	}
	//d最大
	if ( d < b )
	{
		t = d;
		d = b;
		b = t;
	}
	if ( d < c )
	{
		t = d;
		d = c;
		c = t;
	}
	//排bc
	if (b > c)
	{
		t = b;
		b = c;
		c = t;
	}
	printf("从小到大的顺序为%d<%d<%d<%d\n",a,b,c,d);

	return 0;
}

12.圆塔问题:在阴影部分为10,不在阴影部分为0

#include 
#include 
int main()
{
	int x , y;
	scanf("%d%d",&x,&y);
	if ( y <=  2 + sqrt(1.00 - ( x - 2) * (x - 2)) && y >=  2 - sqrt(1.00 - ( x - 2) * (x - 2)) ||
		 y <= -2 + sqrt(1.00 - ( x - 2) * (x - 2)) && y >= -2 - sqrt(1.00 - ( x - 2) * (x - 2)) ||
		 y <=  2 + sqrt(1.00 - ( x + 2) * (x + 2)) && y >=  2 - sqrt(1.00 - ( x + 2) * (x + 2)) ||
		 y <= -2 + sqrt(1.00 - ( x + 2) * (x + 2)) && y >= -2 - sqrt(1.00 - ( x + 2) * (x + 2)) 
		)
	{
		printf("高度为 10m 。\n");
	}
	else
		printf("高度为 0m 。\n");
	return 0;
}

你可能感兴趣的:(谭浩强C程序设计第四版课后答案)