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

下載本文檔

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

文檔簡介

1、文檔來源為:從網(wǎng)絡(luò)收集整理,word版本可編輯.歡迎下載支持第一章1.5 題#include<iostream>usingnamespacestd;intmain()cout<<"This"<<"is"cout<<"a"<<"C+"cout<<"program."return0;1.6 題#include<iostream>usingnamespacestd;intmain()inta,b,c;a=10;b=23

2、;c=a+b;cout<<"a+b="cout<<c;cout<<endl;return0;1.7 七題#include<iostream>usingnamespacestd;intmain()inta,b,c;intf(intx,inty,intz);cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return0;intf(intx,inty,intz)intm;if(x<y)m=x;elsem=y;if(z<m)m=z;re

3、turn(m);1.8 題#include<iostream>usingnamespacestd;intmain()(inta,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return0;)1.9 題#include<iostream>usingnamespacestd;intmain()(inta,b,c;intadd(intx,inty);cin>>a>>b;c=add(a,b);cout<<"a+

4、b="<<c<<endl;return0;)intadd(intx,inty)intz;z=x+y;return(z);)2.3 題#include<iostream>usingnamespacestd;intmain()charc1='a',c2='b',c3='c',c4='101',c5='116'cout<<c1<<c2<<c3<<'n'cout<<"tb"<&

5、lt;c4<<'t'<<c5<<'n'return0;)2.4 題#include<iostream>usingnamespacestd;intmain()charc1='C',c2='+',c3='+'cout<<"Isay:""<<c1<<c2<<c3<<'"'cout<<"tt"<<"Hesay

6、s:"C+isveryinteresting!""<<'n'3文檔來源為:從網(wǎng)絡(luò)收集整理,word版本可編輯return0;)2.7 題#include<iostream>usingnamespacestd;intmain()inti,j,m,n;i=8;j=10;m=+i+j+;n=(+i)+(+j)+m;cout<<i<<'t'<<j<<'t'<<m<<'t'<<n<<endl;

7、return0;)2.8 題#include<iostream>usingnamespacestd;intmain()charc1='C',c2='h',c3='i',c4='n',c5='a'c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout<<"passwordis:"<<c1<<c2<<c3<<c4<<c5<<endl;return0;)3.2 題#include<ios

8、tream>#include<iomanip>usingnamespacestd;intmain()floath,r,l,s,sq,vq,vz;constfloatpi=3.1415926;cout<<"pleaseenterr,h:"cin>>r>>h;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout<<setiosflags(ios:fixed)<<setiosflags(ios:right)<<s

9、etprecision(2);cout<<"l="<<setw(10)<<l<<endl;文檔來源為 :從網(wǎng)絡(luò)收集整理.word 版本可編輯.歡迎下載支持cout<<"s="<<setw(10)<<s<<endl;cout<<"sq="<<setw(10)<<sq<<endl;cout<<"vq="<<setw(10)<<vq<&l

10、t;endl;cout<<"vz="<<setw(10)<<vz<<endl;return0;3.3 題#include<iostream>usingnamespacestd;intmain()floatc,f;cout<<"請輸入一個華氏溫度:"cin>>f;c=(5.0/9.0)*(f-32);/注意5和9要用實(shí)型表示,否則5/9值為0cout<<"攝氏溫度為:"<<c<<endl;return0;3.4 題#i

11、nclude<iostream>usingnamespacestd;intmain()charc1,c2;cout<<"請輸入兩個字符c1,c2:"c1=getchar();/將輸入的第一個字符賦給c1c2=getchar();/將輸入的第二個字符賦給c2cout<<"用putchar函數(shù)輸出結(jié)果為:"putchar(c1);putchar(c2);cout<<endl;cout<<"用cout語句輸出結(jié)果為:"cout<<c1<<c2<<

12、endl;return0;3.5 題另一解#include<iostream>usingnamespacestd;intmain()charc1,c2;cout<<"請輸入兩個字符c1,c2:"c1=getchar();/將輸入的第一個字符賦給c1c2=getchar();/將輸入的第二個字符賦給c2cout<<"用putchar函數(shù)輸出結(jié)果為:"putchar(c1);putchar(44);# 文檔來源為:從網(wǎng)絡(luò)收集整理.word 版本可編輯文檔來源為:從網(wǎng)絡(luò)收集整理,word版本可編輯.歡迎下載支持putchar

