C语言参考答案Word格式.docx
《C语言参考答案Word格式.docx》由会员分享,可在线阅读,更多相关《C语言参考答案Word格式.docx(52页珍藏版)》请在冰豆网上搜索。
![C语言参考答案Word格式.docx](https://file1.bdocx.com/fileroot1/2023-1/27/3519d9e5-1ce9-45e7-b925-117e635ca360/3519d9e5-1ce9-45e7-b925-117e635ca3601.gif)
7. –03
8. 7
9. 5.0,4,c=3<
Enter>
10. i=10,j=20<
11.
(1)65
(2)65,A
(3)56.123400,123.456001
(4)3.141600
(5)8765.432
(6)5.86400e+02
(7)3.14160e+00
(8)3.1416
(9)8765
(10)3.1416,8765.43
12. a=2b=5x=8.8y=76.34c1=65c2=97
3-3编程题
{intx,y;
scanf(“%d%d”,&
x,&
y);
printf(“xy\n”);
printf(“%4d%4d\n”,x,y);
printf(“%4o%4o\n”,x,y);
printf(“%4x%4x\n”,x,y);
{floata,b,h,s;
scanf(“%f%f%f”,&
a,&
b,&
h);
s=(a+b)*h/2;
printf(“%8.1f\n”,s);
3.
{charc;
scanf(“%c”,&
c);
printf(“%c,%c,%c\n”,c-1,c,c+1);
4.
{inta,a1,a2,a3;
scanf(“%d”,&
a);
a1=a%10;
a=a/10;
a2=a%10;
a3=a%10;
printf(“%d%d%d\n”,a1,a2,a3);
5.
{doublea,b,c,ave;
scanf(“%lf%lf%lf”,&
ave=(a+b+c)/3;
printf(“%8.2f\n”,ave);
6.
{inta,b,c,t;
scanf(“%d%d%d”,&
t=a;
a=c;
c=b;
b=t;
printf(“a=%d,b=%d,c=%d\n”,a,b,c);
习题4
4-1单选题
1~5BADAC6~10DDACD11~15BBBAB
4-2填空题
1.1
2.if(x>
0)y=1
elseif(x==0)y=0
elsey=-1
3.
(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
4.
(1)0
(2)1(3)1(4)0(5)0
5.max=(max=a>
b?
a:
b)>
c?
max:
6.-4
7.1
8.5,0,3
9.85belongstoB
10.
(1)(a==0)
(2)(b==0)(3)(disc<
0)
4-3编程题
1.#include“math.h”
main()
{floata,b,c,s,area;
scanf(“%f,%f,%f”,&
a,&
b,&
c);
if(a+b>
c&
a+c>
b&
b+c>
a)
{s=(a+b+c)/2;
printf(“%f”,area);
2.main()
{intx,y;
scanf(“%d,%d”,&
if(x*x+y*y>
1000)
printf(“%d\n”,(x*x+y*y)/100);
else
printf(“%d\n”,x+y);
3.main()
{intx,
scanf(“%d”,&
x);
if(x%3==0&
x%5==0&
x%7==0)
printf(“yes\n”);
printf(“no\n”);
4.#include“math.h”
{floatx,y;
scanf(“%f”,&
if(x<
-2)y=x*x-sin(x);
elseif(x<
=2)y=pow(2,x)+1;
elsey=sqrt(x*x+x+1);
printf(“%f\n”,y);
5.main()
{
longa,b,c,d,e,x;
scanf("
%ld"
&
a=x/10000;
/*分解出万位*/
b=x%10000/1000;
/*分解出千位*/
c=x%1000/100;
/*分解出百位*/
d=x%100/10;
/*分解出十位*/
e=x%10;
/*分解出个位*/
if(a!
=0)printf("
thereare5,%ld%ld%ld%ld%ld\n"
e,d,c,b,a);
elseif(b!
thereare4,%ld%ld%ld%ld\n"
e,d,c,b);
elseif(c!
thereare3,%ld%ld%ld\n"
e,d,c);
elseif(d!
thereare2,%ld%ld\n"
e,d);
elseif(e!
thereare1,%ld\n"
e);
6.1.程序分析:
main()
longge,shi,qian,wan,x;
wan=x/10000;
qian=x%10000/1000;
shi=x%100/10;
ge=x%10;
if(ge==wan&
shi==qian)/*个位等于万位并且十位等于千位*/
printf("
thisnumberisahuiwen\n"
);
else
thisnumberisnotahuiwen\n"
7.main()
{floatp,w,s,d,f;
scanf(“%f,%,%f”,p,s,w);
if(s>
3000)d=0.15
elseif(s>
=2000)d=0.1;
elseif(s>
=1000)d=0.08;
=500)d=0.05;
=250)d=0.02;
elsed=0
f=p*w*s*(1-d);
printf(“%f”,f);
习题5
5-1单选题
1~5CDACB6~10DCAAB11~16DBDBCB
5-2填空题
1.20
2.k=5,s=4
3.333
4.HowAreYou
5.2#18#
6.
(1)i<
10
(2)j%3!
=0
7.
(1)flag*(float)k/(k+1)
(2)flag=-flag
8.
(1)s=0,t=1;
(2)t=t*x/i*pow(-1,i+1);
(3)printf(“%f”,s);
9.
(1)max=x
(2)x!
=-1(3)scanf("
%d"
&
x)
10.
(1)n=1
(2)s
5-3编程题
1.
(1)main()
{inti;
lonts=0;
for(i=1;
i<
=100;
i++)
s+=i*i;
printf(“%ld”,s);
(2)main()
lontp=1,s=0;
for(i=1;
=10;
{p=p*i;
s+=p;
(3)main()
{inti=1;
lontp=1;
floats=0;
do{
s+=1.0/p;
p*=++i;
}while(1.0/p>
1e-6)
printf(“%f”,s);
(4)main()
{inti=1,flag=1,pi=0;
s+=flag*1.0/(2*i-1);
i++;
}while(1.0/(2*i-1)>
1e-6);
printf(”%f”,s);
2.main()
{intm,n,t,a,b;
scanf(“%d,%d”
m,&
n)
;
if(m<
n){t=m
m=n
n=t
a=m;
b=n;
t=m%n
while(t)
{m=n
n=t
t=m%n;
printf(”%d,%d”,n,a*b/n);
3.main()
{intx,y,s=1;
scanf(”%d,%d
”,&
y)
for(
y>
0
y--)s*=x
printf(”%d,%d,%d\n
”,s%10,s/10%10,s/100%10);
4.main()
{inti=1,k=2,sum=0;
sum+=k;
k=k*2;
}while(sum<
=100);
printf(”total=%f\n”,0.4*sum/(i-1));
5.main()
{intx,y,z;
for(x=1
20
x++)
for(y=1
y<
33
y++)
{z=100-x-y
if(z>
=0&
(5*x+3*y+z/3)-100<
1e-5)
printf(”x=%d,y=%d,z=%d\n”,x,y,z)
6.main()
{intj,k
for(j=1
j<
=4
j++)
{for(k=1;
k<
=4-j;
k++;
)printf(””);
for(k=1
=2*j-1
k++)
printf(”*”)
printf(”\n”)
for(j=3;
j>
=1;
j--)
{for(k=1;
for(k=1;
=2*j-1;
printf(”*”);
printf(”\n”);
7.分析:
其实此问题的解法非常简单。
从数学上来说,可以用穷举法。
比如让x从2开始,判断表达式“(x%3==2&
x%5==3&
x%7==2)”是否成立,若不成立,让x自增1,直到某个x满足条件了,则这个x即为所求。
程序如下:
#include<
stdio.h>
{
intx=2;
while(!
(x%3==2&
x%7==2))
x++;
printf("x=%d\n",x);
程序运行结果:
x=23
8分析:
定义一个字符变量c和一个整型变量n,c用于接受从键盘输入的非回车行字符,n用于统计从键盘输入的非回车符字符的个数,也就是说,以(c=getchar())!
='
\n'
为循环条件,反复进行“n++;
”的运算。
程序代码如下:
#include<
voidmain(){
intn=0;
charc;
printf("
inputastring:
\n"
while((c=getchar())!
)n++;
n);
9.例如:
153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
程序分析:
利用for循环控制100-999个数,每个数分解出个位,十位,百位。
inti,j,k,n;
'
waterflower'
numberis:
"
for(n=100;
n<
1000;
n++)
{
i=n/100;
j=n/10%10;
k=n%10;
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
{
printf("
%-5d"
}
}
10.main()
{intx,y,z;
x<
17
for(y=1
25
{z=30-x-y
if(z>
3*x+2*y+z==50)
printf("
man=%d,woman=%d,child=%d\n”,x,y,z);
}
习题6
6-1单选题
1-5 CBBAD6-10 DBAAC11-15 CCDDC16-20 BBABC
6-2填空题
1. 120
2. x
3. 3,2,2,34./I
5. 8,17
6. 9
7. 1.0/(i*i)
8. fun-in:
30,20,10
fun-end:
1015,35,1050
10,20,30
9. 01234510.93636
6-3编程题
voidzhuan()
{charch;
while((ch=getchar())!
=’\n’)
{if(ch>
=’a’&
ch<
=’z’)ch=ch-32;
putchar(ch);
floatexpp(intn)
{intk,fac=1;
floatsum=0;
k<
=n;
k++)
{fac*=k;
sum+=1.0/fac;
return(sum);
intxy3(intx,inty)
{intk,num=1;
=y;
num*=x;
num=num%1000;
returnnum;
voidfactors(intn)
{intk;
printf(“%ditsfactorsare:
”,n);
n;
if(n%k==0)
printf(“%d,”,k);
printf(“\n”);
intage(intn)
{intc;
if(n==1)c=10;
elsec=age(n-1)+2;
returnc;
习题7
7-1单选题
1-5 DBCCB6-8BDC
7-2填空题
1. c
2. 60
3. 1000 10
4. 16
7-3编程题
1.
#defineF(a)a*a+sqrt(3*a*a+2*a+1)
{floatx,f;
scanf(“%f”,&
x);
f=4.5/F(exp(x))+F(cos(x))+F(sqrt(x))/F(x*x);
printf(“%f\n”,f);
习题8
8-1单选题
1~15CADAC6~10CCDAA11~16BCBBCD
10000
01000
00100
00010
00001
8-2填空题
1
(1)@*&
$
(2)1345(3)10010(4)QuickC(5)
2.
(1)j+=2
(2)a[i]>
a[j]
3.
(1)r+b[k]
(2)*x
8-3编程题
1.#defineN10
{inta[N]={1,2,3,4,5,6,7,8,9,0},osum=0,qsum=0,j;
for(j=0;
10;
if(j%2)qsum+=a[j];
elseosum+=a[j];
printf(“osum=%d,qsum=%d\n”,osum,qsum);
2.#defineN10
{inta[N]={10,20,30,40,50,60,70,80,90},j,k,x;
N;
if(x<
a[j])break;
for(k=N-1;
k>
j;
k--)
a[k]=a[k-1];
a[j]=x;
for(j=0;
printf(“%d”,a[j]);
3.#defineN10
{inta[N]={1,2,3,4,5,6,7,8,9,0}.b[N-1],j;
for(j=0;
N-1;
b[j]=a[j+1]-a[j];
{if(j%3==0)printf(“\n”);
printf(“%d”,b[j]);
4.#defineM3
{inta[M][M]={{1,2,3},{4,5,6},{7,8,9}},j,sum1=0,sum2=0;
M;
{sum1+=a[j][j];
sum2+=a[j][M-(j+1)];
printf(“%d,%d\n”,sum1,sum2);
5.#defineM3
{inta[M][M]={{1,2,3},{2,4,5},{3,5,6}},j,k,flag=1;
for(k=0;
if(a[j][k]!
=a[k][j]){flag=0;
break;
if(flag)printf(“ok”);
elseprintf(”NO”);
6.#include“string.h”
{charc[10],j;
gets(c);
c[j];
c[j]=(c[j]>
c[j]<
=’z’)?
c[j]-32:
puts(c);
7.#include“string.h”
{charc1[10],c2[10],j;
gets(c1);
gets(c2);
(c1[j]==c2[j])&
c1[j]&
c2[j];
j++);
printf(“%d\n”,c1[j]-c2[j]);
8.#include”string.h”
#defineM3
#defineN80
{chara[M][N],j,k,n[5]={0};
gets(a[j]);
a[j][k];
if(a[j][k]>
=’A’&
a[j][k]<
=’Z’)n[1]++;
elseif(a[j][k]>
=’z’)n[2]++;
=’0’&
=’9’)n[3]++;
elseif(a[j][k]=’’)n[4]++;
n[5]++;
5;
printf(“%4d”,n[j]);
习题9
9-1单选题
1~5DDBAC6~10BADCB11~15ADCDA16~20CCABD
9-2填空题
1.
(1)2,1
(2)10#30#5#(3)FOUR,O(4)60
2.
(1)49
(2)AC(3)2(4)2(5)3,12,39(6)75319(7)15
01234
56789
1011121314
0522713
(8)12,10,9,6,5,4,3,2,1(9)
1,1,1
1,1
3,3,3
3,3
(10)
10001
01010
00100
(11)
3.
(1)*x
(2)t
4.
(1)'
\0'
或0
(2)n++或n+=1或n=n+1
5.ABCDCD
6.024
7.voidfun(doubleb[][22])
或voidfun(doubleb[0][22])
或