程序设计技术习题参考答案.docx
《程序设计技术习题参考答案.docx》由会员分享,可在线阅读,更多相关《程序设计技术习题参考答案.docx(92页珍藏版)》请在冰豆网上搜索。
程序设计技术习题参考答案
《程序设计技术》(第三版)习题参考答案
习题1
一、单项选择题
DADDDBCCD(注:
第7小题无正确选项,正确答案为-12)
二、填空题
①主
②main
③x=x*(x+b)
④普通、格式、分隔和转义
⑤指定输出数据格式
⑥voidmain()
⑦"howareyou!
"
三、阅读程序题
1.9,11,9,10
2.2,-1,-1
3.aabbccabc
4.2,2
5.57
57
67.5864000,-789.124023
67.5864000,-7.89e+002
67,86,-789.12,67.5864000,-789.124023,67.5864000,-789.124023
6.758640e+001,-7.89e+002
A,65,101,41
1234567,4553207,12d687
65535,177777,ffff,65535
COMPUTER,COM.
6.a+c=102
a+c=f
f+m=17.950000
a+m=17.650000
c+f=102.300000
double=1746.150019
四、程序设计题
//xt010401.cpp
#include
#definePI3.1415926
voidmain()
{doubler,h;
printf("请输入半径r和高h:
");
scanf("%lf,%lf",&r,&h);
printf("圆半径:
%lf\n",2*PI*r);
printf("圆面积:
%lf\n",PI*r*r);
printf("圆球表面积:
%lf\n",4*PI*r*r);
printf("圆球体积:
%lf\n",4.0/3*PI*r*r);
printf("圆柱体积:
%lf\n",PI*r*r*h);
}
//xt010402.cpp
#include
voidmain()
{intc;
printf("Inputacharacter:
");
c=getchar();
printf("%c\t%d\n",c,c);
}
//xt010403.cpp
#include
voidmain()
{intx,y,h=30,f=90;
x=(4*h-f)/2;
y=(f-2*h)/2;
printf("鸡:
%d,兔:
%d\n",x,y);
}
//xt010404.cpp
#include
#include
voidmain()
{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("三角形的面积是:
%lf\n",area);
}
//xt010405.cpp
#include
voidmain()
{doublef,c;
printf("请输入华氏温度:
");
scanf("%lf",&f);
c=5.0/9.0*(f-32);
printf("与华氏温度%.2lf对应的摄氏温度是:
%.2lf\n",f,c);
}
//xt010406.cpp
#include
voidmain()
{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*/
#include
voidmain()
{intnum;
printf("****Inputnum:
****\n");
scanf("%d",&num);
if(num%3==0&&num%5==0&&num%7==0)
printf("**YES!
**\n");
else
printf("**NO!
**\n");
}
/*xt020402.cpp*/
#include
voidmain()
{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=%f\n",x,y);
printf("h=%d\n",h);
}
/*xt020403.cpp*/
#include
voidmain()
{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("%d\n",n);
}
}
/*xt020404.cpp*/
#include
#include
#defineEPS1.0e-6
voidmain()
{
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=%f\n",e);
}
/*xt020405.cpp*/
#include
voidmain()
{intnum=1;
do
{if((num+3)%5==0&&(num-3)%6==0)
break;
num+=1;
}while
(1);
printf("num=%d\n",num);
}
/*xt020406.cpp*/
#include
voidmain()
{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*/
#include
voidmain()
{intn,k=1;
floats=0;
for(n=1;n<=10000;n++)
{s=s+1.0/(2.0*n-1)*k;
k=-k;
}
printf("PI=%f\n",4*s);
}
/*xt020408.cpp*/
#include
voidmain()
{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=%d\n",n10,n5,n2,n1);
}
}
/*xt020409.cpp*/
#include
voidmain()
{floatsn=100,hn=sn/2;
intn=2;
while(n<=10)
{sn+=2*hn;
hn=hn/2;
n++;
}
printf("%f,%f\n",sn,hn);
}
/*xt020410.cpp*/
#include
voidmain()
{intday,x1,x2=1;
for(day=9;day>0;day--)
{x1=(x2+1)*2;
x2=x1;
}
printf("Total=%d\n",x1);
}
习题3
单项选择题
BBCACDDABB
填空题
①9
②0
③10
④x
⑤一致
⑥n/2
⑦sum+a
⑧sum/n
三、阅读程序题
1.246
2.s=30
3.5945842
4.lanuge
5.816
357
492
6.max=10,row=2,colum=1
四、程序设计题
//xt030401.cpp
#include
#include
#include
voidmain(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("%d\t",num[i]);
}
//xt030402.cpp
#include
#include
#include
voidmain(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
#defineSIZE1000
voidmain()
{
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{a[x]=rand()%1000;
printf("%d\t",a[x]);
sum+=arr[x];
}
printf("\nThesumofarrayelementsis%d",sum);
}
//xt030404.cpp
#include
#include
#include
#defineSIZE100
voidmain()
{
intx,a[SIZE],max=0,min=1000,b1,b2;
srand((unsigned)time(NULL));
printf("\nAutomakea%dsizeoflist:
\n",size);
for(x=0;x{a[x]=rand()%1000;
printf("%d\t",a[x]);
}
for(x=0;x{
if(a[x]>max)max=a[x],b1=x;
if(a[x]}
printf("\nThearrayelementsmax=%din%d\nmin=%din%d\n",max,b1,min,b2);
}
//xt030405.cpp
#include
#defineSIZE100
#defineKIND5
voidmain()
{
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;x{total[x]=0;
}
for(y=1;y{
for(x=0;x{
if(a[x]==y)total[y]++;
}
}
printf("\nThelistkinded:
\n");
for(x=0;xprintf("\nThetotalresult:
");
for(x=1;x}
//xt030406.cpp
#defineN26
#include
intmain(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(p{i++;
bh:
if(i>N)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
#include
void 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:
%d\nc languag:
%d\ndbase:
%d\n",v[0],v[1],v[2]);
printf("totalave:
%d\n",avel);
}
//xt030408.cpp
#include
voidmain()
{
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("%d\t",a[i][j]);
printf("\n");
}
}
//xt030409.cpp
#include
#include
#include
#defineSIZE5000
#defineN7
voidmain()
{
intx,y,a[N]={0,0,0,0,0,0,0},s;
srand((unsigned)time(NULL));
x=0;
while(x{s=rand()%6+1;
if(s>0&&s<7)
{a[s]++;
x++;
}
}
printf("\nRandomiz1-6list\n");
for(x=1;x{
s=a[x]*0.07;
for(y=0;y
printf("%d",x);
printf("\t%d\t%2.1f%%\n",a[x],(float)a[x]*100/SIZE);
}
}
//xt030410.cpp
#include
#defineN20
intmain()
{
intx,s,tag;
chara[N];
printf("Pleaseinputnumbersandit'slengthlessthen%d:
",N-1);
s=1;
while(s{scanf("%c",&a[s]);
if(a[s]==10)break;
s++;
}
printf("\nThenumbersis:
\n");
for(x=1;x
{
printf("%c",a[x]);
}
tag=0;
for(x=1;x
{
if(a[x]!
=a[s-x])
{tag=1;
break;
}
}
if(tag==0)printf("\nThenumberispalindrome!
");
elseprintf("\nThenumberisn'tpalindrome!
");
}
习题4
一、单项选择题
ABACABCBDD
二、填空题
①主
②main函数
③main函数
④个数
⑤类型
⑥顺序
⑦函数类型
⑧float(floatx,intn)
⑨f(x,n)
⑩x*f(x,n-1)
三、阅读程序题
1.x=5,y=25
2.y=170.0
3.s1=3.14,s2=5.14
4.5002
5.5,3
6.3.00,1.00,0.25
四、程序设计题
//xt040401.cpp
#include
voidmain()
{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;
else
k0=k0+2;
}while
(1);
for(i=1;i<=n;i++)
{printf("%5d",k0);
k0=k0+2;
}
}
//xt040402.cpp
#include
voidmain()
{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);
else
return(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;
else
return0;
}
//xt040404.cpp
#include
voidmain()
{longfun(inta,intn);
intk,n;
scanf("%d,%d",&k,&n);