13、(c2);cout<<endl;cout<<"用cout語句輸出結(jié)果為:";cout<<c1<<","<<c2<<endl;return0;)3.6 題#include<iostream>usingnamespacestd;intmain()charc1,c2;inti1,i2;/定義為整型cout<<"請輸入兩個整數(shù)i1,i2:"cin>>i1>>i2;c1=i1;c2=i2;cout<<"按

14、字符輸出結(jié)果為:"<<c1<<","<<c2<<endl;return0;)3.8 題#include<iostream>usingnamespacestd;intmain()inta=3,b=4,c=5,x,y;cout<<(a+b>c&&b=c)<<endl;cout<<(a|b+c&&b-c)<<endl;cout<<(!(a>b)&&!c|1)<<endl;cout&

15、lt;<(!(x=a)&&(y=b)&&0)<<endl;cout<<(!(a+b)+c-1&&b+c/2)<<endl;return0;)3.9 題include<iostream>usingnamespacestd;intmain()inta,b,c;cout<<"pleaseenterthreeintegernumbers:"cin>>a>>b>>c;if(a<b)if(b<c)cout<<&qu

16、ot;max="<<c;elsecout<<"max="<<b;elseif(a<c)cout<<"max="<<c;elsecout<<"max="<<a;cout<<endl;return0;)3.9題另一解#include<iostream>usingnamespacestd;intmain()inta,b,c,temp,max;cout<<"pleaseenterthreeinteg

17、ernumbers:"cin>>a>>b>>c;maxtemp=(a>b)?a:b;/*將a和b中的大者存入temp中*/max=(temp>c)?temp:c;/*將a和b中的大者與c比較,最大者存入*/cout<<"max="<<max<<endl;return0;)3.10題#include<iostream>usingnamespacestd;intmain()intx,y;cout<<"enterx:"cin>>x;

18、if(x<1)y=x;cout<<"x="<<x<<",y=x="<<y;)elseif(x<10)/1y10y=2*x-1;cout<<"x="<<x<<",y=2*x-1="<<y;)else/x>10y=3*x-11;cout<<"x="<<x<<",y=3*x-11="<<y;)cout<<end

19、l;return0;)3.11題#include<iostream>usingnamespacestd;5文檔來源為:從網(wǎng)絡(luò)收集整理,word版本可編輯文檔來源為 :從網(wǎng)絡(luò)收集整理.word 版本可編輯.歡迎下載支持intmain()floatscore;chargrade;cout<<"pleaseenterscoreofstudent:"cin>>score;while(score>100|score<0)cout<<"dataerror,enterdataagain."cin>>

20、;score;switch(int(score/10)case10:case9:grade='A'break;case8:grade='B'break;case7:grade='C'break;case6:grade='D'break;default:grade='E'cout<<"scoreis"<<score<<",gradeis"<<grade<<endl;return0;3.12題#include<io

21、stream>usingnamespacestd;intmain()longintnum;intindiv,ten,hundred,thousand,ten_thousand,place;/*分別代表個位,十位,百位,千位,萬位和位數(shù)*/cout<<"enteraninteger(099999):"cin>>num;if(num>9999)place=5;elseif(num>999)place=4;elseif(num>99)place=3;elseif(num>9)place=2;elseplace=1;cout&l

22、t;<"place="<<place<<endl;/計(jì)算各位數(shù)字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1000;hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*

23、100-ten*10);cout<<"originalorder:"switch(place)case5:cout<<ten_thousand<<","<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverseorder:"cout<<indiv&

24、lt;<ten<<hundred<<thousand<<ten_thousand<<endl;break;case4:cout<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverseorder:"cout<<indiv<<ten<<hundre

25、d<<thousand<<endl;break;case3:cout<<hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverseorder:"cout<<indiv<<ten<<hundred<<endl;break;case2:cout<<ten<<","<<indiv<&l

26、t;endl;cout<<"reverseorder:"cout<<indiv<<ten<<endl;break;case1:cout<<indiv<<endl;cout<<"reverseorder:"cout<<indiv<<endl;break;)return0;)3.13題#include<iostream>usingnamespacestd;/i為利潤intmain()longi;float bonus,bon1,bon2,b

