第五章八大题编程题答案
时间:2026-01-16
时间:2026-01-16
第五章八大题编程题答案:
1、 编写一个程序,输入a、b、c三个值,输出其中最大值。
方法一:
main()
{
int a,b,c,max;
printf(“请输入三个数a,b,c:\n”);
scanf(“%d,%d,%d”,&a,&b,&c);
max=a;
if(max<b) max=b;
if(max<c) max=c;
printf(“最大数为:“%d”,max);
}
方法二:
main( )
{
int a,b,c,temp,max;
printf("input three integer:");
scanf("%d,%d,%d",&a,&b,&c);
temp=(a>b)?a:b; /*temp为a、b中较大值*/
max=(temp>c)?max:c; /*在temp和c中比较出最大值*/
printf("max=%d\n",max);
}
运行结果:
input three integer:12,34,9↙
max=34
2、编程实现:输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上的数字,否则输出两数之和
main( )
{
int a,b;
scanf(“%d%d”,&a,&b);
if(a*a+b*b>100)
printf(“%d”,(a*a+b*b)/100);
else
printf(“%d”,a+b);
}
3、有一函数:y=
main( )
{
x 2x 1 3x 11 (x 1)(1 x 10)(x 10),写一程序,输入x,输出y值。
int x,y;
printf("input x:");
scanf("%d",&x);
if(x<1) /*当x<1时,求对应y值*/
y=x;
else if(x<10) /*当1≤x<10时,求对应y值*/
y=2*x-1;
else /*当x≥10时,求对应y值*/
y=3*x-11;
printf(“x=%d,y=%d”,x,y);
}
运行结果:
input x:20↙
x=20,y=49
4、给出一百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。90分以上为‘A’,80~89分为‘B’,70~79分为‘C’,60~69分为‘D’,60分以下为‘E’。
main( )
{
float score;
char grade;
printf("input student score:");
scanf("%f",&score);
while (score>100||score<0) /*当输入错误时,提示用户并允许重新输入*/
{
printf("error\n.");
scanf("%f",&score);
}
switch((int)(score/10)) /*将score/10的值转换成整型以便于判断*/
{
case 10:
case 9: grade=’A’;break;
case 8: grade=’B’;break;
case 7: grade=’C’;break;
case 6: grade=’D’;break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: grade=’E’;
}
printf("score: %5.1f,grade: %c\n",score,grade);
}
运行结果:
input student score:90.5↙
score:90.5,grade:A
5、给一个不多于5位的正整数,要求:①求出它是几位数;②分别打印出每一位数字;③按逆序打印出各位数字,例如原数为321,应输出123。
main( )
{
long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
/*分别定义个位、十位、百位、千位、万位数字相应变量
printf("input 0-99999:");
scanf("%ld",&num);
if(num>9999)
place=5;
else if(num>999)
place=4;
else if(num>99)
place=3;
else if(num>9)
place=2;
else place=1;
printf("place=%d\n",place); /*输出位数*/
printf("shuzi is:");
ten_thousand=num/10000;/*以下五行分别求万位、千位、百位、十位、个位数字 */ thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10); switch(place) /*根据位数判断应该输出哪几位数字*/
{case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,
ten,indiv);/*正序输出*/
printf("\ninvert is:");
printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);
/*逆序输出*/
break;
case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
printf("\ninvert is:");
printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d",hundred,ten,indiv);
printf("\ninvert is:");
printf("%d%d%d\n",indiv,ten,hundred);
break;
case 2:printf("%d,%d",ten,indiv);
printf("\ninvert is:");
printf("%d%d\n",indiv,ten);
break;
case 1:printf("%d",indiv);
printf("\ninvert is:");
printf("%d\n",indiv);
break;
}
}
运行结果:
input 0-99999:98765↙
place=5
shuzi is:9,8,7,6,5
invert is: 56789
6、试编程判断输入的正整数是否既是5又是7的整数倍。若是,则输出yes;否则输出no。 main( )
{
int x;
printf("input x:");
scanf("%d",&x);
if(x%5==0&&x%7==0)
printf("yes");
else
printf("no");
}
运行情况:
inupt x:35↙
yes
思考:if中的条件表达式可否改成 !x%5&&!x%7
7、编制程序要求输入正整数a和b,若a2+b2大于100,则输出a2+b2百位以上的数字,否则输出两数之和。
main( )
{
int a,b,x,y;