版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第一章
1.5題
#include<iostream>
usingnamespacestd;
intmain()
(
coutvv”This“vv”is”;
cout?,,a',?,,C-H-H-
cout?Hprogram.H;
return0;
1.6題
#include<iostream>
usingnamespacestd;
intmain()
(
inta,b,c;
a=10;
b=23;
c=a+b;
cout?na+b=n;
cout?c;
cout?endl;
return0;
}
1.7七題
#include<iostream>
usingnamespacestd;
intmain()
(
inta,b,c;
intf(intx,inty,intz);
cin?a?b?c;
c=fifa,b,c);
cout?c?endl;
return0;
}
intf(intx,inty,intz)
(
intm;
if(x<y)m=x;
elsem=y;
if(z<m)m=z;
retum(m);
}
1.8題
#include<iostream>
usingnamespacestd;
intmain()
(
inta,b,c;
cin?a?b;
c=a+b;
cout?,,a4-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?na+b=n?c?endl;
return0;
)
intadd(intx,inty)
{intz;
z=x+y;
return(z);
}
2.3題
#include<iostream>
usingnamespacestd;
intmain()
{charcl='a',c2='b',c3='c',c4='\10r,c5='\l16;
cout?c1<<c2?c3?,\n,;
cout?,,\t\bn?c4?,\t'?c5?'\n,;
return0;
}
2.4題
#include<iostream>
usingnamespacestd;
intmain()
{charcl='C,c2='+',c3='+';
cout?nIsay:\HH?cl?c2?c3?,\n,;
cout?n\t\tH?"Hesays:\"C++isveryinteresting!\""?
return0;
}
2.7題
#include<iostream>
usingnamespacestd;
intmain()
{inti,j,m,n;
i=8;
j=10;
m=++i+j++;
n=(++i)+("Hj)+m;
cout?i?,\t,?j?'\t,?m?,\t,?n?endl;
return0;
)
2.8題
#include<iostream>
usingnamespacestd;
intmain()
{charcl-C,c2-h\c3-i\c4='n\c5-a*;
c1+=4;
c2+=4;
c3+=4;
c4+=4;
c5+=4;
cout?npasswordis:n?c1?c2?c3?c4?c5?end1;
return0;
}
3.2題
#include<iostream>
#include<iomanip>
usingnamespacestd;
intmain()
{floath,r,l,s,sq,vq,vz;
constfloatpi=3.1415926;
cout?npleaseenterr,h:n;
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)
?setprecision(2);
cout?nl=H?setw(lO)?l?endl;
cout?Ms=H?setw(10)?s?endl;
cout?nsq="?setw(lO)?sq?endl;
cout?nvq=n?setw(10)?vq?endl;
cout?nvz=n?setw(10)?vz?endl;
return0;
}
3.3題
#include<iostream>
usingnamespacestd;
intmain()
{floatc,f;
coutw”請(qǐng)輸入一個(gè)華氏溫度:“;
cin?f;
c=(5.0/9.0)*(A32);〃注意5和9要用實(shí)型表示,否則5/9值為0
cout?"攝氏溫度為:"wcwendl;
return0;
};
3.4題
#include<iostream>
usingnamespacestd;
intmain()
{charcl,c2;
coutw”請(qǐng)輸入兩個(gè)字符cl,c2:M;
cl=getchar();〃將輸入的第一個(gè)字符賦給cl
c2=getchar();〃將輸入的第二個(gè)字符賦給c2
coutvv”用putchar函數(shù)輸出結(jié)果為:";
putchar(c1);
putchar(c2);
cout?endl;
cout?"用cout語(yǔ)句輸出結(jié)果為:";
cout?c1?c2?endl;
return0;
}
3.4題另一?解
#include<iostream>
usingnamespacestd;
intmain()
{charcl,c2;
cout?”請(qǐng)輸入兩個(gè)字符cl,c2:M;
cl=getchar();〃將輸入的第一個(gè)字符賦給cl
c2=getchar();〃將輸入的第二個(gè)字符賦給c2
coutvv”用putchar函數(shù)輸出結(jié)果為:“;
putchar(cl);
putchar(44);
putchar(c2);
cout?endl;
coutvv”用cout語(yǔ)句輸出結(jié)果為:“;
cout?c1?H,H?c2?endl;
return0;
}
3.5題
#include<iostream>
usingnamespacestd;
intmain()
{charcl,c2;
intil,i2;〃定義為整型
coutvv”請(qǐng)輸入兩個(gè)整數(shù)il,i2:n;
cin?il?i2;
cl=il;
c2=i2;
coutw”按字符輸出結(jié)果為:“vvclw",”v〈c2vvendl;
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||l)?endl;
cout?(!(x=a)&&(y=b)&&0)?endl;
cout?(!(a+b)+c-l&&b+c/2)?endl;
return0;
}
3.9題
include<iostream>
usingnamespacestd;
intmain()
{inta,b,c;
cout?npleaseenterthreeintegernumbers:";
cin?a?b?c;
if(a<b)
ifi[b<c)
cout?Hmax=H?c;
else
cout?nmax=H?b;
elseif(a<c)
cout?Hmax=H?c;
else
cout?nmax=M?a;
cout?endl;
return0;
}
3.9題另一解
#include<iostream>
usingnamespacestd;
intmain()
{inta,b,c,temp,max;
cout?npleaseenterthreeintegernumbers:";
cin?a?b?c;
temp=(a>b)?a:b;/*將a和b中的大者存入temp中*/
max=(temp>c)?temp:c;/*將a和b中的大者與c比較,最大者存入max
*/
cout?nmax=,,?max?endl;
return0;
|
3.10題
#include<iostream>
usingnamespacestd;
intmain()
{intx,y;
cout?nenterx:n;
cin?x;
if(x<l)
{y=x;
cout?Hx="?x?n,y=x=H?y;
}
elseif(x<10)//l<x<10
{y=2*x-l;
cout〈v”x="vvxvv”,y=2*x-1=H?y;
)
else//x>10
{y=3*x-ll;
cout?nx="?x?n,y=3*x-ll-*?y;
cout?endl;
return0;
}
3.11題
#include<iostream>
usingnamespacestd;
intmain()
{floatscore;
chargrade;
cout?npleaseenterscoreofstudent:";
cin?score;
while(score>100||score<0)
{cout?Hdataerror,enterdataagain.";
cin?score;
}
switch(int(score/l0))
{case10:
case9:grade=,A,;break;
case8:grade-B';break;
case7:grade-C*;break;
case6:grade='D';break;
default:grade-E';
}
cout?nscoreisn?score?n,gradeis"?grade?endl;
return0;
}
3.12題
#include<iostream>
usingnamespacestd;
intmain()
{longintnum;
intindiv,ten,hundred,thousand,tenthousand,place;
/*分別代表個(gè)位,十位,百位,千位,萬(wàn)位和位數(shù)
*/
cout?nenteraninteger(0-99999):u;
cin?num;
if(num>9999)
place=5;
elseif(num>999)
place=4;
elseif(num>99)
place=3;
elseif(num>9)
place=2;
elseplace=l;
cout?nplace=,,?place?endl;
〃計(jì)算各位數(shù)字
ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*l0000-thousand*1000)/100;
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
cout?noriginalorder:
switch(place)
{case
5:cout?ten_thousand?H,"?thousand?,,;,?hundred?,,,"?ten?";,?indiv?en
dl;
cout?"reverseorder:n;
cout?indiv?ten?hundred?thousand?ten_thousand?endl;
break;
case4:cout?thousand?,,,''<<hundred?,,,,,?ten?*7,?indiv?endl;
cout?"reverseorder:";
cout?indiv?ten?hundred?thousand?endl;
break;
case3:cout?hundred?,,,',<<ten?",,,<<indiv?endl;
cout?"reverseorder:n;
cout?indiv?ten?hundred?endl;
break;
case2:cout?ten?H,"?indiv?endl;
cout?"reverseorder:";
cout?indiv?ten?endl;
break;
caseI:cout?indiv?endl;
cout?"reverseorder:H;
cout?indiv?endl;
break;
}
return0;
}
313題
#include<iostream>
usingnamespacestd;
intmain()
{longi;//i為利潤(rùn)
floatbonus,bon1,bon2,bon4,bon6,bon10;
bonl=100000*0.1;〃利潤(rùn)為10萬(wàn)元時(shí)的獎(jiǎng)金
bon2=bon1+100000*0.075;//利潤(rùn)為20萬(wàn)元時(shí)的獎(jiǎng)金
bon4=bon2+100000*0.05;〃利潤(rùn)為40萬(wàn)元時(shí)的獎(jiǎng)金
bon6=bon4+100000*0.03;〃利潤(rùn)為60萬(wàn)元時(shí)的獎(jiǎng)金
bonl0=bon6+400000*0.015;〃利潤(rùn)為100萬(wàn)元時(shí)的獎(jiǎng)金
cout?"enteri:H;
cin?i;
if(i<=100000)
bonus=i*0.1;〃利潤(rùn)在10萬(wàn)元以?xún)?nèi)按10%提成獎(jiǎng)金
elseif(i<=200000)
bonus=bon1+(i-l00000)*0.075;〃利潤(rùn)在10萬(wàn)元至20萬(wàn)時(shí)的獎(jiǎng)金
elseif(i<=400000)
bonus=bon2+(i-200000)*0.05;〃利潤(rùn)在20萬(wàn)元至40萬(wàn)時(shí)的獎(jiǎng)金
elseif(i<=600000)
bonus=bon4+(i-400000)*0.03;〃利潤(rùn)在40萬(wàn)元至60萬(wàn)時(shí)的獎(jiǎng)金
elseif(i<=1000000)
bonus=bon6+(i-600000)*0.015;〃利潤(rùn)在60萬(wàn)元至100萬(wàn)時(shí)的獎(jiǎng)金
else
bonus=bon10+(i-1000000)*0.01;〃利潤(rùn)在100萬(wàn)元以上時(shí)的獎(jiǎng)金
cout?nbonus=M?bonus?endl;
return0;
}
3.13題另解
#include<iostream>
usingnamespacestd;
intmain()
{longi;
floatbonus,bon1,bon2,bon4,bon6,bon10;
intc;
bonl=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
cout?nenteri:";
cin?i;
c=i/100000;
if(c>10)c=10;
switch(c)
{case0:bonus=i*0.1;break;
case1: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=bon10+(i-1000000)*0.01;
)
cout?nbonus="?bonus?endl;
return0;
)
3.14題
#include<iostream>
usingnamespacestd;
intmain()
(intt,a,b,c,d;
cout?nenterfournumbers:H;
cin?a?b?c?d;
cout?na=n?a?n,b=H?b?H,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?nthesortedsequence:H?endl;
cout?a?H,H?b?M,H?c?n,n?d?endl;
return0;
)
3.15題
#include<iostream>
usingnamespacestd;
intmain()
{intp,r,n,m,temp;
cout?npleaseentertwopositiveintegernumbersn,m:,r
cin?n?m;
if(n<m)
{temp=n;
n=m;
m=temp;//把大數(shù)放在n中,小數(shù)放在m中
}
p=n*m;//先將n和m的乘積保存在p中,以便求最小公倍數(shù)時(shí)用
while(m!=0)〃求n和m的最大公約數(shù)
{r=n%m;
n=m;
m=r;
cout?uHCF="?n?endl;
cout<<uLCD=n?p/n?endl;//p是原來(lái)兩個(gè)整數(shù)的乘積
return0;
3.16題
#include<iostream>
usingnamespacestd;
intmain()
{charc;
intletters=0,space=0,digit=0,other=0;
cout?nenteroneline::H?endl;
while((c=getchar())!=,\n,)
{if(c>=fa'&&cv=N||c>=A&&c<='Z,)
letters-H-;
elseif(c=**)
space++;
elseif(c>=O&&c<=9)
digit++;
else
other++;
)
cout?,,letter:n?letters<<",space:n?space?n,digit:n?digit?H,
other:"?other?endl;
return0;
}
3.17題
#include<iostream>
usingnamespacestd;
intmain()
{inta,n,i=1,sn=0,tn=0;
cout?na,n=:H;
cin?a?n;
while(i<=n)
{tn=tn+a;〃賦值后的tn為i個(gè)a組成數(shù)的值
sn=sn+tn;〃賦值后的sn為多項(xiàng)式前i項(xiàng)之和
a=a*10;
++i;
cout?na+aa+aaa+...=,,?sn?endl;
return0;
)
3.18題
#include<iostream>
usingnamespacestd;
intmain()
{floats=0,t=l;
intn;
fbr(n=l;n<=20;n++)
(
t=t*n;〃求n!
s=s+t;//將各項(xiàng)累加
)
cout?H1!+2!+...+20!-,?s?endl;
return0;
)
3.19題
#include<iostream>
usingnamespacestd;
intmain()
{intij,k,n;
cout?Hnarcissusnumbersare:n?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?MM;
)
cout?endl;
return0;
}
3.20題
#include<iostream>
usingnamespacestd;
intmain()
(constintm=1000;//定義尋找范圍
intkI,k2,k3,k4,k5,k6,k7,k8,k9,k10;
inti,a,n,s;
fbr(a=2;a<=m;a++)〃a是2-1000之間的整數(shù),檢查它是否為完數(shù)
{n=0;〃n用來(lái)累計(jì)a的因子的個(gè)數(shù)
s=a;〃s用來(lái)存放尚未求出的因子之和,開(kāi)始時(shí)等于a
for(i=l;i<a;i++)//檢查i是否為a的因子
if(a%i=0)//如果i是a的因子
{n++;〃11加1,表示新找到一個(gè)因子
s=s-i;〃s減去已找到的因子,s的新值是尚未求出的因子
之和
switch(n)//將找到的因子賦給kl,..”kl0
{case1:
kl=i;break;//找出的笫1個(gè)因子賦給kl
case2:
k2=i;break;//找出的笫2個(gè)因子賦給k2
case3:
k3=i;break;//找出的笫3個(gè)因子賦給k3
case4:
k4=i;break;//找出的笫4個(gè)因子賦給k4
case5:
k5=i;break;//找出的笫5個(gè)因子賦給k5
case6:
k6=i;break;//找出的笫6個(gè)因子賦給k6
case7:
k7=i;break;//找出的笫7個(gè)因子賦給k7
case8:
k8=i;break;//找出的笫8個(gè)因子賦給k8
case9:
k9=i;break;//找出的笫9個(gè)因子賦給k9
case10:
kl0=i;break;//找出的笫10個(gè)因子賦給klO
}
if(s==O)//s=0表示全部因子都已找到了
{cout?a?"isa完數(shù)”vvendl;
cout?'*itsfactorsare:“;
if(n>l)cout?kl?",n?k2;//n>l表示a至少有2個(gè)因子
if(n>2)cout?H,"?k3;//n>2表示至少有3個(gè)因子,故應(yīng)再輸出一個(gè)因子
if(n>3)cout<v","vvk4;〃n>3表示至少有4個(gè)因子,故應(yīng)再輸出?個(gè)因子
if(n>4)coutvv”,”vvk5;//以下類(lèi)似
if(n>5)cout?\"?k6;
if(n>6)cout?n,"?k7;
if(n>7)cout?H,n?k8;
if(n>8)cout?,,,,,?k9;
if(n>9)cout?M,M?klO;
cout?endl?endl;
return0;
}
3.20題另一解
#include<iostream>
usingnamespacestd;
intmain()
{intm,s,i;
for(m=2;m<1000;m-H4-)
{s=0;
for(i=l;i<m;i++)
if((m%i)==0)s=s+i;
if(s==m)
{cout?m?"isa完數(shù)”vvendl;
cout?nitsfactorsare:";
for(i=l;i<m;i++)
if(m%i==0)cout?i?nM;
cout?endl;
}
)
return0;
}
3.20題另一解
#include<iostream>
usingnamespacestd;
intmain()
{intk[ll];
inti,a,n,s;
for(a=2;a<=1000;a-H-)
{n=0;
s=a;
for(i=l;i<a;i++)
if((a%i)==0)
{n++;
s=s-i;
k[n]=i;//將找到的因子賦給k[l]—k[10]
)
if(s=0)
{cout?a?Misa完數(shù)”vvendl;
cout?"itsfactorsare:";
for(i=l;i<n;i++)
cout?k[i]?nH;
cout?k[n]?endl;
return0;
3.21題
#include<iostream>
usingnamespacestd;
intmain()
{inti,t,n=20;
doublea=2,b=l,s=0;
for(i=];iv=n;i++)
{s=s+a/b;
t=a;
a=a+b;//將前一項(xiàng)分子與分母之和作為下一項(xiàng)的分子
b=t;//將前一項(xiàng)的分子作為下一項(xiàng)的分母
}
cout?nsum=H?s?endl;
return0;
)
3.22題
#include<iostream>
usingnamespacestd;
intmain()
{intday,xl,x2;
day=9;
x2=l;
while(day>0)
{x1=(x2+1)*2;//第1天的桃子數(shù)是第2天桃子數(shù)加1后的2倍
x2=xl;
day—;
}
cout?ntotal=,,?x1?endl;
return0;
)
3.23題
#include<iostream>
#include<cmath>
usingnamespacestd;
intmain。
{floata,x0,xl;
cout?nenterapositivenumber:";
cin?a;//輸入a的值
x0=a/2;
xl=(x0+a/x0)/2;
do
{xO=xl;
xl=(x0+a/x0)/2;
}
while(fabs(xO-xl)>=le-5);
cout?nThesquarerootofn?a?His"?xl?endl;
return0;
)
3.24題
#include<iostream>
usingnamespacestd;
intmain()
{inti,k;
for(i=0;i<=3;i-H-)//輸出上面4行*號(hào)
{for(k=0;k<=2*i;k++)
cout?M*M;//輸出*號(hào)
cout?endl;〃輸出完一行*號(hào)后換行
)
for(i=0;i<=2;i-H-)//輸出下面3行*號(hào)
{for(k=0;k<=4-2*i;k++)
cout?M*H;//輸出*號(hào)
cout?endl;//輸出完一行*號(hào)后換行
}
return0;
}
3.25題
#includev沁stream〉
usingnamespacestd;
intmain()
{chari,j,k;/*i是a的對(duì)手;j是b的對(duì)手;k是c的對(duì)手*/
for(i=,X,;i<=,Z,;i++)
for0=X;jv='Z;j")
if(i!=j)
for(k=,X,;k<='Z';k++)
if(i!=k&&j!=k)
if(i!=X&&k!='X'&&k!='Z')
cout?nA-n?i?HB-n<<j?HC-M?k?endl;
return0;
4.1題
#include<iostream>
usingnamespacestd;
intmain()
{inthcf{int,int);
intlcd(int,int,int);
intu,v,h,l;
cin?u?v;
h=hcf{u,v);
cout?nH.C.F="?h?endl;
l=lcd(u,v,h);
cout?,,L.C.D=n?K<endl;
return0;
)
inthcfifintu,intv)
{intt,r;
if(v>u)
{t=u;u=v;v=t;}
while((r=u%v)!=O)
{u=v;
v=r;}
retum(v);
}
intlcd(intu,intv,inth)
{retum(u*v/h);
4.2題
#include<iostream>
#include<math.h>
usingnamespacestd;
floatxl,x2,disc,p,q;
intmain()
{voidgreater_than_zero(float,float);
voidequal_to_zero(float,float);
voidsmaller_than_zero(float,float);
floata,b,c;
cout?ninputa,b,c:";
cin?a?b?c;
disc=b*b-4*a*c;
cout?nroot:H?endl;
if(disc>0)
greater_than_zero(a,b);
cout?nx1=n?x1?,,,x2=,,?x2?endl;
}
elseif(disc==O)
{equal_to_zero(a,b);
cout?nxl=n?xK<n,x2=n?x2?endl;
}
else
{smaller_than_zero(a,b);
cout?,,xl=,'?p?n+,,?q?"i,,?endl;
cout?,,x2-,?p?,,-,,?q?"i"?endl;
}
return0;
)
voidgreater_than_zero(floata,floatb)/*定義-個(gè)函數(shù),用來(lái)求disc>0時(shí)方
程的根*/
{xl=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
}
voidequal_to_zero(floata,floatb)/*定義,個(gè)函數(shù),用來(lái)求disc=0時(shí)方程
的根*/
{
x1=x2=(-b)/(2*a);
}
voidsmaller_than_zero(floata,floatb)/*定義個(gè)函數(shù),用來(lái)求disc〈0時(shí)方
程的根*/
{
p=-b/(2*a);
q=sqrt(-disc)/(2*a);
}
4.3題
#include<iostream>
usingnamespacestd;
intmain()
(intprime(int);/*函數(shù)原型聲明*/
intn;
cout?ninputaninteger:M;
cin?n;
if(prime(n))
cout?n?Hisaprime."?endl;
else
cout?n?Misnotaprime.u?endl;
return0;
|
intprime(intn)
{intflag=l,i;
for(i=2;i<n/2&&flag==l;i++)
if(n%i=0)
flag=0;
retum(flag);
)
4.4題
#include<iostream>
usingnamespacestd;
intmain()
{intfac(int);
inta,b,c,sum=0;
cout?nentera,b,c:M;
cin?a?b?c;
sum=sum+fac(a)+fac(b)+fac(c);
cout?a?M!+"?b?,,!+"?c?H!=,,?sum?endl;
return0;
}
intfac(intn)
{intf^l;
for(inti=l;i<=n;i++)
f=f*i;
returnf;
)
4.5題
#include<iostream>
#include<cmath>
usingnamespacestd;
intmain()
{doublee(double);
doublex,sinh;
cout?nenterx:H;
cin?x;
sinh=(e(x)+e(-x))/2;
cout?nsinh("?x?M)=H?sinh?endl;
return0;
}
doublee(doublex)
{returnexp(x);}
4.6題〃牛頓迭代法
#include<iostream>
#include<cmath>
usingnamespacestd;
intmain()
{doublesolut(double,double,double,double);
doublea,b,c,d;
cout?ninputa,b,c,d:n;
cin?a?b?c?d;
cout?,,x-,?solut(a,b,c,d)?endl;
return0;
}
doublesolut(doublea,doubleb,doublec,doubled)
{doublex=l,x0,f,fl;
do
{x0=x;
f=((a*xO+b)*xO+c)*xO+d;
fl=(3*a*x0+2*b)*x0+c;
x=x0-f7fl;
}
while(fabs(x-xO)>=le-5);
retum(x);
|
intGcd_2(inta,intb)//歐兒里德算法求a,b的最大公約數(shù){if(a<=0||b<=0)//預(yù)防
錯(cuò)誤return0;inttemp;while(b>0)//b總是表示較小的那個(gè)數(shù),若不是則交
換a,b的值{temp=a%b;〃迭代關(guān)系式a=b;〃a是那個(gè)膽小鬼,始終跟在
b的后面b=temp;//b向前沖鋒占領(lǐng)新的位置}returna;}
4.7題
#include<iostream>
#include<cmath>
usingnamespacestd;
intmain()
{voidgodbaha(int);
intn;
cout?ninputn:";
cin?n;
godbaha(n);
return0;
)
voidgodbaha(intn)
{intprime(int);
inta,b;
fbr(a=3;a<=n/2;a=a+2)
{if(prime(a))
{b=n-a;
if(prime(b))
cout?n?n=M?a?**+n?b?endl;}
intprime(intm)
{inti,k=sqrt(m);
fbr(i=2;i<=k;i4-+)
if(m%i==0)break;
if(i>k)return1;
elsereturn0;
}
4.8題//遞歸法
#include<iostream>
usingnamespacestd;
intmain()
{intx,n;
floatp(int,int);
cout?ninputn&x:n;
cin?n?x;
cout?nn=n?n?H,x=M?x?endl;;
cout?uPH?n?H(x)=H?p(n,x)?endl;
return0;
}
floatp(intn,intx)
{if(n==0)
retum(l);
elseif(n==l)
retum(x);
else
retum(((2*n-1)*x-p((n-1),x)-(n-1)*p((n-2),x))/n);
}
4.9題〃漢諾塔問(wèn)題
#include<iostream>
usingnamespacestd;
intmain()
{voidhanoi(intn,charone,chartwo,charthree);
intm;
cout?ninputthenumberofdiskes:";
cin?m;
cout?nThestepsofmoving"?m?"disks:n?endl;
hanoi(m/A,,,B,;C,);
return0;
}
voidhanoi(intn,charone,chartwo,charthree)
〃將n個(gè)盤(pán)從one座借助two座,移到three座
{voidmove(charx,chary);
if{n==l)move(one,three);
else
{hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
voidmove(charx,chary)
{cout?x?n—>n?y?endl;}
4.10題
#include<iostream>
usingnamespacestd;
intmain()
{voidconvert(intn);
intnumber;
cout?ninputaninteger:";
cin?number;
cout?noutput:H?endl;
if(number<0)
{cout?H-H;
number=-number;
)
convert(number);
cout?endl;
return0;
)
voidconvert(intn)〃感覺(jué)根本想不出的么
{inti;
charc;
if((i=n/10)!=0)
convert(i);
c=n%10+'0';
cout?nH?c;
)
4.11題
#include<iostream>
usingnamespacestd;
intmain()
{intf(int);
intn,s;
cout?ninputthenumbern:";
cin?n;
s=f(n);
cout?nTheresultis"?s?endl;
return0;
}
intfifintn)
(;
if(n=l)
return1;
else
return(n*n+f(n-l));
4.12題
#include<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?ninputa,b,c:";
cin?a?b?c;
if(a+b>c&&a+c>b&&b+c>a)
cout?narea=M?AREA(a,b,c)?endl;
else
cout?nItisnotatriangle!n?endl;
return0;
)
4.14題
#include<iostream>
usingnamespacestd;
//#defineLETTER1
intmain()
{charc;
cin?c;
#ifLETTER
iRc>=n&&c<=,z,)
c=c-32;
#else
iRc>=A&&c<=Z)
c=c+32;
#endif
cout?c?endl;
return0;
)
4.15題
#include<iostream>
usingnamespacestd;
#defineCHANGE1
intmain()
{charch[40];
cout?ninputtext:**?endl;;
gets(ch);
#if(CHANGE)
{for(inti=0;i<40;i++)
{if(ch[i]!士\0’)
if(ch[i]>=,a'&&ch[i]<'z,||ch[i]>'A,&&ch[i]<,Z,)
ch[i]+=l;
elseif(ch[i]=,z*||ch[i]=,Z,)
ch[i]-=25;
}
}
#endif
cout?noutput:n?endl?ch?endl;
return0;
}
4.16題file
#include<iostream>
usingnamespacestd;
inta;
intmain()
{externintpower(int);
intb=3,c,d,m;
cout?nenteranintegeraanditspowerm:u?endl;
cin?a?m;
c=a*b;
cout?a?H*n?b?H="?c?endl;
d=power(m);
cout?a?n**,,?m?*-"?d?endl;
return0;
}
416題file
externinta;
intpower(intn)
{inti,y=l;
fbr(i=l;i<=n;iH-4-)
y*=a;
returny;
5.1題
#include<iostream>
usingnamespacestd;
intmain()
{cout〈v2vv-;
for(inti=3;i<=100;i++)
{boolt=true;
fbr(inta=2;a<i;a++)if(i%a=0){t=false;break;}
if(t)cout?i?f
return0;
}
#include<iostream>
#include<iomanip>
usingnamespacestd;
#include<math.h>
intmain()
{inti,j,n,a[101];
for(i=l;i<=100;i++)
a[i]=i;
a[l]=O;
for(i=2;i<sqrt(100);i-?-+)
for(j=i+l;j<=100;j++)
{if(a[i]!=O&&a[j]!=O)
if(a[j]%a[i]=O)
aU]=O;}
cout?endl;
for(i=1,n=O;i<=1()0;i++)
{if(a[i]!=O)
{cout<<setw(5)?a[i]?M**;
n++;}
if(n=10)
{cout?endl;
n=0;}
}
cout?endl;
return0;
)
5.2題
#include<iostream>
usingnamespacestd;
//#include<math.h>
intmain()
{inti,j,min,temp,a[l1];
cout?nenterdata:,,?endl;
fbr(i=l;i<=10;i++)
{cout?,,a[M?i?,,]=M;
cin?a[i];〃輸入10個(gè)數(shù)
)
cout?endl?nTheoriginalnumbers:"?endl;;
fbr(i=1;i<=10;i-H-)
cout?a[i]?n//輸出這10個(gè)數(shù)
cout?endl;;
fbr(i=l;i<=9;i++)//以下8行是對(duì)?10個(gè)數(shù)排序
{min=i;
for(j=i+1;j<=10;j++)
if(a[min]>a[j])min=j;
temp=a[i];〃以下3行將a[i+l]?a[10]中最小者與a[i]對(duì)換
a[i]=a[min];
a[min]=temp;
}
cout?endl?nThesortednumbers:"?endl;
for(i=l;i<=10;i++)〃輸出已排好序的10個(gè)數(shù)
cout?a[i]?"M;
cout?endl;
return0;
}
#include<iostream>
usingnamespacestd;
intmain()
{inta[10],e,k,t;
for(inti=0;i<10;i++){cin?a[i];}
fbr(inti=0;i<9;i++){k=i;for(t=i+l;t<10;t+4-){if(a[i]<a[t])k=t;}e=a[i];a[i]=a[k];a[k]=e;}
for(inti=0;i<10;i++){cout?a[i]?,
return0;
)
#include<iostream>
#include<iomanip>
usingnamespacestd;
intmain()
{inta[10][10];
fbr(inti=0;i<10;i++){fbr(intt=0;t<10;t++)a[i][t]=0;}
fbr(inti=0;i<10;i++)a[i][i]=l;
for(inti=0;i<10;i++)a[i][0]=l;
fbr(inti=2;i<10;i++){fbr(intt=1;t<=i-1;t++)a[i][t]=a[i-1][t-1]+a[i-1][t];}
fbr(inti=0;i<10;i++){for(intt=0;t<10;t++){if(a[i][t]!=0)cout?a[i][t]?,*;}cout?endl;}
return0;
}
我的第一個(gè)帶指針的C++程序,呵呵??
#include<iostream>
usingnamespacestd;
intmain()
{char*p;inti=0;
charc[100];
gets(c);
P=C;
while(*p!='\0'){i++;p++;}
cout?i;
return0;
}
5.3題
#include<iostream>
usingnamespacestd;
intmain()
{inta[3][3],sum=0;
intij;
cout?nenterdata:"?endl;;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin?a[i][j];
for(i=0;i<3;i++)
sum=sum+a[i][i];
cout?Hsum=M?sum?endl;
return0;
}
5.4題
#include<iostream>
usingnamespacestd;
intmain()
{inta[l1]={1,4,6,9,13,16,19,28,40,100};
intnum,ij;
cout?narraya:n?endl;
for(i=0;i<10;i++)
cout?a[i]?"**;
cout?endl;;
cout?ninsertdata:";
cin?num;
if(num>a[9])
a[10]=num;
else
{for(i=0;i<10;i++)
{if(a[i]>num)
{for(j=9;j>=i;j-)
a[j+l]=a[j];
a[i]=num;
break;
cout?nNow,arraya:n?endl;
for(i=O;i<ll;i-H-)
cout?a[i]?n
cout?endl;
return0;
}
5.5題
#include<iostream>
usingnamespacestd;
intmain()
{constintn=5;
inta[n],i,temp;
cout?nenterarraya:"?endl;
fbr(i=0;i<n;i++)
cin?a[i];
cout?narraya:H?endl;
fbr(i=0;i<n;i4-+)
cout?a[i]?nn;
for(i=0;i<n/2;i++)〃循環(huán)的作用是將對(duì)稱(chēng)的元素的值互換
{temp=a[i];
a[i]=a[n-i-l];
a[n-i-l]=temp;
)
cout?endl?MNow,arraya:n?endl;
fbr(i=0;i<n;i4-+)
cout?a[i]?n
cout?endl;
return0;
)
5.6題
#include<iostream>
#include<iomanip>
usingnamespacestd;
intmain()
{constintn=ll;
inti,j,a[n][n];
fbr(i=l;i<n;i++)
{a[i][i]=l;
a[i][l]=l;
}
fbr(i=3;i<n;i++)
for(j=2;j<=i-l;j++)
a[i]U]=a[i-l]U-l]+a[i-l]U];
for(i=l;i<n;i-H-)
{fbr(j=l;j<=i;j-H-)
cout?setw(5)?a[i][j]?M
cout?endl;
)
cout?endl;
return0;
}
5.7題
#include<iostream>
usingnamespacestd;
intmain()
{constintn=4,m=5;〃假設(shè)數(shù)組為4行5列
inti,j,a[n][m],max,maxj;
boolflag;
fbr(i=0;i<n;i++)〃輸入數(shù)組
for(j=O;j〈m;j++)
cin?a[i]|j];
fbr(i=0;i<n;i++)
{max=a[i][0];maxj=0;
for(j=0;j<m;j4-+)〃找出第i行中的最大數(shù)
if(a[i][j]>max)
{max=a[i][j];〃將本行的最大數(shù)存放在max中
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版裝飾軟裝采購(gòu)合同(含設(shè)計(jì)、安裝)3篇
- 2025年桉樹(shù)種植與木材出口貿(mào)易合作合同2篇
- 2025版食用油購(gòu)銷(xiāo)契約3篇
- 二零二五年專(zhuān)用環(huán)保鋼管銷(xiāo)售協(xié)議范本3篇
- 二零二五年度中石化標(biāo)準(zhǔn)合同文本(海上石油平臺(tái)建設(shè))
- 2025年度力拓公司二零二五版鐵礦石資源儲(chǔ)備與開(kāi)發(fā)合同3篇
- 自動(dòng)攪拌喂料機(jī)課程設(shè)計(jì)
- 老虎照相課程設(shè)計(jì)案例
- 2025版鋼筋市場(chǎng)分析及預(yù)測(cè)合同把握行業(yè)動(dòng)態(tài)3篇
- 空調(diào)通風(fēng)的課程設(shè)計(jì)
- 期末測(cè)試(試題)-三年級(jí)數(shù)學(xué)上冊(cè)人教版
- 藥劑科門(mén)診中成西藥房利用PDCA循環(huán)降低門(mén)診藥房調(diào)劑內(nèi)差發(fā)生率品管圈QCC成果匯報(bào)
- 物料員工作計(jì)劃與總結(jié)
- 浙江省金華市2023年九年級(jí)上學(xué)期期末數(shù)學(xué)試題附答案
- JGT292-2010 潔凈工作臺(tái)標(biāo)準(zhǔn)
- 圣誕節(jié)元旦搞笑小品就劇本:浪子回家
- 2023年海南省天一大聯(lián)考高一化學(xué)第一學(xué)期期末達(dá)標(biāo)測(cè)試試題含解析
- 銀行工會(huì)個(gè)人總結(jié)報(bào)告
- 部編版小學(xué)語(yǔ)文2-6年級(jí)詞語(yǔ)表
- 甘肅省平?jīng)鍪嗅轻紖^(qū)2024屆九年級(jí)英語(yǔ)第一學(xué)期期末統(tǒng)考模擬試題含解析
- 測(cè)繪法規(guī)與工程管理(第2版)完整全套教學(xué)課件
評(píng)論
0/150
提交評(píng)論