27、on4,bon6,bon10; bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+100000*0.05;bon6=bon4+100000*0.03;bon10=bon6+400000*0.015;cout<<"enter i:"cin>>i;禾1J潤為10萬元時的獎金 禾I潤為20萬元時的獎金 利潤為40萬元時的獎金 禾I潤為60萬元時的獎金 利潤為100萬元時的獎金if (i<=100000)bonus=i*0.1;利潤在10萬元以內(nèi)按10%提成獎金/利潤在10萬元至20 萬時的獎金/利潤在2

28、0萬元至40 萬時的獎金/利潤在40萬元至60 萬時的獎金/利潤在60萬元至100 萬時的獎金/ 利潤在 100 萬元以上時的獎金elseif(i<=200000)bonus=bon1+(i-100000)*0.075;elseif(i<=400000)bonus=bon2+(i-200000)*0.05;elseif(i<=600000)bonus=bon4+(i-400000)*0.03;elseif(i<=1000000)bonus=bon6+(i-600000)*0.015;elsebonus=bon10+(i-1000000)*0.01;cout<<

29、;"bonus="<<bonus<<endl;return0;3.13 題另一解#include<iostream>usingnamespacestd;intmain()longi;floatbonus,bon1,bon2,bon4,bon6,bon10;intc;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;cout<<"enteri:"c

30、in>>i;c=i/100000;if(c>10)c=10;switch(c)case0:bonus=i*0.1;break;case 1: bonus=bon1+(i-100000)*0.075;break;case 2:case 3: bonus=bon2+(i-200000)*0.05;break;case 4:case 5: bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015;break;case 10: bonus=bon10+(i-100

31、0000)*0.01;cout<<"bonus="<<bonus<<endl;return0;)3.14 題#include<iostream>usingnamespacestd;intmain()intt,a,b,c,d;cout<<"enterfournumbers:"cin>>a>>b>>c>>d;cout<<"a="<<a<<",b="<<b<&l

32、t;",c="<<c<<",d="<<d<<endl;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;cout<<"thesortedsequence:"<<endl;cout<<a<<","<<

33、b<<","<<c<<","<<d<<endl;return0;3.15 題#include<iostream>usingnamespacestd;intmain()intp,r,n,m,temp;cout<<"pleaseentertwopositiveintegernumbersn,m:"cin>>n>>m;if(n<m)temp=n;n=m;m=temp;/把大數(shù)放在n中,小數(shù)放在m中p=n*m;/先將n和m的乘積保

34、存在p中,以便求最小公倍數(shù)時用while(m!=0)求n和m的最大公約數(shù)r=n%m;n=m;m=r;cout<<"HCF="<<n<<endl;9 文檔來源為:從網(wǎng)絡(luò)收集整理.word 版本可編輯文檔來源為 :從網(wǎng)絡(luò)收集整理.word 版本可編輯.歡迎下載支持cout<<"LCD="<<p/n<<endl;/p是原來兩個整數(shù)的乘積return0;)3.16 題#include<iostream>usingnamespacestd;intmain()charc;intlet

35、ters=0,space=0,digit=0,other=0;cout<<"enteroneline二"<<endl;while(c=getchar()!='n')if(c>='a'&&c<='z'|c>='A'&&c<='Z')letters+;elseif(c='')space+;elseif(c>='0'&&c<='9')digit+;

36、elseother+;)cout<<"letter:"<<letters<<",space:"<<space<<",digit:"<<digit<<",other:"<<other<<endl;return0;)3.17 題#include<iostream>usingnamespacestd;intmain()inta,n,i=1,sn=0,tn=0;cout<<"a,n=

37、:"cin>>a>>n;while(i<=n)tn=tn+a;賦值后的tn為i個a組成數(shù)的值sn=sn+tn;賦值后的sn為多項(xiàng)式前i項(xiàng)之和a=a*10;+i;)cout<<"a+aa+aaa+.="<<sn<<endl;return0;)3.18 題#include<iostream>usingnamespacestd;intmain()floats=0,t=1;intn;for(n=1;n<=20;n+)t=t*n;/求n!s=s+t;/將各項(xiàng)累加cout<<&qu

