c程序设计第三版课后习题答案
发布时间: 2024-09-18 20:44:36
A. 求c语言程序设计第三版和c语言程序设计实验与习题指导答案
c语言程序设计第三版指导答案
附录F课后题参考答案
习题1
1.1填空题
1.函数
2.主函数main();主函数main()3.主函数main()4.函数首部;函数体5.{;}
6.顺序结构;选择结构;循环结构7..c;.obj;.exe
1.2思考题
1.答:结构化程序设计是指,为使程序具有一个合理的结构以保证程序正确性而规定的一套如何进行程序设计的原则。顺序结构,选择结构,循环结构
2.答:算法是对具体问题求解步骤的一种描述。计算机算法的表达工具通常采用以下几种方法:①用自然语言表示算法;②用流程图表示算法;③用伪代码表示算法;④用程序设计语言表示算法。
3.略4.略5.略1.3编程题1.答:
#include"stdio.h"main()
{floata=10,b=20,h=5,s;s=(a+b)*h/2;
printf("s=%f",s);}
2.答:
#include"stdio.h"
main()
{printf("******************************");printf("*helloworld*");printf("******************************");}
习题2
2.1单选题
DBDCADCABBCA
2.2填空题
1.2.000000
2.1;0.500000
3.9;24.65.100;d6.(1)20
(2)0
(3)607.(1)10;6;4
(2)6;9;15
(3)3;60;83
8.55或'7'
9.x=4;y=6;z=3;m=463
2.3改错题(略)
习题3
3.1单选题
BDABCADCACBBA
3.2填空题
1.32.2613.10
4.2,1;互换a,b的值5.6.66.0037.7
8.5.0,4,c=3<Enter>
9.i=10,j=20<Enter>
10.(1)65(2)65,A(3)3.14,123.46
(4)3.141600e+000,1.234560e+002(5)8765.432100(6)8.765432e+003
11.a=2b=5x=8.8y=76.34c1=65c2=9712.%d/%d;%d/%d=%.2f
3.3改错题(略)3.4编程题
1.答:
#include"stdio.h"main(){
intx,y;
scanf("%d%d",&x,&y);printf(" x y ");
printf("十进制数 %d %d ",x,y);printf("八进制数 %o %o ",x,y);printf("十六进制数 %X %x ",x,y);}
2.答:
#include"stdio.h"main(){
charch;
printf("请输入一个大写英文字母");scanf("%c",&ch);
printf("大写英文字母是%c ",ch);printf("它的前导字符是%c ",ch-1);printf("它的后续字符是%c ",ch+1);}
3.答:
#include"stdio.h"main(){
intx,a,b,c,y;
printf("请输入一个三位整数 ");scanf("%d",&x);a=x/100;
b=(x-a*100)/10;c=x%10;
y=c*100+b*10+a;
printf("反向输出该整数:%d ",y);}}
4.答:
#include"stdio.h"main()
{inthour;
doublesalary,salaryday;
scanf("%d,%lf",&hour,&salaryday);
salary=hour*salaryday-hour*salaryday*0.1;
printf("%8.2lf ",salary);}
5.答:
#include"stdio.h"main(){
inta,b,c,t;
printf("请输入三个整数 ");scanf("%d%d%d",&a,&b,&c);
printf("交换前a=%d,b=%d,c=%d ",a,b,c);t=a;a=c;c=b;b=t;
printf("交换后a=%d,b=%d,c=%d ",a,b,c);}
习题4
4.1单选题
BADDDACBBBBA
4.2填空题
1.1
2.(1)a>0||b>0
(2)x>0&&x<=10(3)a==1.5&&b==1.5&&c==1.5
(4)p<a||p<b||p<c
3.(1)0(2)1(3)1(4)0(5)1
4.c=15.-46.17.5,0,38.59.123
10.(cvb=='y'||cvb=='Y')&&(work>=3||college=='y'||college=='Y')&&age<=35
4.3改错题(略)4.4编程题
1.答
#include"stdio.h"
#include"math.h"main(){
doublea,b,c,p,area;
scanf("%lf%lf%lf",&a,&b,&c);
printf("三角形的三边为:%.llf,%.1lf,%.1lf ",a,b,c);if(a+b>c&&a+c>b&&b+c>a){p=(a+b+c)/2;
area=sqrt(p*(p-a)*(p-b)*(p-c));
printf("三角形的面积为%.2lf ",area);}else
printf("不能构成三角形 ");}
2.答:
#include"stdio.h"main()
{intx,y;
scanf("%d,%d",&x,&y);if(x*x+y*y>1000)
printf("%d ",(x*x+y*y)/100);else
printf("%d ",x+y);}
3.答:
#include"stdio.h"#include"math.h"main()
{doublex,y;
scanf("%lf",&x);if(x<-2)y=x*x-sin(x);elseif(x<=2)y=pow(2,x)+x;elsey=sqrt(x*x+x+1);
printf("x=%.2lfy=%.2lf ",x,y);}
4.答:
#include"stdio.h"main()
{longge,shi,qian,wan,x;scanf("%ld",&x);wan=x/10000;
qian=x%10000/1000;shi=x%100/10;
ge=x%10;
if(ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/printf("thisnumberisahuiwen ");else
printf("thisnumberisnotahuiwen ");
}
5.答:
#include"stdio.h"main()
{floatp,w,s,d,f;
scanf("%f,%,%f",p,s,w);if(s>3000)d=0.15elseif(s>=2000)d=0.1;elseif(s>=1000)d=0.08;elseif(s>=500)d=0.05;elseif(s>=250)d=0.02;elsed=0f=p*w*s*(1-d);printf("%f",f);}
6.答:
#include"stdio.h"main()
{intyear,money;charx;
printf("是否是本公司产品(y/n):");scanf("%c",&x);
if(x=='y'||x=='Y')
{printf("产品使用的年限:");scanf("%d",&year);
if(year<=1)money=0;
elseif(year<8)money=50;elsemoney=100;
printf("产品保修额是:%d ",money);
}
else
{money=200;
printf("不是本公司产品,产品保修额是:%d ",money);}}
7.答:
#include"stdio.h"main()
{intmoney,num1,num2;
printf("请输入取款额(≤2000):");scanf("%d",&money);
if(money>2000)printf("请输入取款额数≤2000! ");elseif(money%50==0){num1=money/100;num2=(money-num1*100)/50;printf("需支付100元:%d张 ",num1);printf("需支付50元:%d张 ",num2);}elseprintf("输入钱数必须是50的倍数! ");}
习题5
5.1单选题
CDABAABDDBDBCB
5.2填空题
1.202.333
3.(1)i<10或i<=9(2)j%3!=0
4.(1)flag*(float)k/(k+1)或1.0*flag*k/(k+1)(2)flag=-flag5.(1)max=x
(2)x!=-1(3)scanf("%d",&x)
6.(1)x<=9或x<10
(2)y=9-x
5.3改错题(略)5.4编程题
1.答:
(1)for循环,其他略
#include"stdio.h"
main()
{inti,s=0;
for(i=1;i<=100;i++)s+=i*i;
printf("%d ",s);}
(2)for循环,其他略
#include"stdio.h"main()
{inti=1,p=1;doubles=1;do{
s+=1.0/p;p*=++i;
}while(1.0/p>1e-6);printf("%lf",s);}
2.答:
#include"stdio.h"main()
{intm,n,t,a,b;
scanf("%d,%d",&m,&n)if(m<n)
{t=mm=nn=t}a=m;b=n;t=m%nwhile(t)
{m=nn=tt=m%n;}printf("%d",n);}
3.答:
#include"stdio.h"main()
{intx,y,s=1;
scanf("%d,%d",&x,&y)for(y>0y--)s*=x
printf("%d,%d,%d ",s%10,s/10%10,s/100%10);}
4.答:
#include"stdio.h"main()
{intx,y,z;
for(x=1x<20x++)for(y=1y<33y++){z=100-x-y
if((z%3)==0&&(5*x+3*y+z/3)==100)printf("x=%d,y=%d,z=%d ",x,y,z)}}
5.答:(a)
#include"stdio.h"main()
{intj,k
for(j=1j<=4j++)
{for(k=1;k<=4-j;k++)printf("");printf("****")printf(" ")}}
(b)
#include"stdio.h"main()
{intj,k
for(j=1j<=4j++)
{for(k=1;k<=4-j;k++)printf("");for(k=1k<=2*j-1k++)printf("*")printf(" ")}}
6.答:
程序分析:利用for循环控制在100~999之间,对每个数分解出个位、十位、百位。
#include<stdio.h>main(){inti,j,k,n;printf("waterflower'numberis:");for(n=100;n<1000;n++){i=n/100;/*分解出百位*/j=n/10%10;/*分解出十位*/k=n%10;/*分解出个位*/if(n==i*i*i+j*j*j+k*k*k){printf("%-5d",n);}}printf(" ");}
7.答:
#include<stdio.h>main(){intx;for(x=1000;x>=3;x--)if(x%3==1&&x%5==2&&x%7==3){
printf("该校的学生人数是:%d人 ",x);break;}}
8.答:
#include<stdio.h>main(){intx=12,i=1;while(1)
{if((x+20+i)==2*(x+i))break;i++;}printf("小明母亲在%d年后比小明的年龄大一倍 ",i);printf("那时小明年龄是:%d岁,小明母亲年龄是:%d岁 ",x+i,x+20+i);}
习题6
6.1单选题
DBCCBBDC
C语言程序设计教程(第3版)
278
6.2填空题
1.c2.603.1000104.16
6.3编程题
1.答:
#include"stdio.h"#include"math.h"
#defineF(a)a*a+sqrt(3*a*a+2*a+1)main()
{floatx,f;
scanf("%f",&x);
f=4.5/F(exp(x))+F(cos(x))+F(sqrt(x))/F(x*x)printf("%f ",f);}
习题7
7.1单选题
BCADACCCDABCBDB
7.2填空题
1.(1)2345(2)10010(3)QuickC
(4)1000001000001000001000001(5)Language
(6)LanguageProgramming2.(1)j+=2(2)a[i]>a[j]3.(1)i=1(2)x[i-1]
7.3改错题(略)7.4编程题
1.答:
#defineN10
#include"stdio.h"main()
{inta[N]={1,2,3,4,5,6,7,8,9,10,osum=0,qsum=0,j;for(j=0;j<10;j++)
if(j%2)qsum+=a[j];
elseosum+=a[j];
printf("osum=%d,qsum=%d ",osum,qsum);}
2.答:
#defineN10
#include"stdio.h"main()
{inta[N]={10,20,30,40,50,60,70,80,90},j,k,x;scanf("%d",&x);for(j=0;j<N;j++)
if(x<a[j])break;if(j==N)a[N-1]=x;else
{for(k=N-1;k>j;k--)a[k]=a[k-1];a[j]=x;}
for(j=0;j<N;j++)
printf("%d",a[j]);}
3.答:
#defineM3
#include"stdio.h"main()
{inta[M][M]={{1,2,3},{2,4,5},{3,5,6}},j,k,flag=1;;for(j=0;j<M;j++)
for(k=0;k<M;k++)if(a[j][k]!=a[k][j]){flag=0;break;}if(flag)printf("ok");elseprintf("NO");}
4.答:
#include"stdio.h"#include"string.h"main()
{charc1[10],c2[10],j;gets(c1);gets(c2);
for(j=0;(c1[j]==c2[j])&&c1[j]&&c2[j];j++);if(c1[j]>c2[j])printf("%d ",1);if(c1[j]<c2[j])printf("%d ",-1);if(c1[j]==c2[j])printf("%d ",0);}
5.答:
#include"stdio.h"#include"string.h"#defineM3#defineN80main()
{chara[M][N],j,k,n[5]={0};for(j=0;j<M;j++)gets(a[j]);
for(j=0;j<M;j++)
for(k=0;a[j][k];k++)
if(a[j][k]>='A'&&a[j][k]<='Z')n[0]++;
elseif(a[j][k]>='a'&&a[j][k]<='z')n[1]++;elseif(a[j][k]>='0'&&a[j][k]<='9')n[2]++;elseif(a[j][k]=='')n[3]++;elsen[4]++;
for(j=0;j<5;j++)printf("%4d",n[j]);}
习题8
8.1单选题
DBDACBACCC
8.2填空题
1.(1)2,1(2)10#30#(3)FOUR,P(4)60
2.(1)49
(2)2
(3)2
(4)
(5)
8.3改错题(略)8.4编程题
1.答:
#include"stdio.h"
main()
{intn1,n2,n3,t;int*p1,*p2,*p3;
printf("pleaseinput3number:n1,n2,n3:");scanf("%d,%d,%d",&n1,&n2,&n3);p1=&n1;
p2=&n2;p3=&n3;
if(*p1>*p2){t=*p1;*p1=*p2;*p2=t;}
if(*p1>*p3){t=*p1;*p1=*p3;*p3=t;}if(*p2>*p3){t=*p2;*p2=*p3;*p3=t;}
printf("thesortednumbersare:%d,%d,%d ",n1,n2,n3);}
2.答:
#include"stdio.h"#defineN3main()
{inta[N],*p=a;for(;p-a<N;p++)scanf("%d",p);p=a+N-1;
for(;p-a>=0;p--)printf("%d",*p);}
3.答:
#include"stdio.h"main()
{inta[10];
intj,minl=0,maxl=0;for(j=0;j<10;j++)
scanf("%d",a+j);for(j=0;j<10;j++)
{if(a[maxl]<*(a+j))maxl=j;if(a[minl]>*(a+j))minl=j;}
j=a[0];a[0]=a[minl];a[minl]=j;j=a[9];a[9]=a[maxl];a[maxl]=j;for(j=0;j<10;j++)printf("%d",*(a+j));}
4.答:
输入阵列如下:123456789101112输出阵列如下:
121110987654321
#defineM3
#defineN4
#include"stdio.h"main()
{inta[M][N]={1,2,3,4,5,6,7,8,9,10,11,12},k,j,*p=&a[0][0],t;for(k=0,j=M*N-1;k<j;k++,j--)
{t=*(p+k);*(p+k)=*(p+j);*(p+j)=t;}for(k=0k<Mk++){for(j=0j<Nj++)
printf("%4d",a[k][j]);printf(" ");
}}
5.答:
#include"stdio.h"main(){
intlen;
charstr[20],*p=str;
printf("pleaseinputastring: ");scanf("%s",str);len=0;
while(*p!='