- 295.50 KB
- 2022-04-22 11:46:02 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话:19940600175。
'《程序设计技术》(第三版)习题参考答案习题1一、单项选择题DADDDBDCCD二、填空题①主②main③x=x*(x+b)④普通、格式、分隔和转义⑤指定输出数据格式⑥voidmain()⑦"howareyou!"三、阅读程序题1.9,11,9,102.2,-1,-23.aabbccabcAN4.2,25.575767.5864000,-789.12402367.5864000,-7.89e+00267,86,-789.12,67.5864000,-789.124023,67.5864000,-789.1240236.758640e+001,-7.89e+002A,65,101,411234567,4553207,12d68765535,177777,ffff,65535COMPUTER,COM.6.a+c=102a+c=ff+m=17.950000a+m=17.650000c+f=102.300000double=1746.150019四、程序设计题//xt010401.cpp#include#definePI3.1415926voidmain(){doubler,h;printf("请输入半径r和高h:");scanf("%lf,%lf",&r,&h);printf("圆半径:%lfn",2*PI*r);
printf("圆面积:%lfn",PI*r*r);printf("圆球表面积:%lfn",4*PI*r*r);printf("圆球体积:%lfn",4.0/3*PI*r*r);printf("圆柱体积:%lfn",PI*r*r*h);}//xt010402.cpp#includevoidmain(){intc;printf("Inputacharacter:");c=getchar();printf("%ct%dn",c,c);}//xt010403.cpp#includevoidmain(){intx,y,h=30,f=90;x=(4*h-f)/2;y=(f-2*h)/2;printf("鸡:%d,兔:%dn",x,y);}//xt010404.cpp#include#includevoidmain(){doublea,b,c,s,area;printf("请输入三角形的三条边长(注意三条边长应能够构成三角形):");scanf("%lf,%lf,%lf",&a,&b,&c);s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));printf("三角形的面积是:%lfn",area);}//xt010405.cpp#includevoidmain(){doublef,c;printf("请输入华氏温度:");scanf("%lf",&f);c=5.0/9.0*(f-32);printf("与华氏温度%.2lf对应的摄氏温度是:%.2lfn",f,c);}//xt010406.cpp#includevoidmain()
{charc1,c2,c3,c4,c5;printf("请输入需加密的字符串(5个字符):");c1=getchar();c2=getchar();c3=getchar();c4=getchar();c5=getchar();printf("加密后的字符串是:");putchar(c1+5);putchar(c2+5);putchar(c3+5);putchar(c4+5);putchar(c5+5);printf("n");}习题2一、单项选择题CBADCCCAAC二、填空题①a+b==0&&a*b!=0②逻辑与③逻辑或④continue⑤(c=getchar())!="n"⑥%c三、阅读程序题答案:答案:该程序所完成功能的函数关系为:答案:答案:答案:
答案:四、程序设计题/*xt020401.cpp*/#includevoidmain(){intnum;printf("****Inputnum:****n");scanf("%d",&num);if(num%3==0&&num%5==0&&num%7==0)printf("**YES!**n");elseprintf("**NO!**n");}/*xt020402.cpp*/#includevoidmain(){inth=0;floatx,y,x0=2,y0=2,d1,d2,d3,d4;printf("***input:x,y***n");scanf("%f,%f",&x,&y);d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);/*点(x,y)到各中心点的距离*/d2=(x-x0)*(x-x0)+(y+y0)*(y+y0);d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);if(d1<=1||d2<=1||d3<=1||d4<=1)h=15;printf("x=%f,y=%fn",x,y);printf("h=%dn",h);}/*xt020403.cpp*/#includevoidmain(){inti,j,k,n;for(n=100;n<=999;n++){i=n/100;
j=n/10-i*10;k=n%10;if(n==i*i*i+j*j*j+k*k*k)printf("%dn",n);}}/*xt020404.cpp*/#include#include#defineEPS1.0e-6voidmain(){floate=1,t;longintj=1,n=1;do{j=j*n;t=1.0/j;e=e+t;n++;}while(fabs(t)>=EPS);printf("e=%fn",e);}/*xt020405.cpp*/#includevoidmain(){intnum=1;do{if((num+3)%5==0&&(num-3)%6==0)break;num+=1;}while(1);printf("num=%dn",num);}/*xt020406.cpp*/#includevoidmain(){intnum;for(num=1;num<=99;num++)if(num*num%10==num||num*num%100==num)printf("%4d,%5d",num,num*num);printf("n");}/*xt020407.cpp*/
#includevoidmain(){intn,k=1;floats=0;for(n=1;n<=10000;n++){s=s+1.0/(2.0*n-1)*k;k=-k;}printf("PI=%fn",4*s);}/*xt020408.cpp*/#includevoidmain(){intn10,n5,n2,n1;for(n10=1;n10<=9;n10++)for(n5=1;n5<=17;n5++)for(n2=1;n2<=37;n2++){n1=40-n10-n5-n2;if(n1>=1&&n10*10+n5*5+n2*2+n1==100)printf("n10=%d,n5=%d,n2=%d,n1=%dn",n10,n5,n2,n1);}}/*xt020409.cpp*/#includevoidmain(){floatsn=100,hn=sn/2;intn=2;while(n<=10){sn+=2*hn;hn=hn/2;n++;}printf("%f,%fn",sn,hn);}/*xt020410.cpp*/#includevoidmain(){intday,x1,x2=1;for(day=9;day>0;day--){x1=(x2+1)*2;x2=x1;}printf("Total=%dn",x1);}
习题3单项选择题BBCACBDABB填空题①9②0③10④x⑤一致⑥n/2⑦sum+a⑧sum/n三、阅读程序题1.2462.s=303.24224214.lanuge5.8163574926.max=10,row=2,colum=1四、程序设计题//xt030401.cpp#include#include#includevoidmain(void){ intnum[10],a;; srand((unsigned)time(NULL)); printf("请输入一个2位数,试试您是否能中奖:");scanf("%d",&a); for(inti=0;i<10;i++) {num[i]=(rand()+10)%100; if(a==num[i]) printf("猜中一个数%d下标是%d",a,i); } }printf("-------------------------n"); for(i=0;i<10;i++)printf("%dt",num[i]);}
//xt030402.cpp#include#include#includevoidmain(void){inta[5]={23,45,60,67,88},x,i,j,yes=0;for(i=0;i<5;i++)printf("%d,",a[i]);printf("nwhichnumberdoyouwanttodelete?n");scanf("%d",&x);for(i=0;i<5;i++)if(a[i]==x){yes=1;for(j=i;j<5;j++)a[j]=a[j+1];}if(yes==1)for(i=0;i<4;i++)printf("%d,",a[i]);elseprintf("Thenumberyouwantdoesn"texist");}//xt030403.cpp#include#include#include#defineSIZE1000voidmain(){intx,a[SIZE],size,sum=0; srand((unsigned)time(NULL));while((size=rand()%1000)>200);printf("nRandomizlistsize(<1000):%d",size);printf("nAutomakea%dsizeoflist:n",size);for(x=0;x#include#include#defineSIZE100voidmain(){
intx,a[SIZE],max=0,min=1000,b1,b2; srand((unsigned)time(NULL));printf("nAutomakea%dsizeoflist:n",size);for(x=0;xmax)max=a[x],b1=x;if(a[x]#defineSIZE100#defineKIND5voidmain(){intx,y,len,total[KIND+1];inta[]={1,1,1,1,2,2,2,3,5,5,5,5,1,1,3,3,3,3,3,2,2,2,5,5,5};len=sizeof(a)/sizeof(a[0]);for(x=0;xintmain(void){staticinta[N+1],b[N+1];inti,j,s,m,p;
scanf("%d%d",&s,&m);for(i=1;i<=N;i++)a[i]=i;i=s-1;j=0;p=0;while(pN)i-=N;if(a[i]==0){i++;gotobh;}j++;if(j%m==0){p++;b[p]=a[i];a[i]=0;j=0;}}for(i=1;i<=N;i++)printf("%3d",b[i]);printf("n");}//xt030407.cpp#includevoid main(){int i,j,s=0,ave,v[3];static int a[5][3]={ {80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85} };for(i=0;i<3;i++){ for(j=0;j<5;j++){s=s+a[j][i];v[i]=s/5;s=0;}ave=(v[0]+v[1]+v[2])/3;printf("math:%dnc languag:%dndbase:%dn",v[0],v[1],v[2]);printf("totalave:%dn",avel);}//xt030408.cpp#includevoidmain(){inti,j,a[10][10],b[]={1,2,3,4,5,6,7,8,9,10};for(i=0;i<10;i++)a[0][i]=b[i];for(i=1;i<10;i++)for(j=0;j<10;j++)if(i>j)a[i][j]=a[j][i];elsea[i][j]=a[i-1][j-1];for(i=0;i<10;i++){for(j=0;j<10;j++)printf("%dt",a[i][j]);
printf("n");}}//xt030409.cpp#include#include#include#defineSIZE5000#defineN7voidmain(){intx,y,a[N]={0,0,0,0,0,0,0},s; srand((unsigned)time(NULL));x=0;while(x0&&s<7){a[s]++;x++;}}printf("nRandomiz1-6listn");for(x=1;x#defineN20intmain(){intx,s,tag;chara[N];printf("Pleaseinputnumbersandit"slengthlessthen%d:",N-1);s=1;while(svoidmain()
{voiddis(intn);intn;scanf("%d",&n);dis(n);}voiddis(intn){intk0,k,sum,i;k0=1;do{k=k0;sum=0;for(i=1;i<=n;i++){sum=sum+k;k=k+2;}if(sum==n*n*n)break;elsek0=k0+2;}while(1);for(i=1;i<=n;i++){printf("%5d",k0);k0=k0+2;}}//xt040402.cpp#includevoidmain(){floath(intn,floatx);intn;floatx;scanf("%d,%f",&n,&x);printf("h(%d,%f)=%f",n,x,h(n,x));}floath(intn,floatx){if(n==0)return1;elseif(n==1)return(2*x);elsereturn(2*x*h(n-1,x)-2*(n-1)*h(n-2,x));}//xt040403.cpp#include
voidmain(){intf(intn);inti;for(i=2;i<=1000;i++)if(f(i))printf("%6d",i);}intf(intn){if(n==n*n%10||n==n*n%100||n==n*n%1000)return1;elsereturn0;}//xt040404.cpp#includevoidmain(){longfun(inta,intn);intk,n;scanf("%d,%d",&k,&n);printf("%ldn",fun(k,n));}longfun(inta,intn){intj;longs=0,t=0;for(j=1;j<=n;j++){t=t*10+a;s=s+t;}returns;}//xt040405.cpp#include#includevoidmain(){intnum,middle1,middle2;intdetect(intd);printf("Pleaseinputapositiveevennumber(>=6):");scanf("%d",&num);for(middle1=2;middle1<=num/2;middle1+=1)if(detect(middle1)){middle2=num-middle1;if(detect(middle2)){printf("%d=%d+%dn",num,middle1,middle2);break;
}}}intdetect(intd){inti;for(i=2;i<=sqrt(d*1.0);i++)if(d%i==0)return(0);return1;}//xt040406.cpp#includevoidmain(){floatf(floatx,intn);floatx,an;intn;scanf("%f,%d",&x,&n);an=f(x,n);printf("an=%fn",an);}floatf(floatx,intn){if(n==0)return1;elsereturnx/n*f(x,n-1);}//xt040407.cpp#includevoidmain(){floatf(floatx,intn);floatx,fn;intn;scanf("%f,%d",&x,&n);fn=f(x,n);printf("an=%fn",fn);}floatf(floatx,intn){if(n==0)return1;elseif(n>0)returnx*f(x,n-1);elsereturnf(x,n+1)/x;}
//xt040408.cpp#includevoidmain(){voidf(intn);intn;scanf("%d",&n);while(n<=0)scanf("%d",&n);f(n);}voidf(intn){if(n/10==0)putchar(n%10+"0");else{putchar(n%10+"0");f(n/10);}}//xt040409.cpp#includevoidmain(){voidf(intn);intn;scanf("%d",&n);while(n<=0)scanf("%d",&n);f(n);}voidf(intn){if(n/10)f(n/10);putchar(n%10+"0");}//xt040410.cpp#includevoidmain(){intf(intn);intn;for(n=2;n<=1000;n++)if(f(n))printf("%disa完数n",n);}intf(intn){inti,s=0;
for(i=1;iintmax(inta,intb){if(a>b)returna;elsereturnb;}voidmain(){intmax(inta,intb);int(*pmax)(int,int);intx,y,z;pmax=max;printf("inputtwonumbers:n");scanf("%d%d",&x,&y);z=(*pmax)(x,y);printf("maxmum=%d",z);}//xt050403.cpp#include"stdio.h"voidgcd(intx,inty){inttemp;while(y!=0){temp=x%y;x=y;y=temp;}printf("Thegreatestcommondivisoris:%dn",x);}voidlcm(intx,inty){inttemp,m,n;m=x;n=y;while(x!=0){temp=y%x;y=x;x=temp;}
printf("Thelowestcommonmultipleis:%dn",m*n/y);}voidmain(){inta,b;void(*p)(int,int);printf("Pleaseinputaandb:");scanf("%d%d",&a,&b);if(a>b)p=gcd;elsep=lcm;(*p)(a,b);}//xt050404.cpp#include"stdio.h"voidmain(){intx,y;intsum(int,int),diff(int,int),product(int,int);voidmma(intx,inty,ints(int,int));printf("Pleaseinputx,y:n");scanf("%d%d",&x,&y);printf("Thesumofxandyis:");mma(x,y,sum);printf("Thedifferenceofxandyis:");mma(x,y,diff);printf("Theproductofxandyis:");mma(x,y,product);}intsum(intx,inty){returnx+y;}intdiff(intx,inty){returnx-y;}intproduct(intx,inty){returnx*y;}voidmma(inta,intb,ints(int,int)){intw;w=s(a,b);printf("%dn",w);}
//xt050405.cpp#includedoublefunc1(intx),func2(inty);voidmain(){double(*fptr)(int);/*定义指向函数的指针变量fptr*/intn;printf("inputanumber:");scanf("%d",&n);if(n>1){if(n%2==0)fptr=func1;/*n为偶数,指针变量ptr指向函数func1()*/elsefptr=func2;/*n为奇数,指针变量ptr指向函数func2()*/printf("value=%9.4fn",(*fptr)(n));}elseprintf("error!n");}doublefunc1(intx){intk;doublevalue;value=1.0;for(k=2;k<=x;k=k+2)value=value+1/(double)k;returnvalue;}doublefunc2(inty){intk;doublevalue;value=1.0;for(k=3;k<=y;k=k+2)value=value+1/(double)k;returnvalue;}//xt050406.cpp#include"stdio.h"#include"math.h"voidmain(){intn,yinzi(int),(*f)(int);f=yinzi;printf("inputanumbern:");scanf("%d",&n);if((*f)(n)==2)printf("n%disaprimenumber.",n);
elseprintf("n%disnotaprimenumber.",n);}intyinzi(intx){inti,k=0,q=(int)sqrt(x);for(i=1;i<=q;i++)if(x%i==0){k+=2;printf("%5d%5d",i,x/i);}returnk;}//xt050407.cpp#include#includedoublef1(doublex){returnx*x*log(x);}doublef2(doublex){returnx*sin(x);}doublef3(doublex){returnx/exp(x);}doublecollect(double(*p)(doublex),doublea,doubleb,doublen){inti;doubleh,area;h=(b-a)/n;area=((*p)(a)+(*p)(b))/2.0;for(i=1;i=eps));if(m==0)return(0);return(1);}doublef1(doublex){return(1.0+atan(x));}doublef2(doublex){return(0.5*cos(x));}doublef3(doublex){return((6.0+3*x-x*x)/4);/*x=(6+3*x-x*x)/4*/}voidmain(){doublex,(*p)(double);x=1.0;p=f1;if(root(&x,50,0.00001,p))printf("x1=%fn",x);p=f2;if(root(&x,50,0.00001,p))printf("x1=%fn",x);p=f3;if(root(&x,50,0.00001,p))printf("x1=%fn",x);}//xt050409.cpp#include"stdio.h"int*f(intp[],intq[],intm){inti;for(i=0;i#includevoidmain(){intwanshu(intx);int*ptr;ptr=(int*)malloc(sizeof(int));if(ptr==NULL){printf("Failedtocreateanewobject.");exit(0);}scanf("%d",ptr);if(wanshu(*ptr))printf("%disa完数:",*ptr);elseprintf("%disnota完数:",*ptr);free(ptr);}intwanshu(intx){inti=1,sum=0;while(ivoidmain(){intyh[11],row,col,i;*(yh+1)=1;for(i=0;i<40-2;i++)printf("");printf("%4dn",*(yh+1));for(row=2;row<=10;row++){*(yh+row)=1;/*每行的最后一个元素值为1*/for(col=row-1;col>=2;col--)/*生成一行*/*(yh+col)=*(yh+col)+*(yh+col-1);for(i=0;i<40-2*row;i++)/*输出合适的空格*/printf("");for(col=1;col<=row;col++)/*输出一行*/printf("%4d",*(yh+col));printf("n");}
}//xt060402.cpp#include#include#includevoidmain(){inta[10][5],b[10],i,j;srand(time(NULL));for(i=0;i<10;i++)for(j=0;j<5;j++)*(a[i]+j)=rand()%100;for(i=0;i<10;i++){*(b+i)=a[i][0];for(j=0;j<5;j++)if(*(b+i)<*(*(a+i)+j))*(b+i)=*(*(a+i)+j);}for(i=0;i<10;i++){for(j=0;j<5;j++)printf("%4d",a[i][j]);printf("t%4dn",b[i]);}}//xt060403.cpp#include#include#includeintdelmem(int*v,intn,intdel);voidmain(){int*a,i,n,del;srand(time(NULL));printf("请输入处理的数组长度:");scanf("%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i#include#include#defineN15voidmain(){intscore[N][4]={0},i,j,x;srand(time(NULL));for(i=0;i#include#include#defineN15
voidmain(){intscore[N][4]={0},i,j,x;intt[4];srand(time(NULL));for(i=0;iscore[x][3])x=j;if(x!=i)for(j=0;j<4;j++){*(t+j)=*(*(score+x)+j);*(*(score+x)+j)=*(*(score+i)+j);*(*(score+i)+j)=*(t+j);}}printf("score1tsocre2tsocre3tsumn");for(i=0;i#includevoidmain(){int*ArrayCat(int*s,intslen,int*t,inttlen);inta[]={1,3,5,7,9},b[]={2,4,6,8,10},*p=NULL,i;p=ArrayCat(a,sizeof(a)/sizeof(int),b,sizeof(b)/sizeof(int));for(i=0;i<(sizeof(a)+sizeof(b))/sizeof(int);i++)printf("%4d",p[i]);printf("n");
}int*ArrayCat(int*s,intslen,int*t,inttlen){int*st,i;st=(int*)malloc(sizeof(int)*(slen+tlen));for(i=0;i#include#includevoidmain(){voidreverse(int*v,intn);int*a,n,i;srand(time(NULL));printf("请输入数组的长度:");scanf("%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i#include#includeint*ArrayCopy(int*source,intn);voidmain(){int*a,*b=NULL,n,i;srand(time(NULL));
printf("请输入被拷贝的数组长度:");scanf("%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i#include#includevoidsort(intv[],intn);voidreverse(intv[],intn);voidmain(){int*data,i,n;srand(time(NULL));printf("请输入需要处理的数据个数:");scanf("%d",&n);data=(int*)malloc(sizeof(int)*n);for(i=0;i#include#includevoidsort(intv[],intn);voidreverse(intv[],intn);voidmain(){intm,n,i,j,**row;srand(time(NULL));printf("请输入二维数组的行数:");scanf("%d",&m);printf("请输入二维数组的列数:");scanf("%d",&n);row=(int**)malloc(sizeof(int*)*m);for(i=0;i