38、ot;1!+2!+.+20!="<<s<<endl;return0;3.19 題#include<iostream>usingnamespacestd;intmain()inti,j,k,n;cout<<"narcissusnumbersare:"<<endl;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)cout<<n<<""cout<<endl;r

39、eturn0;3.20 題#include<iostream>usingnamespacestd;intmain()constintm=1000;/定義尋找范圍intk1,k2,k3,k4,k5,k6,k7,k8,k9,k10;inti,a,n,s;for (a=2;a<=m;a+) n=0;s=a;for (i=1;i<a;i+)if (a%i=0)n+;s=s-i;之和switch(n)case 1:/a是21000之間的整數(shù),檢查它是否為完數(shù)/n用來累計(jì)a的因子的個數(shù)/s用來存放尚未求出的因子之和,開始時等于a/檢查i是否為a的因子/如果i是a的因子/n加1,表示

40、新找到一個因子/s減去已找到的因子,s的新值是尚未求出的因子/將找到的因子賦給k1,.,k10k1=i;break;/找出的笫1個因子賦給k1case2:k2=i;break;/找出的笫2個因子賦給k2case3:k3=i;break;/找出的笫3個因子賦給k3case4:k4=i;break;/找出的笫4個因子賦給k4case5:k5=i;break;/找出的笫5個因子賦給k5case6:k6=i;break;/找出的笫6個因子賦給k6case 7:k7=i;break;/找出的笫7個因子賦給k7case 8:k8=i;break;/找出的笫8個因子賦給k8case 9:k9=i;break

41、;/找出的笫9個因子賦給k9case 10:k10=i;break;/找出的笫10個因子賦給k10if(s=0)/s=0表示全部因子都已找到了cout<<a<<"isa完數(shù)"<<endl;cout<<"itsfactorsare:"if(n>1)cout<<k1<<","<<k2;/n>1表示a至少有2個因子if(n>2)cout<<","<<k3;/n>2表示至少有3個因子,故應(yīng)再輸

42、出一個因子if(n>3)cout<<","<<k4;/n>3表示至少有4個因子,故應(yīng)再輸出一個因子if(n>4)cout<<","<<k5;/以下類似if(n>5)cout<<","<<k6;if(n>6)cout<<","<<k7;if(n>7)cout<<","<<k8;if(n>8)cout<<","

43、;<<k9;if(n>9)cout<<","<<k10;cout<<endl<<endl;return0;3.20題另一解#include<iostream>usingnamespacestd;intmain()intm,s,i;for(m=2;m<1000;m+)s=0;13 文檔來源為:從網(wǎng)絡(luò)收集整理.word 版本可編輯文檔來源為 :從網(wǎng)絡(luò)收集整理.word 版本可編輯.歡迎下載支持for(i=1;i<m;i+)if(m%i)=0)s=s+i;if(s=m)cout<<

44、;m<<"isa完數(shù)"<<endl;cout<<"itsfactorsare:"for(i=1;i<m;i+)if(m%i=0)cout<<i<<""cout<<endl;)return0;)3.20 題另一解#include<iostream>usingnamespacestd;intmain()intk11;inti,a,n,s;for(a=2;a<=1000;a+)n=0;s=a;for(i=1;i<a;i+)if(a%i)=0

45、)n+;s=s-i;k1 -k10kn=i;/將找到的因子賦給)if(s=0)cout<<a<<"isa完數(shù)"<<endl;cout<<"itsfactorsare:"for(i=1;i<n;i+)cout<<ki<<""cout<<kn<<endl;)return0;)3.21 題#include<iostream>usingnamespacestd;intmain()inti,t,n=20;doublea=2,b=1,

46、s=0;for(i=1;i<=n;i+)s=s+a/b;t=a;a=a+b;/將前一項(xiàng)分子與分母之和作為下一項(xiàng)的分子b=t;/將前一項(xiàng)的分子作為下一項(xiàng)的分母cout<<"sum="<<s<<endl;return0;3.22 題#include<iostream>usingnamespacestd;intmain()intday,x1,x2;day=9;x2=1;while(day>0)x1=(x2+1)*2;/第1天的桃子數(shù)是第2天桃子數(shù)加1后的2倍x2=x1;day-;cout<<"tot

