习题3答案
1.答案:
#include
main()
{
printf("Thesizeoffundamentaltypesiscomputed.\n\n");
printf("char:
%3ubyte\n",sizeof(char));
printf("short:
%3ubytes\n",sizeof(short));
printf("int:
%3ubytes\n",sizeof(int));
printf("long:
%3ubytes\n",sizeof(long));
printf("unsigned:
%3ubytes\n",sizeof(unsigned));
printf("float:
%3ubytes\n",sizeof(float));
printf("double:
%3ubytes\n",sizeof(double));
}
运行状况:
Thesizeoffundamentaltypesiscomputed.
char:
1byte
short:
2bytes
int:
2bytes
long:
4bytes
unsigned:
2bytes
float:
4bytes
double:
8bytes
2.答案:
在TurboC2.0环境中,用printf函数输出时,做四舍五入处理;赋给整型变量时,做截断处理。
#include
main()
{
doubled=23.176;
inti=2.999;
printf("%.2f,%d\n",d,i);
}
运行结果:
23.18,2
3.答案:
#include
main()
{
charc1,c2;
c1=getchar();c2=getchar();
putchar(c1);putchar(c2);putchar('\n');
printf("%c%c\n",c1,c2);
}
问题回答:
(1)两种都可以;定义成char型较好,因为用char型量意义明确,且占用字节数少。
(2)只能使用printf函数,具体语句是printf("%d%d\n",c1,c2);
(3)不是在任何情况下都可以互相代替,两种变量的值域不一样。
char型量的值域为-128~+127,int型量的值域为-32768~+32767。
一般地,可用int型量代替char型量,反之不然。
本质上,int型量是两个字节长度的整数,char型量是一个字节长度的整数。
8.答案:
c,b,a
9.答案:
#include
main()
{
longm,n;
intb3;
doubleq;
printf("Pleaseinputm&n:
");
scanf("%ld%ld",&m,&n);
q=(double)m/n;
b3=(int)(q*1000)%10;
printf("quotient=%.4f,b3=%d\n",q,b3);
}
运行状况:
Pleaseinputm&n:
188639100000
quotient=1.8864,b3=6
10.答案:
#include
#defineM_TO_K1.6093
#defineM_TO_Y1760
main()
{
doublekilo;
intmile,yard;
printf("Pleaseinputkilometers:
");
scanf("%lf",&kilo);
mile=kilo/M_TO_K;
yard=(kilo/M_TO_K-mile)*M_TO_Y+0.5;
printf("miles=%d,yards=%d\n",mile,yard);
}
运行状况:
Pleaseinputkilometers:
10
miles=6,yards=376
11.答案:
#include
main()
{
intreal1,image1,real2,image2;
printf("Pleaseinputcomplex1:
");
scanf("%d+%di",&real1,&image1);
printf("Pleaseinputcomplex2:
");
scanf("%d+%di",&real2,&image2);
printf("TheSumis=%d+%di\n",real1+real2,image1+image2);
}
运行状况:
Pleaseinputcomplex1:
3+5i
Pleaseinputcomplex2:
8+9i
TheSumis=11+14i
12.答案:
#include
main()
{
inthour,minute,second,year,month,day;
printf("Pleaseinputtime(hh:
mm:
ss):
");
scanf("%d:
%d:
%d",&hour,&minute,&second);
printf("Timeis=%02d:
%02d:
%02d\n",hour,minute,second);
printf("Pleaseinputdate(yyyy-mm-dd):
");
scanf("%d-%d-%d",&year,&month,&day);
printf("Dateis=%04d-%02d-%02d\n",year,month,day);
}
运行状况:
Pleaseinputtime(hh:
mm:
ss):
5:
3:
8
Timeis=05:
03:
08
Pleaseinputdate(yyyy-mm-dd):
2006-12-8
Dateis=2006-12-08
13.答案:
#include
main()
{
inthours,minutes,seconds;
longtotseconds;
printf("Pleaseinputtotalseconds:
");
scanf("%ld",&totseconds);
hours=totseconds/3600;
minutes=(totseconds-hours*3600)/60;
seconds=totseconds-hours*3600-minutes*60;
printf("equalto%dhours,%dminutes,%dseconds\n",hours,minutes,seconds);
}
运行状况:
Pleaseinputtotalseconds:
3753
equalto1hours,2minutes,33seconds
14.答案:
#include
main()
{
unsignedintnum,low,high;
printf("Pleaseinputahexnumber:
");
scanf("%X",&num);
low=num&0x00ff;
high=num>>8;
printf("%2X,%2X\n",low,high);
}
运行状况:
Pleaseinputahexnumber:
A8BD
BD,A8
习题4答案
1.#include
main()
{
doublea,b,c,max;
printf("Pleaseinputthreenumbers:
\n");
scanf("%lf%lf%lf",&a,&b,&c);
if(a>b)max=a;
elsemax=b;
if(maxprintf("Themaxnumberis:
%f\n",max);
}
2.#include
main()
{
intyear;
printf("Pleaseinputayear:
");
scanf("%d",&year);
if((year%4==0&&year%100!
=0)||year%400==0)
printf("%disaleapyear.\n",year);
else
printf("%disnotaleapyear.\n",year);
}
4.#include
main()
{
inti,count=0;
for(i=1;i<=1000;i++)
if(i%9==0&&i%11==0)
{
printf("%d\t",i);
count++;
if(count%5==0)printf("\n");
}
}
5.If-else语句答案:
#include
main()
{
intscore;
printf("Pleaseinputscore(0~100):
");
scanf("%d",&score);
if(score>100||score<0)
printf("Thescoreerror!
\n");
else
{
printf("\nThegradeis(A~E):
");
if(score>=90)printf("A\n");
elseif(score>=80)printf("B\n");
elseif(score>=70)printf("C\n");
elseif(score>=60)printf("D\n");
elseprintf("E\n");
}
}
Switch语句答案:
#include
main()
{
intscore;
printf("Pleaseinputscore(0~100):
");
scanf("%d",&score);
if(score>100||score<0)
printf("Thescoreerror!
\n");
else
{
printf("\nThegradeis(A~E):
");
switch(score/10){
case10:
case9:
printf("A\n");break;
case8:
printf("B\n");break;
case7:
printf("C\n");break;
case6:
printf("D\n");break;
default:
printf("E\n");
}
}
}
6.#include
main()
{
intbase1,base2,base3,base4,base5,base6,base7,base8;
floatmoney,tax;
base1=0.05*500;
base2=base1+0.1*1500;
base3=base2+0.15*3000;
base4=base3+0.2*15000;
base5=base4+0.25*20000;
base6=base5+0.3*20000;
base7=base6+0.35*20000;
base8=base7+0.4*20000;
printf("Pleaseinputtheincome:
");
scanf("%f",&money);
if(money<0)
printf("Inputisnegative,error!
\n");
else
{
money=money-800;
if(money<=0)tax=0;
elseif(money<=500)tax=money*0.05;
elseif(money<=2000)tax=base1+(money-500)*0.1;
elseif(money<=5000)tax=base2+(money-2000)*0.15;
elseif(money<=20000)tax=base3+(money-5000)*0.2;
elseif(money<=40000)tax=base4+(money-20000)*0.25;
elseif(money<=60000)tax=base5+(money-40000)*0.3;
elseif(money<=80000)tax=base6+(money-60000)*0.35;
elseif(money<=100000)tax=base7+(money-80000)*0.4;
elsetax=base8+(money-100000)*0.45;
printf("\nThetaxis:
%f\n",tax);
}
}
7.#include
main()
{
inti,a,b,c,d;
for(i=1000;i<10000;i++)
{
a=i/1000;/*千位*/
b=i%1000/100;/*百位*/
c=i%100/10;/*十位*/
d=i%10;/*个位*/
if(a*a*a*a+b*b*b*b+c*c*c*c+d*d*d*d==i)
printf("%d\t",i);
}
printf("\n");
}
8.#include
#include
main()
{
inti,j,m;
for(i=2;i<1000;i++)
{
m=sqrt(i);
for(j=2;j<=m;j++)
if(i%j==0)break;
if(j>=m+1)
printf("%d\t",i);
}
printf("\n");
}
9.#include
main()
{
intbeginCol=5,i,j,m;
for(i=7,j=4;j>0;j--,i-=2)
{
for(m=0;mprintf("");
for(m=0;m
printf("*");
printf("\n");
}
for(i=3,j=2;j<=4;j++,i+=2)
{
for(m=0;mprintf("");
for(m=0;m
printf("*");
printf("\n");
}
printf("\n");
}
10.#include
main()
{
inti,x,y,z;
for(x=1;x<=9;x++)
for(y=0;y<=9;y++)
for(z=0;z<=9;z++)
if(x!
=y&&y!
=z&&z!
=x&&(x*100+y*10+z)%11==0)
printf("%d\t",x*100+y*10+z);
printf("\n");
}
11.#include
main()
{
inti;
floatm,n,sum=0;
for(i=0,m=1,n=2;i<30;i++)
{
sum+=n/m;
n=n+m;
m=n-m;
}
printf("%f\n",sum);
}
12.#include
main()
{
intm,i,j,k,total;
printf("PleaseinputN(1<=N<=1000):
");
scanf("%d",&m);
total=0;/*初始化*/
for(i=1;i<=100;i++)/*5块钱组合*/
for(j=1;j<=500;j++)/*2块钱组合*/
for(k=1;k<=1000;k++)/*1块钱组合*/
if(i*5+j*2+k==m)
{
printf("%d=%d*5+%d*2+%d*1\n",m,i,j,k);
total++;
}
printf("total=%d\n",total);/*组合次数*/
}
13.#include
main()
{
longtotal;
inti;
total=1;/*初始化*/
for(i=1;i<=50;i++)
if(i%3==0)
total*=i;
printf("total=%ld\n",total);
}
14.#include
main()
{
charc;
inti;
c=0;i=0;
while(c!
=10&&c!
=13)
{
scanf("%c",&c);
if(c=='c')
i++;
}
printf("result=%d",i);
}
15.#include
main()
{
longnum=0,sum=0;
inta,n,i;
printf("Pleaseinputa:
");
scanf("%d",&a);
printf("Pleaseinputn:
");
scanf("%d",&n);
for(i=0;i{
num=num*10+a;
sum=sum+num;
}
printf("sum=%ld\n",sum);
}
16.#include
main()
{
charc;
intCnum,cnum,digit,space,other;
Cnum=cnum=digit=space=other=0;
printf("Pleaseinputastring:
");
while((c=getchar())!
='\n')
{
if(c>='A'&&c<='