C程序设计谭浩强课后习题答案共11章.docx
《C程序设计谭浩强课后习题答案共11章.docx》由会员分享,可在线阅读,更多相关《C程序设计谭浩强课后习题答案共11章.docx(76页珍藏版)》请在冰豆网上搜索。
C程序设计谭浩强课后习题答案共11章
1-5#include<>
intmain()
{printf("**************************\n\n");
printf("VeryGood!
\n\n");
printf("**************************\n");
return0;
}
1-6#include<>
intmain()
{inta,b,c,max;
printf("pleaseinputa,b,c:
\n");
scanf("%d,%d,%d",&a,&b,&c);
max=a;
if(max
max=b;
if(maxmax=c;
printf("Thelargestnumberis%d\n",max);
return0;
}
3-1#include<>
#include<>
intmain()
{floatp,r,n;
r=;
n=10;
p=pow(1+r,n);
printf("p=%f\n",p);
return0;
}
3-2-1
#include<>
#include<>
intmain()
{floatr5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;
p=1000;
r5=;
r3=;
r2=;
r1=;
r0=;
p1=p*((1+r5)*5);#include<>
#include<>
intmain()
{floatd=300000,p=6000,r=,m;
m=log10(p/(p-d*r))/log10(1+r);
printf("m=%\n",m);
return0;
}
3-4
#include<>
intmain()
{intc1,c2;
c1=197;
c2=198;
printf("c1=%c,c2=%c\n",c1,c2);
printf("c1=%d,c2=%d\n",c1,c2);
return0;
}
3-5
#include<>
intmain()
{inta,b;
floatx,y;
charc1,c2;
scanf("a=%db=%d",&a,&b);
scanf("%f%e",&x,&y);
scanf("%c%c",&c1,&c2);
printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2);
return0;
}
3-6
#include<>
intmain()
{charc1='C',c2='h',c3='i',c4='n',c5='a';
c1=c1+4;
c2=c2+4;
c3=c3+4;
c4=c4+4;
c5=c5+4;
printf("passworis%c%c%c%c%c\n",c1,c2,c3,c4,c5);
return0;
}
3-7
#include<>
intmain()
{floath,r,l,s,sq,vq,vz;
floatpi=;
printf("请输入圆半径r,圆柱高h∶");
scanf("%f,%f",&r,&h);#include<>
intmain()
{intx,y;
printf("输入x:
");
scanf("%d",&x);
if(x<1)/*x<1*/
{y=x;
printf("x=%3d,y=x=%d\n",x,y);
}
elseif(x<10)/*1={y=2*x-1;
printf("x=%d,y=2*x-1=%d\n",x,y);
}
else/*x>=10*/
{y=3*x-11;
printf("x=%d,y=3*x-11=%d\n",x,y);
}
return0;
}
4-8
#include<>
intmain()
{floatscore;
chargrade;
printf("请输入学生成绩:
");
scanf("%f",&score);
while(score>100||score<0)
{printf("\n输入有误,请重输");
scanf("%f",&score);
}
switch((int)(score/10))
{case10:
case9:
grade='A';break;
case8:
grade='B';break;
case7:
grade='C';break;
case6:
grade='D';break;
case5:
case4:
case3:
case2:
case1:
case0:
grade='E';
}
printf("成绩是%,相应的等级是%c\n",score,grade);
return0;
}
4-10-1
#include<>
intmain()
{
inti;
doublebonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*;
bon2=bon1+100000*;
bon4=bon2+100000*;
bon6=bon4+100000*;
bon10=bon6+400000*;
printf("请输入利润i:
");
scanf("%d",&i);
if(i<=100000)
bonus=i*;
elseif(i<=200000)
bonus=bon1+(i-100000)*;
elseif(i<=400000)
bonus=bon2+(i-200000)*;
elseif(i<=600000)
bonus=bon4+(i-400000)*;
elseif(i<=1000000)
bonus=bon6+(i-600000)*;
else
bonus=bon10+(i-1000000)*;
printf("奖金是:
%\n",bonus);
return0;
}
4-10-2
#include<>
intmain()
{
inti;
doublebonus,bon1,bon2,bon4,bon6,bon10;
intbranch;
bon1=100000*;
bon2=bon1+100000*;
bon4=bon2+200000*;
bon6=bon4+200000*;
bon10=bon6+400000*;
printf("请输入利润i:
");
scanf("%d",&i);
branch=i/100000;
if(branch>10)branch=10;
switch(branch)
{case0:
bonus=i*;break;
case1:
bonus=bon1+(i-100000)*;break;
case2:
case3:
bonus=bon2+(i-200000)*;break;
case4:
case5:
bonus=bon4+(i-400000)*;break;
case6:
case7:
case8:
case9:
bonus=bon6+(i-600000)*;break;
case10:
bonus=bon10+(i-1000000)*;
}
printf("奖金是%\n",bonus);
return0;
}
4-11
#include<>
intmain()
{intt,a,b,c,d;
printf("请输入四个数:
");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);
if(a>b)
{t=a;a=b;b=t;}
if(a>c)
{t=a;a=c;c=t;}
if(a>d)
{t=a;a=d;d=t;}
if(b>c)
{t=b;b=c;c=t;}
if(b>d)
{t=b;b=d;d=t;}
if(c>d)
{t=c;c=d;d=t;}
printf("排序结果如下:
\n");
printf("%d%d%d%d\n",a,b,c,d);
return0;
}
4-12
#include<>
intmain()
{
inth=10;
floatx1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;
printf("请输入一个点(x,y):
");
scanf("%f,%f",&x,&y);
d1=(x-x4)*(x-x4)+(y-y4)*(y-y4);/*求该点到各中心点距离*/
d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);
d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);
d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);
if(d1>1&&d2>1&&d3>1&&d4>1)h=0;/*判断该点是否在塔外*/
printf("该点高度为%d\n",h);
return0;
}
5-3
#include<>
intmain()
{
intp,r,n,m,temp;
printf("请输入两个正整数n,m:
");
scanf("%d,%d,",&n,&m);
if(n{
temp=n;
n=m;
m=temp;
}
p=n*m;
while(m!
=0)
{
r=n%m;
n=m;
m=r;
}
printf("它们的最大公约数为:
%d\n",n);
printf("它们的最小公约数为:
%d\n",p/n);
return0;
}
5-4
#include<>
intmain()
{
charc;
intletters=0,space=0,digit=0,other=0;
printf("请输入一行字符:
\n");
while((c=getchar())!
='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
elseif(c=='')
space++;
elseif(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("字母数:
%d\n空格数:
%d\n数字数:
%d\n其它字符数:
%d\n",letters,space,digit,other);
return0;
}
5-5
#include<>
intmain()
{
inta,n,i=1,sn=0,tn=0;
printf("a,n=:
");
scanf("%d,%d",&a,&n);
while(i<=n)
{
tn=tn+a;/*赋值后的tn为i个a组成数的值*/
sn=sn+tn;/*赋值后的sn为多项式前i项之和*/
a=a*10;
++i;
}
printf("a+aa+aaa+...=%d\n",sn);
return0;
}
5-6
#include<>
intmain()
{doubles=0,t=1;
intn;
for(n=1;n<=20;n++)
{
t=t*n;
s=s+t;
}
printf("1!
+2!
+...+20!
=%\n",s);
return0;
}
5-7
#include<>
intmain()
{
intn1=100,n2=50,n3=10;
doublek,s1=0,s2=0,s3=0;
for(k=1;k<=n1;k++)/*计算1到100的和*/
{s1=s1+k;}
for(k=1;k<=n2;k++)/*计算1到50各数的平方和*/
{s2=s2+k*k;}
for(k=1;k<=n3;k++)/*计算1到10的各倒数和*/
{s3=s3+1/k;}
printf("sum=%\n",s1+s2+s3);
return0;
}
5-8
#include<>
intmain()
{
inti,j,k,n;
printf("parcissusnumbersare");
for(n=100;n<1000;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);
}
printf("\n");
return0;
}
5-9
#include<>
intmain()
{intm,s,i;
for(m=2;m<1000;m++)
{s=0;
for(i=1;iif((m%i)==0)s=s+i;
if(s==m)
{printf("%d,itsfactorsare",m);
for(i=1;iif(m%i==0)printf("%d",i);
printf("\n");
}
}
return0;
}
5-10
#include<>
intmain()
{
inti,n=20;
doublea=2,b=1,s=0,t;
for(i=1;i<=n;i++)
{
s=s+a/b;
t=a,
a=a+b,
b=t;
}
printf("sum=%\n",s);
return0;
}
5-11
#include<>
intmain()
{
doublesn=100,hn=sn/2;
intn;
for(n=2;n<=10;n++)
{
sn=sn+2*hn;/*第n次落地时共经过的米数*/
hn=hn/2;/*第n次反跳高度*/
}
printf("第10次落地时共经过%f米\n",sn);
printf("第10次反弹%f米\n",hn);
return0;
}
5-12
#include<>
intmain()
{
intday,x1,x2;
day=9;
x2=1;
while(day>0)
{x1=(x2+1)*2;/*第1天的桃子数是第2天桃子数加1后的2倍.*/
x2=x1;
day--;
}
printf("total=%d\n",x1);
return0;
}
5-13
#include<>
#include<>
intmain()
{
floata,x0,x1;
printf("enterapositivenumber:
");
scanf("%f",&a);
x0=a/2;
x1=(x0+a/x0)/2;
do
{x0=x1;
x1=(x0+a/x0)/2;
}while(fabs(x0-x1)>=1e-5);
printf("Thesquarerootof%is%\n",a,x1);
return0;
}
5-14
#include<>
#include<>
intmain()
{doublex1,x0,f,f1;
x1=;
do
{x0=x1;
f=((2*x0-4)*x0+3)*x0-6;
f1=(6*x0-8)*x0+3;
x1=x0-f/f1;
}while(fabs(x1-x0)>=1e-5);
printf("Therootofequationis%\n",x1);
return0;
}
5-15
#include<>
#include<>
intmain()
{floatx0,x1,x2,fx0,fx1,fx2;
do
{printf("enterx1&x2:
");
scanf("%f,%f",&x1,&x2);
fx1=x1*((2*x1-4)*x1+3)-6;
fx2=x2*((2*x2-4)*x2+3)-6;
}while(fx1*fx2>0);
do
{x0=(x1+x2)/2;
fx0=x0*((2*x0-4)*x0+3)-6;
if((fx0*fx1)<0)
{x2=x0;
fx2=fx0;
}
else
{x1=x0;
fx1=fx0;
}
}while(fabs(fx0)>=1e-5);
printf("x=%\n",x0);
return0;
}
5-16
#include<>
intmain()
{inti,j,k;
for(i=0;i<=3;i++)
{for(j=0;j<=2-i;j++)
printf("");
for(k=0;k<=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i<=2;i++)
{for(j=0;j<=i;j++)
printf("");
for(k=0;k<=4-2*i;k++)
printf("*");
printf("\n");
}
return0;
}
5-17
#include<>
intmain()
{
chari,j,k;/*是a的对手;j是b的对手;k是c的对手*/
for(i='x';i<='z';i++)
for(j='x';j<='z';j++)
if(i!
=j)
for(k='x';k<='z';k++)
if(i!
=k&&j!
=k)
if(i!
='x'&&k!
='x'&&k!
='z')
printf("A--%c\nB--%c\nC--%c\n",i,j,k);
return0;
}
6-1
#include<>
#include<>
intmain()
{inti,j,n,a[101];
for(i=1;i<=100;i++)
a[i]=i;
a[1]=0;
for(i=2;ifor(j=i+1;j<=100;j++)
{if(a[i]!
=0&&a[j]!
=0)
if(a[j]%a[i]==0)
a[j]=0;
}
printf("\n");
for(i=2,n=0;i<=100;i++)
{if(a[i]!
=0)
{printf("%5d",a[i]);
n++;
}
if(n==10)
{printf("\n");
n=0;
}
}
printf("\n");
return0;
}
6-2
#include<>
intmain()
{inti,j,min,temp,a[11];
printf("enterdata:
\n");
for(i=1;i<=10;i++)
{printf("a[%d]=",i);
scanf("%d",&a[i]);
}
printf("\n");
printf("Theorginalnumbers:
\n");
for(i=1;i<=10;i++)
printf("%5d",a[i]);
printf("\n");
for(i=1;i<=9;i++)
{min=i;
for(j=i+1;j<=10;j++)
if(a[min]>a[j])min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("\nThesortednumbers:
\n");
for(i=1;i<=10;i++)
printf("%5d",a[i]);
printf("\n");
return0;
}
6-3
#include<>
intmain()
{
inta[3][3],sum=0;
inti,j;
printf("enterdata:
\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%3d",&a[i][j]);
for(i=0;i<3;i++)
sum=sum+a[i][i];
printf("sum=%6d\n",sum);
return0;
}
6-4
#include<>
intmain()
{inta[11]={1,4,6,9,13,16,19,28,40,100};
inttemp1,temp2,number,end,i,j;
printf("arraya:
\n");
for(i=0;i<10;i++)
printf("%5d",a[i]);
printf("\n");
printf("insertdata:
");
scanf("%d",&number);
end=a[9];
if(number>end)
a[10]=number;
else
{for(i=0;i<10;i++)
{if(a[i]>number)
{temp1=a[i];
a[i]=number;
for(j=i+1;j<11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
printf("Nowarraya:
\n");
for(i=0;i<11;i++)
printf("%5d",a[i]);
printf("\n");
return0;
}
6-5
#include<>
#defineN5
intmain()
{inta[N],i,temp;
printf("enterarraya:
\n");
for(i=0;iscanf("%d",&