47、al="<<x1<<endl;return0;3.23 題#include<iostream>#include<cmath>usingnamespacestd;intmain()floata,x0,x1;cout<<"enterapositivenumber:"cin>>a;/輸入a的值x0=a/2;x1=(x0+a/x0)/2;dox0=x1;x1=(x0+a/x0)/2;while(fabs(x0-x1)>=1e-5);cout<<"Thesquarerooto

48、f"<<a<<"is"<<x1<<endl;return0;3.24 題#include<iostream>usingnamespacestd;19 文檔來源為:從網(wǎng)絡(luò)收集整理.word 版本可編輯intmain()inti,k;for(i=0;i<=3;i+)for(k=0;k<=2*i;k+)cout<<"*"cout<<endl;for(i=0;i<=2;i+)/輸出上面4行*號/輸出*號/輸出完一行*號后換行/輸出下面3行*號for(k

49、=0;k<=4-2*i;k+)cout<<"*"cout<<endl;return0;3.25題#include<iostream>usingnamespacestd;intmain()chari,j,k;/輸出*號/輸出完一行*號后換行/*i是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

50、&&j!=k)if(i!='X'&&k!='X'&&k!='Z')cout<<"A-"<<i<<"B-"<<j<<"C-"<<k<<endl;return0;4.1題#include<iostream>usingnamespacestd;intmain()inthcf(int,int);intlcd(int,int,int);intu,v,h,l

51、;cin>>u>>v;h=hcf(u,v);cout<<""<<h<<endl;l=lcd(u,v,h);cout<<""<<l<<endl;return0;inthcf(intu,intv)intt,r;if(v>u)t=u;u=v;v=t;while(r=u%v)!=0)u=v;v=r;return(v);intlcd(intu,intv,inth)return(u*v/h);4.2 題#include<iostream>#include

52、<math.h>usingnamespacestd;floatx1,x2,disc,p,q;intmain()voidgreater_than_zero(float,float);voidequal_to_zero(float,float);voidsmaller_than_zero(float,float);floata,b,c;cout<<"inputa,b,c:"cin>>a>>b>>c;disc=b*b-4*a*c;cout<<"root:"<<endl;if(d

53、isc>0)greater_than_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;elseif(disc=0)equal_to_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;elsesmaller_than_zero(a,b);cout<<"x1="<<p<

54、<"+"<<q<<"i"<<endl;cout<<"x2="<<p<<"-"<<q<<"i"<<endl;return0;disc>0 時方voidgreater_than_zero(floata,floatb)/*定義一個函數(shù),用來求程的根*/x1=(-b+sqrt(disc)/(2*a);x2=(-b-sqrt(disc)/(2*a);voidequal_to_zero(f

55、loata,floatb)/*定義一個函數(shù),用來求disc=0時方程的根*/x1=x2=(-b)/(2*a);voidsmaller_than_zero(floata,floatb)/*定義一個函數(shù),用來求disc<0時方程的根*/p=-b/(2*a);q=sqrt(-disc)/(2*a);4.3 題#include<iostream>usingnamespacestd;intmain()intprime(int);/*函數(shù)原型聲明*/intn;cout<<"inputaninteger:"cin>>n;if(prime(n)co

56、ut<<n<<"isaprime."<<endl;elsecout<<n<<"isnotaprime."<<endl;return0;intprime(intn)intflag=1,i;for(i=2;i<n/2&&flag=1;i+)if(n%i=0)flag=0;return(flag);4.4 題#include<iostream>usingnamespacestd;intmain()intfac(int);inta,b,c,sum=0;cou

57、t<<"entera,b,c:"cin>>a>>b>>c;sum=sum+fac(a)+fac(b)+fac(c);cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<endl;return0;intfac(intn)intf=1;for(inti=1;i<=n;i+)f=f*i;returnf;4.5 題#include<iostrea

58、m>#include<cmath>usingnamespacestd;intmain()doublee(double);doublex,sinh;cout<<"enterx:"cin>>x;sinh=(e(x)+e(-x)/2;cout<<"sinh("<<x<<")="<<sinh<<endl;return0;doublee(doublex)returnexp(x);4.6 題/牛頓迭代法#include<iostream&g

59、t;#include<cmath>usingnamespacestd;intmain()doublesolut(double,double,double,double);doublea,b,c,d;cout<<"inputa,b,c,d:"cin>>a>>b>>c>>d;cout<<"x="<<solut(a,b,c,d)<<endl;return0;doublesolut(doublea,doubleb,doublec,doubled)doubl

60、ex=1,x0,f,f1;dox0=x;f=(a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;while(fabs(x-x0)>=1e-5);return(x);文檔來源為 :從網(wǎng)絡(luò)收集整理.word 版本可編輯.歡迎下載支持intGcd_2(inta,intb)/歐幾里德算法求a,b的最大公約數(shù)if(a<=0|b<=0)/預(yù)防錯誤return0;inttemp;while(b>0)/b總是表示較小的那個數(shù),若不是則交換a,b的值temp=a%b;/迭代關(guān)系式a=b;/a是那個膽小鬼,始終跟在b的后面b=temp;/b

61、向前沖鋒占領(lǐng)新的位置returna;4.7 題#include<iostream>#include<cmath>usingnamespacestd;intmain()voidgodbaha(int);intn;cout<<"inputn:"cin>>n;godbaha(n);return0;voidgodbaha(intn)intprime(int);inta,b;for(a=3;a<=n/2;a=a+2)if(prime(a)b=n-a;if(prime(b)cout<<n<<"=&q

62、uot;<<a<<"+"<<b<<endl;intprime(intm)inti,k=sqrt(m);for(i=2;i<=k;i+)if(m%i=0)break;if(i>k)return1;elsereturn0;4.8 題/遞歸法#include<iostream>usingnamespacestd;intmain()intx,n;floatp(int,int);cout<<"inputn&x:"cin>>n>>x;cout<&

63、lt;"n="<<n<<",x="<<x<<endl;cout<<"P"<<n<<"(x)="<<p(n,x)<<endl;return0;floatp(intn,intx)if(n=0)return(1);elseif(n=1)return(x);elsereturn(2*n-1)*x-p(n-1),x)-(n-1)*p(n-2),x)/n);4.9 題/漢諾塔問題#include<iostream&

