C语言课后答案.docx
《C语言课后答案.docx》由会员分享,可在线阅读,更多相关《C语言课后答案.docx(134页珍藏版)》请在冰豆网上搜索。
![C语言课后答案.docx](https://file1.bdocx.com/fileroot1/2023-4/28/66c98da9-8e53-4ae6-8356-74685a74e8b3/66c98da9-8e53-4ae6-8356-74685a74e8b31.gif)
C语言课后答案
习题5-10
staticinta,b;
staticintcurFactor;
/*
函数功能:
指明计算哪两个数的公约数
函数入口参数:
两个整型数
函数返回值:
无
*/
voidCalcCommonFactorOf(intnum1,intnum2)
{
a=num1;
b=num2;
curFactor=a>b?
b:
a;/*curFactor置为两个数中较小的那个*/
}
/*
函数功能:
得到下一个公约数
函数入口参数:
无
函数返回值:
下一个公约数;-1表示再也没有新的公约数
*/
intNextCommonFactor(void)
{
if(a<=0||b<=0)/*保证输入的参数正确*/
return-1;
while(curFactor>0)
{
if(a%curFactor==0&&b%curFactor==0)
{
returncurFactor--;/*如果不减一,则下次还会测试这个数*/
}
curFactor--;
}
return-1;
}
voidCalcCommonFactorOf(intnum1,intnum2);
intNextCommonFactor(void);
#include
#include"cmnfctr.h"
main()
{
intsub;
CalcCommonFactorOf(100,50);
while((sub=NextCommonFactor())>0)
{
staticintcounter=1;
printf("Commonfactor%dis%d\n",counter++,sub);
}
}
习题1-3
#include
main()
{
printf("%d\n",123+456);
}
习题3-5
#include
#include
main()
{
floata,b,c;/*a,b,c为三边变量*/
floats,area;
printf("Inputa,b,c:
");
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%.2f\n",area);
}
习题3-6
#include
#definePI3.14
main()
{
floatr;/*r为半径变量*/
floatcircum,area;
printf("Inputr:
");
scanf("%f",&r);
circum=2*PI*r;
area=PI*r*r,
printf("circum=%.2f,area=%.2f\n",circum,area);
}
习题4-4
(1)
#include
main()
{
intyear,flag;
printf("Enteryear:
");
scanf("%d",&year);
if((year%4==0&&year%100!
=0)||(year%400==0))
{
flag=1;/*如果year是闰年,则标志变量flag置1*/
}
else
{
flag=0;/*否则,标志变量flag置0*/
}
if(flag)
{
printf("%disaleapyear!
\n",year);/*打印“是闰年”*/
}
else
{
printf("%disnotaleapyear!
\n",year);/*打印“不是闰年”*/
}
}
习题4-4
(2)
#include
main()
{
charch;
ch=getchar();
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
printf("ItisanEnglishcharacter!
\n");
}
elseif(ch<='9'&&ch>='0')
{
printf("Itisadigitcharacter!
\n");
}
elseif(ch=='')
{
printf("Itisaspacecharacter!
\n");
}
else
{
printf("Itisothercharacter!
\n");
}
}
习题4-4(3)
#include
main()
{
intupper,step;
floatfahr=0,celsius;
upper=300;
step=20;
while(fahr{
celsius=5.0/9*(fahr-32);
printf("%4.0f\t%6.1f\n",fahr,celsius);
fahr=fahr+step;
}
}
习题4-5
(1)
#include
main()
{
intm;
printf("Inputm:
");
scanf("%d",&m);/*输入一个整数*/
if(m>0)/*是否为正数*/
{
if(m%2==0)/*是正数,且能被2整除,则是正偶数*/
{
printf("%disapositiveeven\n",m);
}
else/*不能被2整除,则是正奇数*/
{
printf("%disapositiveodd\n",m);
}
}
elseif(m<0)/*判断是否为负数*/
{
if(m%2==0)
{
printf("%disanegativeeven\n",m);/*是负偶数*/
}
else
{
printf("%disanegativeodd\n",m);/*是负奇数*/
}
}
else
{
printf("%diszero.Itisaneven\n",m);
}
}
习题4-5
(2)
#include
main()
{
intm;
printf("Inputm:
");
scanf("%d",&m);/*输入一个整数*/
if(m>0)/*判断是否为正数*/
{
printf("%disapositive",m);/*是正数*/
}
elseif(m<0)/*判断是否为负数*/
{
printf("%disanegative",m);/*是负数*/
}
else
{
printf("%diszero.Itisan",m);
}
if(m%2==0)/*判断是否为偶数*/
{
printf("even\n",m);/*是偶数*/
}
else
{
printf("odd\n",m);/*是奇数*/
}
}
习题4-6
#include
#include
main()
{
intx;
doubley;
printf("Inputx:
");
scanf("%d",&x);/*输入一个整数*/
if(x>0)
{
y=exp(-x);/*如果大于0,计算y=exp(-x)的值*/
}
elseif(x==0)
{
y=1;/*x=0,则y=1*/
}
else
{
y=-exp(x);/*x<0,则y=-exp(x)*/
}
printf("y=%f\n",y);
}
习题4-7
#include
#include
#defineLIMIT1e-1
main()
{
floata,b,c;
intflag=1;
printf("Inputthethreeedgelength:
");
scanf("%f,%f,%f",&a,&b,&c);/*输入三角形的三条边*/
/*三角形的基本条件*/
if((a+b)>c&&((b+c)>a)&&((a+c)>b))
{
if(fabs(a-b)<=LIMIT||fabs(b-c)<=LIMIT
||fabs(c-a)<=LIMIT)/*等腰三角形的条件*/
{
printf("等腰");
flag=0;
}
if(fabs(a*a+b*b-c*c)<=LIMIT
||fabs(a*a+c*c-b*b)<=LIMIT
||fabs(c*c+b*b-a*a)<=LIMIT)/*直角三角形的条件*/
{
printf("直角");
flag=0;
}
if(flag)
{
printf("一般");
}
printf("三角形\n");
}
else
{
printf("不是三角形\n");
}
}
习题4-8
#include
main()
{
charc;
printf("*****Time*****\n");
printf("1morning\n");
printf("2afternoon\n");
printf("3night\n");
printf("pleaseenteryourchoice");/*建立相应的菜单*/
c=getchar();/*输入选项*/
switch(c)/*通过switch选择*/
{
case'1':
printf("Goodmorning\n");
break;
case'2':
printf("Goodafternoon\n");
break;
case'3':
printf("Goodnight\n");
break;
default:
printf("Selectionerror!
\n");
}
}
习题4-9
#include
main()
{
intyear,month;
printf("Inputyear,month:
");
scanf("%d,%d",&year,&month);/*输入相应的年和月*/
switch(month)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
printf("31days\n");
break;
case2:
if((year%4==0&&year%100!
=0)||(year%400==0))
{
printf("29days\n");/*闰年的2月有29天*/
}
else
{
printf("28days\n");/*平年的2月有28天*/
}
break;
case4:
case6:
case9:
case11:
printf("30days\n");
break;
default:
printf("Inputerror!
\n");
}
}
习题4-10
(1)
#include
main()
{
inti,sum=0;
for(i=1;i<=101;i=i+2)
{
sum=sum+i;
}
printf("sum=%d\n",sum);
}
习题4-10
(2)
#include
main()
{
inti=1,sum=0;
while(i<=101)
{
sum=sum+i;
i=i+2;
}
printf("sum=%d\n",sum);
}
习题4-11
#include
main()
{
longi;
longterm,sum=0;
for(i=1;i<=99;i=i+2)
{
term=i*(i+1)*(i+2);
sum=sum+term;
}
printf("sum=%ld",sum);
}
习题4-12
(1)
#include
main()
{
longterm=1,sum=0;
inti;
for(i=1;i<=10;i++)
{
term=term*i;
sum=sum+term;
}
printf("1!
+2!
+...+10!
=%ld\n",sum);
}
习题4-12
(2)
include
main()
{
longterm,sum=0;
inti,j;
for(i=1;i<=10;i++)
{
term=1;
for(j=1;j<=i;j++)
{
term=term*j;
}
sum=sum+term;
}
printf("1!
+2!
+…+10!
=%ld\n",sum);
}
习题4-13
#include
main()
{
longterm=0,sum=0;
inta,i,n;
printf("Inputa,n:
");
scanf("%d,%d",&a,&n);/*输入a,n的值*/
for(i=1;i<=n;i++)
{
term=term*10+a;/*求出累加项*/
sum=sum+term;/*进行累加*/
}
printf("sum=%ld\n",sum);
}
习题4-14
(1)
#include
main()
{
doubleterm,result=1;/*累乘项初值应为1*/
intn;
for(n=2;n<=100;n=n+2)
{
term=(double)(n*n)/((n-1)*(n+1));/*计算累乘项*/
result=result*term;
}
printf("result=%f\n",2*result);
}
习题4-12
(2)
#include
main()
{
doubleterm,result=1;
intn;
for(n=1;n<=50;n++)
{
term=(double)(2*n*2*n)/((2*n-1)*(2*n+1));/*计算累乘项*/
result=result*term;
}
printf("result=%f\n",2*result);
}
习题4-15
(1)
#include
#include
main()
{
intn=1,count=1;
doublee=1.0,term=1.0;
longfac=1;
for(n=1;fabs(term)>=1e-5;n++)
{
fac=fac*n;
term=1.0/fac;
e=e+term;
count++;
}
printf("e=%f,count=%d\n",e,count);
}
习题4-16
#include
#include
main()
{
intn=1;
floatterm=1.0,sign=1,sum=0;
while(fabs(term)>=1e-4)/*判断末项大小*/
{
term=sign/n;/*求出累加项*/
sum=sum+term;/*累加*/
sign=-sign;/*改变项的符号*/
n++;/*分母加1*/
}
printf("sum=%f\n",sum);
}
习题4-17
#include
#include
main()
{
intn=1,count=1;
floatx;
doublesum,term;/*因为位数多,所以定义为双精度*/
printf("Inputx:
");
scanf("%f",&x);
sum=x;
term=x;/*赋初值*/
do
{
term=-term*x*x/((n+1)*(n+2));
sum=sum+term;/*累加*/
n=n+2;
count++;
}while(fabs(term)>=1e-5);
printf("sin(x)=%f,count=%d\n",sum,count);
}
习题4-18
(1)
#include
main()
{
inti,j,k,n;
printf("resultis:
");
for(n=100;n<1000;n++)
{
i=n/100;/*分出百位*/
j=(n-i*100)/10;/*分出十位*/
k=n%10;/*分出个位*/
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
{
printf("%d\t",n);/*输出结果*/
}
}
printf("\n");
}
习题4-18
(2)
#include
main()
{
inti,j,k,n;
printf("resultis:
");
for(i=1;i<=9;i++)
for(j=0;j<=9;j++)
for(k=0;k<=9;k++)
{
if(i*i*i+j*j*j+k*k*k==100*i+10*j+k)
{
printf("%d\t",100*i+10*j+k);
}
}
printf("\n");
}
习题4-19
#include
#include
main()
{
inti1,i2,i3,i4,k,n;
printf("Inputdatais:
");
scanf("%d",&n);
k=fabs(n);/*取绝对值*/
i1=k/1000;/*分离出千位*/
i2=(k-i1*1000)/100;/*分离出百位*/
i3=(k-i1*1000-i2*100)/10;/*分离出十位*/
i4=k%10;/*分离出个位*/
printf("Thesumofthetotalbitis%d\n",i1+i2+i3+i4);
}
习题4-20
(1)
#include
main()
{
intx=1;
intfind=0;/*设置找到标志为假*/
while(!
find)
{
if(x%5==1&&x%6==5&&x%7==4&&x%11==10)
{
printf("x=%d\n",x);
find=1;
}
x++;
}
}
习题4-20
(2)
#include
main()
{
intx=1;
while
(1)
{
if(x%5==1&&x%6==5&&x%7==4&&x%11==10)
{
printf("x=%d\n",x);
break;
}
x++;
}
}
习题4-20(3)
#include
main()
{
intx=0,find=0;
do{
x++;
find=x%5==1&&x%6==5&&x%7==4&&x%11==10;
}while