C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案2_第1頁
C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案2_第2頁
C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案2_第3頁
C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案2_第4頁
C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案2_第5頁
已閱讀5頁,還剩54頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案1-6(2008-10-2117:12:22)

C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案

1.5請(qǐng)參照本章例題,編寫?個(gè)C程序,輸出以下信息:

main()

************

printf("\n");

printf("\n");

printf("VeryGood!\n");

printf("\n");

printf("***********

1.6編寫一個(gè)程序,輸入abe三個(gè)值,輸出其中最大者。

解:main()

{inta,b,c,max;

printf("請(qǐng)輸入三個(gè)數(shù)a,b,c:\rT);

scanf("%d,%d,%d”,&a,&b,&c);

max=a;

if(maxmax=b;

if(maxmax=c;

("最大數(shù)為:

printf%d"zmax);

)

第三章

3.3請(qǐng)將下面各數(shù)用八進(jìn)制數(shù)和十六進(jìn)制數(shù)表示:

(1)10(2)32(3)75(4)-617

(5)-111(6)2483(7)-28654(8)21003

解:十八十六

(10)=(12)=(a)

(32)=(40)=20

(75)=(113)=4b

(-617)=(176627)=fd97

-lll=177621=ff91

2483=4663=963

-28654=110022=9012

21003=51013=520b

3.5字符常量與字符串常量有什么區(qū)別?

解:字符常量是一個(gè)字符,用單引號(hào)括起來。字符串常量是由。個(gè)或若干個(gè)字符

而成,用雙引號(hào)把它們括起來,存儲(chǔ)時(shí)自動(dòng)在字符串最后加一個(gè)結(jié)束符號(hào)、

3.6寫出以下程序的運(yùn)行結(jié)果:

#include

voidmain()

,,,,,,1

charcl='a'/c2=b/c3=c/c4=\101/c5='\116;

printf("a%cb%c\tc%c\tabc\n"/cl/c2/c3);

printf("\t\b%c%c\n",c4/c5);

解:程序的運(yùn)行結(jié)果為:

aabbccabc

AN

3.7將"China"譯成密碼.密碼規(guī)律:用原來的字母后面第4個(gè)字母代替原來的字母,

例如,字母后面第4個(gè)字母是“E",用“E”代音N.因此「China”應(yīng)譯為“Glmre”.

請(qǐng)編一程序,用賦初值的議程使cl,c2,c3,c4,c5分別變成并

輸出.

main()

,,,'

{charcl="C"/c2="h",c3=i",c4=n'/c5='a;

cl+=4;

c2+=4;

c3+=4;

c4+=4;

c5+=4;

printf("密碼是%c%c%c%c%c\n”,cl,c2,c3,c4,c5);

}

3.8例3.6能否改成如下:

include

voidmain()

(

intcl,c2;(原為charcl,c2)

cl=97;

c2=98;

printf("%c%c\n",cl,c2);

printf("%d%d\n",cl/c2);

)

解:可以.因?yàn)樵诳奢敵龅淖址秶鷥?nèi),用整型和字符型作用相同.

3.9求下面算術(shù)表達(dá)式的值.

(l)x+a%3*(int)(x+y)%^=2.5(x=2.5,a=7zy=4.7)

(2)(floatXa+b)/2+(int)x%(int)y=3.5(設(shè)a=2,b=3,x=3.5,y=2.5)

3.10寫出下面程序的運(yùn)行結(jié)果:

include

voidmain()

(

inti,j,m,n;

i=8;

j=10;

m=++i;

n=j++;

pnntf("%dz%d,%d,%d\n"/i/j,m,n);

解:結(jié)果9,11,9,10

第4章

4.4.a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,cWaW.想得

到以下的輸出格式和結(jié)果,請(qǐng)寫出程序要求輸出的結(jié)果如下:

a=3b=4c=5

x=1.200000,y=2.400000,z=-3.600000

x+y=3.60y+z=-1.20z+x=-2.40

u=51274n=128765

cl='a'or97(ASCII)

c2='B'or98(ASCII)

解:

main()

(

inta,b,c;

longintu,n;

floatx,y,z;

charcl,c2;

a=3;b=4;c=5;

x=1.2;y=2.4;z=-3.6;

u=51274;n=128765;

cl='a';c2='b';

printf("\n");

printf("a=%2db=%2dc=%2d\n",a/b,c);

printf("x=%8.6f,y=%8.6f/z=%9.6f\n"/x/y,z);

printf("x+y=%5.2fy=z=%5.2fz+x==%5.2f\n”,x+y,y+z,z+x);

printf("u=%6ldn=%9ld\n”,u,n);

,

printf("cl='%cor%d(ASCII)\n"zcl,c2);

',

printf("c2=%cor%d(ASCII)\n"/c2,c2);

)

4.5請(qǐng)寫出下面程序的輸出結(jié)果.

結(jié)果:

57

57

67.856400,-789.123962

67.856400,-789.123962

67.86,-789.12,67.856400,-789.123962,67.856400,-789.123962

6.785640e+001,-7.89e+002

A,65,101,41

1234567,4553207,d687

65535,17777^-1

COMPUTER,COM

用下面的函數(shù)輸入數(shù)據(jù),使'

4.6scanfa=3,b=7,x=8.5,y=71.82,cl='A'/c2='a/

問在鍵盤上如何輸入?

main()

(

inta,b;

floatx,y;

charclzc2;

scanf("a=%db=%d,&a,&b);

scanf("x=%fy=%e",&x,&y);

scanf("cl=%cc2=%c"/&cl,&c2);

)

解:可按如下方式在鍵盤上輸入:

a=3b=7

x=8.5y=71.82

cl=Ac2=a

說明:在邊疆使用一個(gè)或多個(gè)scnaf函數(shù)時(shí),第一個(gè)輸入行末尾輸入的"回車"被第二

個(gè)scanf函數(shù)吸收,因此在第二'三個(gè)scanf函數(shù)的雙引號(hào)后設(shè)一個(gè)空格以抵消上行

入的"回車".如果沒有這個(gè)空格,按上面輸入數(shù)據(jù)會(huì)出錯(cuò),讀者目前對(duì)此只留有一

初步概念即可,以后再進(jìn)一步深入理解.

4.7用下面的scanf函數(shù)輸入數(shù)據(jù)使a=10,b=20,cl='A,,c2='a,,x=1.5,y=-

3.75,z=57.8,請(qǐng)問

在鍵盤上如何輸入數(shù)據(jù)?

scanf("%5d%5d%c%c%f%f%*f%f",&a,&b,&cl,&c2,&y,&z);

解:

main()

(

inta,b;

floatx,y,z;

charcl,c2;

scanf("%5d%5d%c%c%f%f“,&a,&b,&cl,&c2,&x,&y,&z);

)

運(yùn)行時(shí)輸入:

1020Aal.5-3.75+1.5,67.8

注解:按%5d格式的要求輸入a與b時(shí),要先鍵入三個(gè)空格,而后再打入10與20。%*f

是用來禁止賦值的。在輸入時(shí),對(duì)應(yīng)于%*f的地方,隨意打入了一個(gè)數(shù)1.5,該值不

會(huì)賦給任何變量。

4.8設(shè)圓半徑r=1.5,圓柱高h(yuǎn)=3,求圓周長,圓面積,圓球表面積,圓球體積,圓柱體積,

用scanf輸入數(shù)據(jù),輸出計(jì)算結(jié)果,輸出時(shí)要求有文字說明,取小數(shù)點(diǎn)后兩位數(shù)字.請(qǐng)編

程.

解:main。

floatpi,hjl,s,sq,vq,vz;

pi=3.1415926;

printf("請(qǐng)輸入圓半徑r圓柱高h(yuǎn):\nH);

scanf("%f,%f”,&r,&h);

l=2*pi*r;

s=r*r*pi;

sq=4*pi*r*r;

vq=4.0/3.0*pi*r*r*r;

vz=pi*r*r*h;

print"'圓周長為:=%6.2f\n"J);

(“圓面積為:

printf=%6.2f\n"zs);

("圓球表面積為:n

printf=%6.2f\nzsq);

printf("圓球體積為:=%6.2f\n",vz);

)

4.9輸入一個(gè)華氏溫度,要求輸出攝氏溫度,公式為C=5^(F-32),輸出要有文字說明,

取兩位小數(shù).

解:main()

(

floatc,f;

printf(“請(qǐng)輸入一個(gè)華氏溫度:\n");

scanf("%f",&f);

c=(5.0/5.0)*(f-32);

printf("B氏溫度為:%5,2f\n,c);

)

第五章邏輯運(yùn)算和判斷選取結(jié)構(gòu)

5.4有三個(gè)整數(shù)a,b,c,山鍵盤輸入,輸出其中最大的數(shù).

main()

(

intazb,c;

printf(“請(qǐng)輸入三個(gè)數(shù)嚴(yán));

scanf(”%d,%d,%cT,&a,&b,&c);

if(aif(bprintf("max=%d\n"/c);

else

printf("max=%d\nH,b);

,,

elseif(aprintf("max=%d\n/c);

else

printf(”max-%d\n”⑶;

)

方法2:使用條件表達(dá)式.

main()

{inta,b,c,termp,max;

printf("請(qǐng)輸入A,B,C:");

scanf(”%d,%d,%cT,&a,&b,&c);

printf("A=%d,B=%d/C=%d\n"/a/b,c);

temp=(a>b)?a:b;

max=(temp>c)?temp:c;

printf("A,B,C中最大數(shù)是%dJ,max);

)

5.5main()

{intx,y;

printf("輸入x:");

scanf("%d"z&x);

if(x<l)

{y=x;

,,

printf(X-%d/Y=X=%d\n"zxzy);

)

elseif(x<10)

{y=2*x-l;

printf("X=%dzY=2*X-l=%d\n",x/y);

}

else

{y=3*x-ll;

printf("X=5d,Y=3*x-ll=%d\n",x,y);

)

}

(習(xí)題5-6:)自己寫的已經(jīng)運(yùn)行成功!不同的人有不同的算法,這些答案僅供參考!

voidmain()

(

floatsj;

chara;

scanf(“%f”,&s);

while(s>100||s<0)

(

printf("輸入錯(cuò)誤!error!”);

scanf("%f",&s);

)

i=s/10;

switch((int)i)

(

case10:

case9:a='A';break;

case8:a='B';break;

case7:a='C';break;

case6:a='D';break;

case5:

case4:

case2:

case1:

case0:a='E';

}

printf("%c"za);

)

5.7給一個(gè)不多于5位的正整數(shù),要求:1.求它是幾位數(shù)2.分別打印出每一位數(shù)字3.

按逆序打印出各位數(shù)字.例如原數(shù)為321,應(yīng)輸出123.

main()

(

longintnum;

intindiv,ten,hundred,housand,tenthousand,place;

print",請(qǐng)輸入一個(gè)整數(shù)Q99999):");

scanf(”%ld”,&num);

if(num>9999)

place=5;

elseif(num>999)

place=4;

elseif(num>99)

place=3;

elseif(num>9)

place=2;

elseplace=l;

printf("place=%d\n"zplace);

printf(嗨位數(shù)字為:

ten_thousand=num/10000;

thousand=(num-tenthousand*10000)/1000;

hundred=(num-tenthousand*10000-thousand*1000)/100;

ten=(num-tenthousand*10000-thousand*1000-hundred*100)/10;

indiv=num-tenthousand*10000-thousand*1000-hundred*100-ten*10;

switch(place)

{case5:printf("%d,%d,%d,%d,%d”,tenthousand,thousand,hundred,ten,indiv);

printf("\n反序數(shù)字為巧;

,

printf('%d%d%d%d%d\n",indiv,ten/hundred/thousand,tenthousand);

break;

case4:printf("%d,%d,%d,%d”,thousand,hundred,ten,indiv);

printf("\n反序數(shù)字為

printf(”%d%d%d%d\n",indiv,ten,hundred,thousand);

break;

case3:printf(”%d,%d,%d\n”,hundred,ten,indiv);

printf("\n反序數(shù)字為:");

printf("%d%d%d\n",indivzten/hundred);

case2:printf("%d,%d\n”,ten,indiv);

printf("\n反序數(shù)字為:");

printf("%d%d\n",indiv,ten);

casel:printf("%d\n",indiv);

printf("\n反序數(shù)字為:");

printf("%d\n",indiv);

)

5.8

l.if語句

main()

{longi;

floatbonus,bonl,bon2,bon4zbon6,bonl0;

bonl=100000*0.1;

bon2=bonl+100000*0.075;

bon4=bon2+200000*0.05;

bon6=bon4+200000*0.03;

bonl0=bon6+400000*0.015;

scanf("%ldH,&i);

if(i<=le5)bonus=i*0.1;

elseif(i<=2e5)bonus=bonl+(i-100000)*0.075;

elseif(i<=4e5)bonus=bon2+(i-200000)*0.05;

elseif(i<=6e5)bonus=bon4+(i-400000)*0.03;

elseif(i<=le6)bonus=bon6+(i-600000)*0.015;

elsebonus=bonl0+(i-1000000)*0.01;

,

printf("bonus=%10.2f'/bonus);

)

用switch語句編程序

main()

{longi;

floatbonus,bonl,bon2,bon4,bon6,bonl0;

intbranch;

bonl=100000*0.1;

bon2=bonl+100000*0.075;

bon4=bon2+200000*0.05;

bon6=bon4+200000*0.03;

bonl0=bon6+400000*0.015;

scanf(-%ld”,&i);

branch=i/100000;

if(branch>10)branch=10;

switch(branch)

{case0:bonus=i*0.1;break;

casel:bonus=bonl+(i-100000)*0.075;break;

case2:

case3:bonus=bon2+(i-200000)*0.05;break;

case4:

case5:bonus=bon4+(i-400000)*0.03;break;

case6:

case7

case8:

case9:bonus=bon6+(i-600000)*0.015;break;

case10:bonus=bonl0+(i-1000000)*0.01;

)

printf("bonus=%10.2f,,,bonus);

)

5.9輸入四個(gè)整數(shù),按大小順序輸出.

main()

{intt,a,b,c,d;

printf("請(qǐng)輸入四個(gè)數(shù)

scanf("%d,%d,%d,%d”,&a,&b,&c,&d);

printf("\n\na=%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排序結(jié)果如下:\n");

printf("%d%d%d%d\n",a,b,c,d);

)

5.10塔

main()

(

inth=10;

floatx,y/xO=2,yO=2,dl/d2,d3zd4;

printf(“請(qǐng)輸入一個(gè)點(diǎn)(x,y):");

scanf("%f,%f",&x,&y);

dl=(x-xO)*(x-xO)+(y-yO)(y-yO);

d2=(x-xO)*(x-xO)+(y+yO)(y+yO);

d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);

d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);

if(dl>l&&d2>l&&d3>l&&d4>l)

h=0;

printf("該點(diǎn)高度為%d",h);

}

第六章循環(huán)語句

6.1輸入兩個(gè)正數(shù),求最大公約數(shù)最小公倍數(shù).

main()

{

inta,b,numl,num2,temp;

printf("請(qǐng)輸入兩個(gè)正整數(shù):\n“);

scanf("%d,%cT,&numl,&num2);

if(numl(

temp=numl;

numl=num2;

num2=temp;

}

a=numl,b=num2;

while(b!=O)

(

temp=a%b;

a=b;

b=temp;

)

printf("它們的最大公約數(shù)為:%d\n",a);

printf("它們的最小公倍數(shù)為:%d\n",numl*numM);

}

6.2輸入一行字符,分別統(tǒng)計(jì)出其中英文字母,空格,數(shù)字和其它字符的個(gè)數(shù).

解:

#include<>

main()

(

charc;

intletters=0,space=0/degit=0;other=0;

printf(“請(qǐng)輸入一行字符:\n“);

scanf("%c"z&c);

while((c=getchar())!=,\n,)

(

if(c>=*a,&&c<='z,11c>W&&cv=Z)

letters++;

elseif(c=='*)

space++;

elseif(c>='0'&&c<=9)

digit++;

else

other++;

)

printf("其中:字母數(shù)=%d空格數(shù)=%d數(shù)字?jǐn)?shù)二%d其它字符數(shù)=%

d\nH,letters,space,

digit,other);

)

6.3求s(n)=a+aa+aaa+---+aa---a之值,其中工是一個(gè)數(shù)字.

解:

main()

(

inta,n/count=l,sn=0/tn=0;

printf("請(qǐng)輸入a和n的值:\n”);

scanf(”%d,%d”,&a,&n);

printf("a=%dn=%d\n",a,n);

while(count<=n)

(

tn=tn+a;

sn=sn+tn;

a=a*10;

++count;

)

printf("a+aa+aaa+...=%d\n",sn);

)

6.4求l+2!+3!+4!+??6+20!.

main()

(

floatn,s=0,t=l;

for(n=l;n<=20;n++)

(

t=t*n;

s=s+t;

}

,,

printf("l!+2!+...+20!=%e\n/s);

)

6.5main()

(

intNl=100,N2=50,N3=10;

floatk;

floatsl=0,s2=0,s3=0;

for(k=l;k<=Nl;k++)

(

sl=sl+k;

}

for(k=l;k<=N2;k++)

(

s2=s2+k*k;

)

for(k=l;k<=N3;k++)

(

s3=s3+Vk;

)

printf(",^^U=%8.2f\n",sl+s2+s3);

}

6.6水仙開花

main()

{

inti,j,k,n;

printff''水仙花,數(shù)是:");

for(n=100;n<1000;n++)

(

i=n/100;

j=n/10-i*10;

k=n%10;

if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)

(

printf("%d",n);

}

}

printf("\n");

)

6.7完數(shù)

main()

//includeM1000

main()

(

intk0,kl,k2,k3,k4,k5,k6,k7,k8,k9;

inti,j,n,s;

for(j=2;j<=M;j++)

(

n=0;

s=j;

for(i=l;i{

if((j%i)==O)

(

if((j%i)==O)

(

n++;

s=s-i;

switch(n)

(

case1:

kO=i;

break;

case2:

kl=i;

break;

case3:

k2=i;

break;

case4:

k3=i;

break;

case5:

k4=i;

break;

case6:

k5=i;

break;

case7:

k6=i;

break;

case8:

k7=i;

break;

case9:

k8=i;

break;

case10:

k9=i;

break;

)

)

)

if(s==0)

printf("%d是一個(gè),完數(shù),,它的因子是”,j);

if(n>l)

printf(“%d,%d”,kO,kl);

if(n>2)

printf(",%d",k2);

if(n>3)

printf("z%d"zk3);

if(n>4)

printf(",%d",k4);

if(n>5)

print"',%d",k5);

if(n>6)

printf(,%d”,k6);

if(n>7)

printf(",%d",k7);

if(n>8)

printf(,%d,k8);

if(n>9)

printf(",%d",k9);

printf("\n");

)

)

方法二:此題用數(shù)組方法更為簡單.

main()

(

staticintk[10];

inti,j,n,s;

for(j=2;j<=1000;j++)

(

n=-l;

s=j;

for(i=l;i{

if((j%i)==O)

{

n++;

s=s-i;

k[n]=i;

)

)

if(s==O)

printf("%d是一個(gè)完數(shù),它的因子是:",j);

for(i=0;iprintf("%d,",k[i]);

printf("%d\n",k[n]);

}

6.8有一個(gè)分?jǐn)?shù)序列:如也的解..求出這個(gè)數(shù)列的前20項(xiàng)之和.

解:main()

(

intn,t,number=20;

floata=2,b=lzs=0;

for(n=l;n<=number;n++)

(

s=s+a/b;

t=aza=a+b,b=t;

)

printf("總和=%9.6加”了);

)

6.9球反彈問題

main()

(

floatsn=100.0,hn=sn/2;

intn;

for(n=2;n<=10;n++)

(

sn=sn+2*hn;

hn=hn/2;

)

printf("第10次落地時(shí)共經(jīng)過%£米\n",sn);

printf("第10次反彈%f米.\代反n);

}

6.10猴子吃桃

main()

(

intday,xl,x2;

day=9;

x2=l;

while(day>0)

(

xl=(x2+l)*2;

x2=xl;

day-;

)

printf(“桃子總數(shù)=%d\n”,xl);

6.12

#includeHmath.h"

main()

{floatx,xO,f,fl;

x=1.5;

do

{xO=x;

f=((2*x0-4)*x0+3)*x0-6;

fl=(6*x0-8)*x0+3;

x=xO-f/fl;

)

while(fabs(x-x0)>=le-5);

,,

printf("x=%6.2f\n/x);

6.13

#include"math.h"

main()

{floatx0,xl,x2,fx0,fxl,fx2;

do

n

{scanf("%f,%fz&xl,&x2);

fxl=xl*((2*xl-4)*xl+3)-6;

fx2=x2*((2*x2-4)*x2+3)-6;

while(fxl*fx2>0);

do

{x0=(xl+x2)/2;

fx0=x0*((2*x0-4)*x0+3)-6;

if((fxO*fxl)<O)

{x2=x0;

fx2=fx0;

)

else

{xl=xO;

fxl=fxO;

)

)

while(fabs(fx0)>=le-5);

printf("x0=%6.2f\n",x0);

6.14打印圖案

main()

{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");

}

)

6.15乒乓比賽

main()

(

chari,j,k;

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("順序?yàn)?\na-%c\tb--%c\tc--%c\n”,i,j,k);

)

)

)

)

C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案7-8(2008-10-2117:15:29)

標(biāo)簽:雜談

分類:淘金

C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案

7.1用篩選法求100之內(nèi)的素?cái)?shù).

include

ttdefineN101

main()

{intiJJineza[N];

for(i=2;ifor(i=2;ifor(j=i+l;j{if(a[i]!=0&&a[j]!=0)

if(a[j]%a[i]==0)

aU]=0;

printf("\n");

for(i=2,line=0;i{if(a[i]!=0)

{printf(n%5d",a[i]);

line++;

if(line==10)

{printfC'Xn");

line=0;}

7.2用選擇法對(duì)10個(gè)數(shù)排序.

#defineN10

main()

{intiJmin,temp,a[N];

printf("請(qǐng)輸入十個(gè)數(shù):\n“);

,,,

for(i=0;i{printf('a[%d]=/i);

scanf("%d",&a[i]);

)

printf("\n");

for(i=0;iprintf("%5d"za[i]);

printf("\n");

for(i=0;i{min=i;

for(j=i+l;jif(a[min]>a[j])min=j;

temp=a[i];

a[i]=a[min];

a[min]=temp;

)

printf("\n排序結(jié)果如下:\n");

for(i=0;iprintf("%5d",a[i]);

7.3對(duì)角線和:

main()

(

floata[3][3],sum=0;

intij;

printf(“請(qǐng)輸入矩陣元素:\n“);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

scanf("%f"z&a[i][j]);

for(i=0;i<3;i++)

sum=sum+a[i][i];

printf(“對(duì)角元素之和=6.2f,sum);

)

7.4插入數(shù)據(jù)到數(shù)組

main()

{inta[ll]={l,4,6,9,13,16,19,28,40,100};

inttempLtemp2,numbei;end,i,j;

printf(“初始數(shù)組如下:");

for(i=0;i<10;i++)

printf("%5d"za[i]);

printf("\n");

printf("輸入插入數(shù)據(jù)

scanf("%d",&number);

end=a[9];

if(number>end)

a[10]=number;

else

{for(i=0;i<10;i++)

{if(a[i]>number)

{templ=a[i];

a[i]=number;

for(j=i+l;j<ll;j++)

{temp2=a[j];

a[j]=templ;

templ=temp2;

}

break;

)

)

)

for(i=0;j<ll;i++)

printf("a%6d",a[i]);

7.5將一個(gè)數(shù)組逆序存放。

#defineN5

main()

{inta[N]={8,6z5,4,l},i,temp;

printf("\n初始數(shù)組:\n“);

for(i=0;iprintf(H%4d",a[i]);

for(i=0;i{temp=a[i];

a[i]=a[N-i-l];

a[N-i-l]=temp;

)

printf(H\n交換后的數(shù)組:\n“);

for(i=0;iprintf("%4d"za[i]);

)

7.6楊輝三角

#defineN11

main()

{inti,jza[N][N];

for(i=l;i{a[i][i]=l;

a[i][l]=l;

)

for(i=3;ifor(j=2;j<=i-l;j++)

a[i][j]=a[i01][j-l]+a[i-l][j];

for(i=l;i{for(j=l;j<=i;j++)

pnntf("%6d",a[i][j];

printfCXn'1);

)

printf("\n");

)

7.8鞍點(diǎn)

#defineN10

#defineM10

main()

{inti,j,k,m,n,flagl,flag2,a[N][M],max,maxi,maxj;

printf(H\n輸入行數(shù)n:");

scanf("%d",&n);

printf("\n輸入列數(shù)m:");

scanf(,'%d",&m);

for(i=0;i{printf("第%d行?

,,

for(j=0;jscanf(%d"/&a[i][j];

)

for(i=0;i{for(j=0;jprintf("%5d",a[i][j]);

pritf("\n");

)

flag2=0;

for(i=0;i{max=a[i][O];

for(j=0;jif(a[i][j]>max)

{max=a[i][j];

maxj=j;

)

for(k=O,flagl=l;kif(max>a[k][max])

flagl=0;

if(flagl)

{printf("\n第%:1行,第%d列的%d是鞍點(diǎn)\n”,i,maxj,max);

flag2=l;

if(!flag2)

printf("\n矩陣中無鞍點(diǎn)!\n");

7.9變量說明:top,bott渣找區(qū)間兩端點(diǎn)的下標(biāo);loca:查找成功與否的開關(guān)變量.

#include

#defineN15

main()

{inti^number,top,bott,minJoca,a[N],flag;

charc;

printf(“輸入15個(gè)數(shù)

scanf(”%d”,&a⑼);

i=l;

while(i{scanf(H%d",&a[i]);

if(a[i]>=a[i-l])

i++;

esle

{printf(“請(qǐng)重輸入a[i]");

printf("必須大于

)

)

printf("\nH);

for(i=0;iprint"'%4d

printf("\n");

flag=l;

while(flag)

printf(”請(qǐng)輸入查找數(shù)據(jù)

scanf("%d",&number);

loca=0;

top=0;

bott=N-l;

if((numbera[N-l]))

loca=-l;

while((loca==0)&&(top<=bott))

{min=(bott+top)/2;

if(number==a[min])

{loca=min;

printf("%d位于表中第%d個(gè)數(shù)\n”,number,loca+1);

)

elseif(numberbott=min-l;

else

top=min+l;

)

if(loca==0||loca==-l)

printf(H%d不在表中\(zhòng)n”,number);

printf("是否繼續(xù)查找?Y/N!\n");

c=getchar();

jf(c=='N'||c=='n')

flag=O;

7.10

main()

{inti,j,uppn,lown,dign,span,othn;

chartext[3][80];

uppn=lown=dign=span=othn=0;

for(i=0;i<3;i++)

{printf("\n請(qǐng)輸入第%€)行:\n“,i);

gets(text[i]);

for(j=0;j<80&&text[i][j]!='\0';j++)

{if(text[i][j]>='A'&&text[i]0]<='Z')

uppn+=l;

elseif(text[i][j]>="a'&&text[i][j]<="z')

lown+=l;

elseif(text[i][j]>='l"&&text[i][j]<='9')

dign+=l;

elseif(text[i][j]='')

span+=l;

else

othn+=l;

)

)

for(i=0;i<3;i++)

pnntf(u%s=n",text[i]);

printf("大寫字母數(shù):%d\n,uppn);

printf(“小寫字母數(shù):%d\n“,lown);

printf("數(shù)字個(gè)數(shù):%d\n,dign);

printf("空格個(gè)數(shù):%d\n",sparO;

printf("其它字符:%d\n”,othn);

7.11

main()

{staticchara[5]={,*,;*7*','*'/*'};

inti,j,k;

charspace='

for(i=0;i<=5;i++)

{printf("\n");

for(j=l;j<=3*i;j++)

printf("%lc",space);

for(k=0;k<=5;k++)

printf("%3c",a[k];

7.12

include

main()

{intizn;

charch[80]ztran[80];

printf(“請(qǐng)輸入字符:");

gets(ch);

printf("\n密碼是%c”,ch);

i=0;

while(ch[i]l=>\O')

{if((ch[i]>='A')&&(ch[i]<='Z,))

tran[i]=26+64-ch[i]+l+64;

elseif((ch[i]>=,a')&&(ch[i]<='z'))

tran[i]=26+96-ch[i]+l+96;

else

tran[i]=ch[i];

i++;

)

n=i;

printf("\n原文是:");

for(i=0;iputchar(tran[i]);

)

7.13

main()

(

charsl[80]zs2[40];

inti=O,j=O;

printf(”\i請(qǐng)輸入字符串1:");

scanf("%s"zsl);

printf("\n請(qǐng)輸入字符串2:");

scanf("%s",s2);

while(sl[i]!='\O')

i++;

while(s2[j]!='\0')

sl[i++]=s2[j++];

sl[i]='\O';

printf("\n連接后字符串為:%s”,si);

7.14

#include

main()

{intizresu;

charsl[100],s2[100];

printf(“請(qǐng)輸入字符串l:\n");

gets(sl);

printf("\n請(qǐng)輸入字符串2:\n");

gets(s2);

i=0;

while((sl[i]==s2[i])&&(sl[i]!='\O'))i++;

if(sl[i]=='\O'&&s2[i]=='\0')resu=0;

else

resu=sl[i]-s2[i];

printf(H%s與%,比較結(jié)果是%d”,sl,s2,resu);

7.15

include

main()

charfrom[80],to[80];

inti;

printf(“請(qǐng)輸入字符串,

scanf("%s",from);

for(i=0;i<=strlen(from);i++)

to[i]=from[i];

printf("復(fù)制字符串為:%s\n,to);

)

第八章函數(shù)

8.1(最小公倍數(shù)=u*v/最大公約數(shù).)

hcf(uzv)

intu,v;

(inta,b,t,r;

if(u>v)

{t=u;u=v;v=t;}

a=u;b=v;

while((r=b%a)!=O)

{b=a;a=r;}

return(a);

)

lcd(u,v,h)

intu,v,h;

{intu,vzhj;

scanf(”%d,%d”,&u,&v);

h=hcf(u?v);

printf("H.C.F=%d\n"/h);

l=lcd(uzv,h);

printf("L.C.d=%d\n"J);

)

{return(u*v/h);}

main()

{intu,v,hj;

,

scanf('%d/%d",&u/&v);

h=hcf(uzv);

n

printf("H.C.F=%d\nzh);

l=lcd(u,v,h);

printf("L.C.D=%d\n"J);

8.2求方程根

include

floatxl,x2,disc,p,q;

greater_than_zero(a,b)

floata,b;

(

xl=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

)

equal_to_zero(a,b)

floata,b;

{xl=x2=(-b)/(2*a);}

smaller__than_zero(a,b)

floata,b;

{p=-b/(2*a);

q=sqrt(disc)/(2*a);

)

main()

(

floata,b,c;

printf("\n輸入方程的系數(shù)a,b,c:\n");

方程是:

printf("\n%5,2f*x*x+%5.2f*x+%5.2f=0\n"/a,bzc);

disc=b*b-4*a*c;

printf(“方程的解是:\n");

if(disc>0)

{great_than_zero(a,b);

printf("Xl=%5.2f\tX2=%5.2f\n\n"zxl,x2);

elseif(disc==O)

(

zero(a,b);

printf("Xl=%5.2f\tX2=%5.2f\n\n",xl,x2);

)

else

(

small_than_zero(a,b,c);

printf("Xl=%5.2f+%5.2fi\tX2=%5.2f-%2.2fi\n",p,q,p,q);

)

)

8.3素?cái)?shù)

#include"math.h"

main()

{intnumber;

scanf("%d",&number);

if(prime(number))

printf("yes");

else

printf("no");

)

intprime(number)

intnumber;

{intflag=l,n;

for(n=2;nif(number%n==O)

flag=0;

return(flag);

8.4

#defineN3

intarray[N][N];

convert(array)

intarray⑶[3];

{int

for(i=0;ifor(j=i+l;j{t=array[i][j];

array[i][j]=array[j][i];

array[j][i]=t;

main()

(

intij;

printf("輸入數(shù)組元素:\n“);

for(i=0;ifor(j=0;jscanf("%d",&array[i][j];

printf("\n數(shù)組是:\n“);

for(i=0;i{for(j=0;jprintf("%5d",array[i][j]);

printf("\n");

convert(array);

printf(“轉(zhuǎn)置數(shù)組是:\n”);

for(i=0;i{for(j=0;jpnntf("%5d"zarray[i][j]);

printfCAn");

}

8.5

main()

(

charstr[100];

printf("輸入字符串:\n“);

scanf("%s",str);

inverse(str);

("轉(zhuǎn)換后的字符串是:

printf%s\n"zstr);

}

inverse(str)

charstr[];

(

chart;

intij;

for(i=0,j=strlen(str);i{

t=str[i];

str[i]=str[i-l];

str[i-l]=t;

8.6

charconcatenatefstringl^tringZ,string);

charstringl[],string2[],string[];

intizj;

for(i=0;stringl[i]!='\0,;i++)

string[i]=stringl[i];

for(j=0;string2[j]!=*\0';j++)

string[i+j]=string2[j];

string[i+j]='\O';

)

main()

(

charsl[100],s2[100],s[100];

printf(“\n輸入字符串1:\n");

scanf("%s",sl);

printf("輸入字符串2:\n");

scanf("%s",s2);

concatenate(sl,s2zs);

("連接后的字符串:

printf%s\n"zs);

8.8

main()

(

charstr[80];

prints請(qǐng)輸入含有四個(gè)數(shù)字的字符串:\n“);

n

scanf("%szstr);

insert(str);

insert(str)

charstr[];

inti;

for(i=strlen(str);i>0;i-)

{str[2*i]=str[i];

str[2*i-l]=*

)

printf("\n結(jié)果是:\n%s",str);

)

8.9

#include"math.h"

intalph,digit,space,others;

main()

{chartext[80];

gets(text);

alph=0,digit=0,space=0/others=0;

count(text);

printf("\nalph=%d/digit=%d,space=%d,others=%d\n",alph/digitzspace,others);

)

count(str)

charstr[];

{inti;

for(i=0;str[i]!='\0';i++)

if((str[i]>=,a'&&str[i]<='z,)11(str[i]>='A'&&str[i]<='Z'))

alph++;

elseif(str[i]>='0,&&str[i]<='9')

digit++;

elseif(strcmp(str[i]/*)==0)

space++;

else

others++;

8.10

intalphabetic(c);

charc;

if((c>='a'&&c<='z'||(c>='A'&&c<="Z'))

return(l);

else

return(O);

intlongest(string)

charstring[];

(

intlen=OJzlength=0,flag=lzplacezpoint;

for(i=0;i<=strlen(string);i++)

if(alphabctic(string[i]))

if(flag)

(

point=i;

flag=O;

}

else

len++;

else

{flag=l;

iflen>length)

{length=len;

place=point;

len=0;

)

)

return(place);

)

main()

(

inti;

charline[100];

printf(“輸入一行文本\n“);

gets(line);

printf("\n最長的單詞是

for(i=longest(line);alphabctic(line[i]);i++)

printf("%cHJine[i];

printf("\n");

8.11

#include

#defineN10

charstr[N];

main()

(

inti,flag;

for(flag=l;flag==l;)

(

printf("\n輸入字符串,長度為10:\n");

scanf("%s"z&str);

if(strlen(str)>N)

printf(“超過長度,請(qǐng)重輸!”);

else

flag=0;

)

sort(str);

printf("\n排序結(jié)果巧;

for(i=0;iprintf("%c",str[i]);

}

sort(str)

charstr[N];

(

inti,j;

chart;

for(j=l;jfor(i=0;(iif(str[i]>str[i+l])

{t=str[i];

str[i]=str[i+l];

str[i+l]=t;

8.12

#include

ttinclude

floatsolut(a/b/czd)

floata,b,c,d;

{floatx=lzxO,fzfl;

do

{xO=x;

f=((a*xO+b)*xO+c)*xO+d;

fl=(3*a*xO+2*b)*xO+c;

x=xO-f/fl;

)

while(fabs(x-x0)>=le-5);

return(x);

)

main()

{floata,b,c,d;

scanf(“%f,%f,%£%f“,&a,&b,&c,&d);

printf("x=%10.7f\n"/solut(azb,c,d));

}

8.13

include

main()

{intx,n;

floatp();

scanf(”%d,%d”,&n,&x);

printf("P%d(%d)=%10.2f\n"/n,x/p(n,x));

)

floatp(tn,tx)

inttn,tx;

{if(tn==0)

return(l);

elseif(tn==l)

return(tx);

else

return(((2*tn-l)*tx*p((tn-l),tx)-(tn-l)*p((tn-2),tx))/tn);

}

8.14

#include"stdio.h"

#defineN10

#defineM5

floatscore[N][M];

floata_stu[N],a_cor[M];

main()

{inti,j,r,c;

floath;

floats_diff();

floathighest();

c=l;

input_stu();

avr_stu();

avr_cor();

printf("\nnumberclass12345avr");

for(i=0;i{printf("\nNO%2d"/i+l);

for(j=0;jprintf(”%8.2f”,score[i][j]);

printf("%8.2f"za_stu[i]);

)

printf("\nclassavr");

for(j=0;jprintf("%8.2fH,a_cor[j]);

h=highest(&G&c);

printf("\n\n%8.2f%d%d\n”,h,r,c);

printf("\n%8.2f\n"/s_diff());

)

input_stu()

{inti,j;

floatx;

for(i=0;i{for(j=0;j{scanf(”%f”,&x);

score[i][j]=x;

)

avr_stu()

{inti,j;

floats;

for(i=0;i{for(j=0,s=0;js+=score[i][j];

a_stu[i]=s/5.0;

)

}

avr_cor()

{intij;

floats;

for(j=0;j{for(i=0,s=0;is+=score[i]0];

a_cor[j]=s/(float)N;

floathighest(r;c)

int*r/c;

{floathigh;

intij;

high=score[0][0];

for(i=0;ifor(j=0;jif(score[i][j]>high)

{high=score[i][j];

*r=i+l;

*c=j+l;

)

return(high);

)

floats_diff()

{inti,j;

floatsumx=0.0,sumxn=0.0;

for(i=0;i{sumx+=a_stu[i]*a_stu[i];

sumxn+=a_stu[i];

)

return(sumx/N-(sumxn/N)*(sumxn/N));

)

8.15

#include

#defineN10

voidinput_e(num,name)

intnum[];

charname[N][8];

{inti;

for(i=0;i{scanf("%d",&num[i]);

gets(name[i]);

)

}

voidsort(num,name)

intnum[];

charname[N][8];

{intij,min,tempi;

chartemp2[8];

for(i=0;i{min=i;

for(j=i;jif(num[min]>num[j])min=j;

templ=num[i];

num[i]=num[min];

num[min]=templ;

strcpy(temp2,name[i]);

strcpy(name[i],name[min]);

strcpy(name[min],temp2);

)

for(i=0;iprintf("\n%5d%10s"/num[i]/name[i]);

)

voidsearch(n,num,name)

intn,num[];

charname[N][8];

{inttop,bottzmin,loca;

loca=0;

top=0;

bott=N-l;

if{(nnum[N-l]))

loca=-l;

while((loca==0)&&(top<=bott))

{min=(bott+top)/2;

if(n==num[min])

{loca=min;

printf("number=%d,name=%s\nH,n,name[loca]);

)

elseif(nbott=min-l;

else

top=min+l;

)

if(loca==0||loca==-l)

printf("number=%disnotintable\n”,n);

}

main()

{intnum[N],number,flag,c,n;

charname[N][8];

input_e(num,name);

sort(num,name);

for(flag=l;flag;)

{scanf("%d"z&number);

search(numbeGnum,name);

printf("continue?Y/N!");

c=getchar();

if(c=='N,||c=='n,)

flag=O;

8.16

#include

#defineMAX1000

main()

{intcjjlagjlagl;

chart[MAX];

i=0;

flag=0;

flagl=l;

printf("\n輸入十六進(jìn)制數(shù):");

while((c=getchar())!='\O'&&i{ifc>='0'&&c<='9'||c>='a'&&c<='f,||c>='A'&&c<='F")

{flag=l;

t[i++]=c;

)

elseif(flag)

(

t[i]='\0';

printf("\n十進(jìn)制數(shù)%&己方的(切;

printf("繼續(xù)嗎?”);

c=getchar();

if(c=='N,||c=='n,)

flagl=0;

else

{flag=0;

i=0;

printf("\n輸入十六進(jìn)制數(shù):");

}

htoi(s)

chars[];

{inti,n;

n=0;

for(i=0;s[i]!='\0*;i++)

{if(s[i]>='0'&&s[i]<=,9')

n=n*16+s[i]-'O';

if(s[i]>='a'&&s[i]<='f')

n=n*16+s[i]-'a'+10;

if(s[i]>=,A'&&s[i]<=,F,)

n=n*16+s[i]-'A'+10;

return(n);

8.17

#include

voidcounvert(n)

intn;

{inti;

if((i=n/10)!=0)

convert(i);

putchar(n%10+'0');

)

main()

{intnumber;

printf("\n輸入整數(shù)門;

scanf("%d",&number);

printf("\n輸出是:");

if(number<0)

{putchar('-');

number=-number;

)

convert(number);

8.18

main()

(

intyear,month,day;

intdays;

printf("\n請(qǐng)輸入日期(年,月,日)\n“);

scanf(”%d,%d,%cT,&year,&month,&day);

printf("\n%d年%€)月%(1日”,year,month,day);

days=sum_day(month/day);

if(leap(year)&&month>=3)

days=days+l;

printf("是該年的%d天.\n'',days);

)

staticintday_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}

int(sum_day(month,day)

intmonth,day;

(

inti;

for(i=l;iday+=day_tab[i];

return(day);

)

intleap(year)

intyear;

(

intleap;

leap=year%4==0&&year%100!=01|year%400==0;

return(leap);

C語言設(shè)計(jì)譚浩強(qiáng)第三版的課后習(xí)題答案9-11(2008-10-2117:18:54)

標(biāo)簽:雜談

分類:淘金

第九章編譯預(yù)處理

9.1

#defineSWAP(a,b)t=b;b=a;a=t

main()

(

inta,b,t;

printf("請(qǐng)輸入兩個(gè)整數(shù)a,b:");

scanf("%d,%d”,&a,&b);

SWAP(a,b);

printf(“交換結(jié)果為:a=%d,b=%d\n”,a,b);

9.2

#defineSURPLUS(a,b)((a)%(b))

main()

(

inta,b;

printf(u請(qǐng)輸入兩個(gè)整數(shù)a,b:");

scanf(”%d,%d”,&a,&b);

printf(”a,b相除的余數(shù)為:%d\n,SURPLUS(a,b));

9.3

#include

#definS(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論