64、gt;usingnamespacestd;intmain()voidhanoi(intn,charone,chartwo,charthree);intm;cout<<"inputthenumberofdiskes:"cin>>m;cout<<"Thestepsofmoving"<<m<<"disks:"<<endl;hanoi(m,'A','B','C');return0;voidhanoi(intn,charone

65、,chartwo,charthree)將n個盤從one座借助two座,移到three座voidmove(charx,chary);if(n=1)move(one,three);elsehanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);voidmove(charx,chary)cout<<x<<"->"<<y<<endl;4.10 題#include<iostream>usingnamespacestd;intmain()void

66、convert(intn);intnumber;cout<<"inputaninteger:"cin>>number;cout<<"output:"<<endl;if(number<0)cout<<"-"number=-number;convert(number);cout<<endl;return0;voidconvert(intn)/感覺根本想不出的么inti;charc;if(i=n/10)!=0)convert(i);c=n%10+'0

67、9;cout<<""<<c;4.11 題#include<iostream>usingnamespacestd;intmain()intf(int);intn,s;cout<<"inputthenumbern:"cin>>n;s=f(n);cout<<"Theresultis"<<s<<endl;return0;intf(intn);if(n=1)return1;elsereturn(n*n+f(n-1);4.12 題#include<

68、;iostream>#include<cmath>usingnamespacestd;#defineS(a,b,c)(a+b+c)/2#defineAREA(a,b,c)sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)intmain()floata,b,c;cout<<"inputa,b,c:"cin>>a>>b>>c;if(a+b>c&&a+c>b&&b+c>a)cout<<"ar

69、ea="<<AREA(a,b,c)<<endl;elsecout<<"Itisnotatriangle!"<<endl;return0;4.14 題#include<iostream>usingnamespacestd;/#defineLETTER1intmain()charc;cin>>c;#ifLETTERif(c>='a'&&c<='z')c=c-32;#elseif(c>='A'&&c&l

70、t;='Z')c=c+32;#endifcout<<c<<endl;return0;4.15 題#include<iostream>usingnamespacestd;#defineCHANGE1intmain()charch40;cout<<"inputtext:"<<endl;gets(ch);#if(CHANGE)for(inti=0;i<40;i+)if(chi!='0')if(chi>='a'&&chi<'z'|chi>'A'&&chi<'Z

溫馨提示

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

評論

0/150

提交評論