50道C編程練習題及解答_第1頁
50道C編程練習題及解答_第2頁
50道C編程練習題及解答_第3頁
50道C編程練習題及解答_第4頁
50道C編程練習題及解答_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

50道C/C++編程練習題

1.輸入3個數(shù),求最大值elseif(a>=60)cout?/,D*;

intmainOelsecout?"E";

{inta,b,c,m;(

c:n?a>>b?c;4.輸入3個double類型的值,判斷這3個值是否

m=a;可以表示一個三角形的三條邊。

if(b>m)m=b;intmainO

if(c>m)m=c;(

cout<<m;doublea,b,c;

}cin?a?b>>c;

if(a+b>c&&b+c>a&&c+a>b)

2.編程序,求方程ax2+bx+c=0的根cout〈,可以構(gòu)成三角形”;

#include<iostream>else

#include<cmath>cout<<"不可以構(gòu)成三角形”;

usingnamespacestd;)

intmainO

(5.輸入20個數(shù),求其最大、最小和平均值

doublea,b,c,d,xl,x2;intmainO

cm?a>>b?c;(

if(a==O)inti;

if(b==0)cout?*error\n*;inta,max,min,s;

elsecout?z,x=z*?-c/b?endl;cin?a;

elsemax=min=a;s=a;

{d=b*b-4*a*c;for(i=l;i<20;i++)

if(fabs(d)<=le-6){cin?a;

coul?/,xl-x2-/,?-b/(2*a)?sndl;if(a>max)max-a;

elseif(d>le-6)if(a<min)min=a;

{xl=(-b+sqrt(d))/(2*a);s=s+a;

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

cout?z,xl=,z?xK<,z,x2=*<<x2?endl;cout?max?z,,/z<<min?w,v?s/20.0?endl;

))

elsecout<<”方程無實根\n”;

I6.輸入若干個數(shù),設(shè)輸入的第一個數(shù)為后面要輸

}入的數(shù)的個數(shù),求平均值及最大值。

intmainO

3.輸入一個成績,打印相應的等級(

intmain()inta,m,s=0;

{inta;cin?n;

cm?a;cin?a;

if(a>=90)coui?"A";m=a:s=a;

elseif(a>=80)cout?"B";for(inti=l;i<n;i++)

e-seif(a>=70)cout?"C”;{cin?a;

s+=a;tor(inti=U;i<=63;1++)

if(a>m)m=a;{s=s+t;

)t=2*t;

cout<<"平均值:"<<(double)s/n<<:最大值:}

*?n?endl:cout?s/l.4e8?endl;

}}

7、輸入若干個數(shù),輸入-999表示結(jié)束,求平均值

及最大值。10、求和s=l!+2!+3!+...+10

itinclude<iostream>intmain()

#include<iomanip>{inti;

#include<csldlib>longl,s;

usingnamespacestd;t=l;s=0;

intmain()for(i=l;i<=10;i++)

{intn,count,sum,max;{t=t*i;

doubleave;s=s+t;

count=0;}

cm?n;(

sum=0;max=n;

while(n!=-999)11.求e=l+1/1!+1/2!+1/3!+

{sum=sum+n;intmainO

if(n>max)max=n;(inti;

count++;doublet,e;

cin>>n;1—1,l—l,0-1,

}while(t>=le-7)

if(count!=0){t=t/i;

{ave=staticcast<double>(sum)/count;e=e+t;

cout<<setiosflags(ios::fixed)i=i+l;

〈〈setprecision(2);)

cout<<“平均值為:〃<<ave<<”最大值為:cout?e;

^<<nax?endl;)

i

J

}12.求PI值,PIA=1-1/3+1/5~1/7+...

intmainO

8、求和s=l*l+2*2+3*3+...+100*100{inti,k;

intmainOdoublepi,t;

{inti,t;i=l;t=l;pi=0;k=l;

doubles=0;while(fabs(t)>le-8)

for(i=l:i<=100;i++){pi=pi+t:

{t=i*i:i=i+2;

s=s+t;k=-k;

)t=double(k)/i:

})

cout?4*pi;

9、印度國王的獎勵,求和s=20+21+22+...+}

263

intmain()13.求PI值,PI/2=1+1/3-1/3*2/5+

(doublet=l,s=0;l/3*2/5*3/7+...

^include<iostream>s=s*x;

#include<cstdlib>returns;

intmain()}

(

inti,j;17、求水仙花數(shù)(一個三位數(shù),其各位數(shù)字立方和

doublepi,t;等于該數(shù)字本身〉

i=0;j=l;t=l;pi=0;intmainO

while(t>le-18){inti,a,b,c;

{pi=pi+t;for(i=100;i<=999;i++)

i=i+l:{a=i/100:

J-J+2;b-i/10%10;

t=t*i/j;c=i%10;

}if(i==a*a*a+b*b*b+c*c*c)

cout?setprecision(17)?2*pi;cout<<i?endl;

})

)

14.輸入20個數(shù),統(tǒng)計其中正數(shù)、負數(shù)和零的個數(shù)。

intmain()intmain()

({inti,a,b,c;

inta,n=0,m=0,s=0;for(a=l;a<=9;a++)

for(inti=l;i<=20;i++)for(b-0;b<=9;b++)

{cin?a;for(c=0;c<=9;c++)

if(a>0)n++;{i=a*100+b*10+c;

elseif(a<0)m++;if(i==a*a*a+b*b*b+c*c*c)

elses++;cout<<i?cndl:

)

cout?n?z,”<<s;

}

18、編寫一個函數(shù),確定一個整數(shù)是否為完全數(shù)

15.輸入若干個整數(shù),計算其中的奇數(shù)之和與偶數(shù)(一個數(shù),等于他的因子之和)。用這個函數(shù)確定

之和,假設(shè)輸入。表示結(jié)束.和打印1到1000之間的所有完全數(shù).

intmain()intperfect(intn)

{inta,n=0,m=0:{inti,s=l;

cm?a;for(i=2;i<=n/2;i++)

while(a!=0)if(n%i==0)s=s+i;

{if(a%2==0)n+=a;;if(s==n)return1;

elsem+=a;elsereturn0;

cin>>a;)

)intmainO

cout<<n?/,”<<m;{intn;

|for(n=2:n<=1000:n++)

ifperfect(n)

16.寫一函數(shù),計算x的y次方(假設(shè)x、y都為正cout<<n?en(ll;

整數(shù))。)

intpow(intx,inty)

{ints=l;19、寫一函數(shù),求斐波那契數(shù)列的第n項。

for(inti=l;i<=y;i++)intfib(intn)

while(n)

intf;{cout?n/k?

if(n==l||n==2)return1;n%=k;

fl=l;f2=l;k/=10;

for(i=3;i<=n;i++)};

{f=fl+f2;

fl=f2;

f2=f;voidshow(intn)

){inta[l0],i=0;

returnf;while(n)

}{a[i]-ii%10;

n/=10;

20、寫?個函數(shù),取?個整數(shù)值并返回將此整數(shù)的i++;

各數(shù)字反序的數(shù)值)

intreverse(intn)for(intj=i-l;j>=0;j-)

{ints=0;cout?a[j]?*

while(n)(

{s=s*10+n%10;

n/=10;voidshow(intn)

);{if(n<10)cout?n;

return3;else

}{show(n/10);

cout<<""?n%10;

21.寫一個函數(shù),將一個整數(shù)的各位數(shù)字的反序打}

印)

voidshow(intn)

(while(n)23.求一個整數(shù)的各位數(shù)之和的函數(shù)

{cout?n%10?intsum(intn)

n/=10;{ints=0;

);whilc(n)

{s+=n%10;

n/=10;

voidshow(intn));

{if(n<10)cout?n:returns;

else)

{cout?n%10

show(n/10);24.寫一函數(shù),判斷某個數(shù)是否素數(shù),以及求1-

}1000之內(nèi)的素數(shù)

}#includc<iostrcam>

#include<cmath>

22.寫一個函數(shù),將一個整數(shù)的各位數(shù)字的按順序#include<stdlib.h>

打印出來usingnamespacestd;

voidshow(intn)boolisprime(intn)

((floatk=sqrt(float(n));

intk=1,m=n;for(inti=2;i<=k;i++)

whi1e(m>10)if(n%i==0)returnfalse;

{k*=10;m/=10;}returntrue;

}intgcd(intm,intn)

intmainO{intk;

{for(intn=2;n<=1000:n++)while(n!=0)

if(isprime(n))cout?setw(5)?n;{k=m%n;m=n;n=k;}

}returnm;

}

25.用篩法求1—1000之內(nèi)的素數(shù)

#include<iostream>intgcd(intm,intn)

#include<cmath>{intk;

#include<stdlib.h>while((k=n^n)!=0)

includc<iomanip>{rn-n;n-k;}

usingnamespacestd;returnn;

intmain())

(

inti,k,a[1001]:intgcd(intm,intn)

for(i=2;i<=1000;i++)a[i]=l;(whilc(m!=n)

floats=sqrt(float(1000));{if(m>n)m=m-n;

for(i=2;i<=s;i++)elsen=n-m;

if(a[i]=l)}

{k=2*i;returnm;

while(k<=1000))

{a[k]=0;

k=k+i;29、求兩個數(shù)的最小公倍數(shù)

}intlcm(intm,intn)

){intt,s;

for(i=2;i<=1000;i++)if(m<n){t=m;m=n;n=t;}

if(a[i]==1)cout?setw(5)?i;s=m;

}while(s%n!=0)s=s+m;

26.判斷某一年是否閏年的函數(shù)}

boolIsLeapYcar(inty)

{return(y%4==0&&y%100!=0)||(y%400二intlcm(intm,intn)

0);(

}returnm*n/gcd(m,n);

)

27、寫一個函數(shù),交換兩個整型變量的值

voidswap(int*p,int*q)30、百錢買百雞問題:雞翁一值錢五,雞母一值錢

{intt;三,雞雛三值錢一,百錢買百雞,問雞翁、母、雛

t=*p;*p=*q;*q=t;各幾何?

}intmainO

(

voidswap(int&a,int&b)intcock.hen,chick:

{intt;for(cock=0;cock<=20;cock++)

t=a;a=b;b=t;for(hen=0;hen<=33;hen++)

}{chick=100-cock-hen;

if(5*cock+3*hen+chick/3.0==100)

28、求兩個數(shù)的最大公約數(shù),歐兒里德算法(根轉(zhuǎn)相cout?setw(4)?cock?setw(4)<<hen

除法)?setw(4)?chick?endl:

)}

}

31.編一程序,輸入一行字符串,統(tǒng)計其中的小寫34.打印一個九九乘法表

英文字母的個數(shù)。#include<iostream>

intmainO#includc<iomanip>

{chars[100];usingnamespacestd;

c:n.getline(s,100);intmainO

inti=0,count=0;I

while(s[i]\0*)for(intj=l;j<=9;j++)

{a*&&sEiK-z*){for(inii-1;i<-j;i++)

count++;cout?i?***?j?*=*?setw(2)?i*j<<*

i++;t

)cout?endl;

cout?count?endl;)

}}

32.編一程序,輸入一行字符串,將其中的大寫英35.擲骰子10000次,統(tǒng)計得到各點數(shù)的次數(shù)。

文字母改為小寫,再輸出。intmainO

intmainO{inta[7]={0};

{char3[100];srand(time(0));

inti;for(inti=l;i<=10000;++i)

c:n.get]ine(s,100);++a[1+rand()%6];

i=0;for(inti=l;i<=6;++i)

while(s[i]!=\0')cout?i?/,:*?a[i]?endl;

if(s[i]>=&&s[i]<=Z')}

s[i]=s[i]+32;

cout?s?endl;36.編寫函數(shù)distance,計算兩點(xl,yl)和

}(x2,y2)之間的距離。

doubledistance(doublexl,doubleyl,double

3工?T印楊輝三角形(帕斯卡三角形)打印10行.x2,doubley2)

#inc1ude<iostream>I

#include<iomanip>returnsqrt((xl-x2)*(xl-x2)+

usingnamespacesld;(yl-y2)*(yl-y2)):

intmainO)

{inta[10][10]={0};

for(inti=0;i<10;i++)37、寫一個程序,進行體操評分,依次輸入10名

{a[i][0]=l;評委所評分數(shù),去除一個最高分和一個最低分,

a[i][i]=l;再算出平均分作為選手的得分。

}intmainO

for(inti=l:i<10:i++){inti:

for(intj=l;j<i;j++)floatmax,min,s,x;

a[i][j]=a[i-l][j-l]+a[i-l][j];max=0;min=10;s=0;

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

{for(intj=0;j<=i;j++){cin?x;

cout?setw(4)?a[i][j];s=s+x;

cout<<endl;if(x<min)min=x;

if(x>max)max=x;{ii(*strl=>\0')return0;

)strl++;str2++;

s=s-min-max;}

cout<<s/8;return*strl-*str2;

})

38、寫一函數(shù),將一數(shù)組中的元素反轉(zhuǎn)。43.寫一個字符串連接函數(shù)

voidreverse(inta[],intn)char*strcat(char*strl,char*str2)

{for(inti=0;i<n/2;i++)I

swap(a[i],a[n-i-l]);char*p=strl;

}whilc(*sirl!-0)slrl++;

while(*strl++=*str2++);

39、寫一函數(shù),在一個數(shù)組中找出最大元素的位置returnp;

intSeai'chMax(inta[],intn)}

{intk=0;

for(inti=l;i<n;i++)44.寫一個求字符串長度函數(shù)

if(a[i]>a[k])k=i;intstrlen(char*str)

returnk;I

}intn=0;

while(*str!-\0f)

40、找出一個二維數(shù)組中的鞍點,即該元素在該行{n++;3tr++;}

上最大,在該列上最小。returnn;

}

41.寫一個字符串拷貝函數(shù)

voidstrcpy(char*p,constchar*q)45.寫一函數(shù),在一數(shù)組里查找某個值。

(intsearch(inta[],intn,intkey)

while(*p++=*q++);{for(inti=0;i<n;i++)

}if(a[i]==key)

returni;

char*strcpy(char*strl,constcnar*str2)return-1:

()

char*p=str1;

while(*strl++=*str2++);46.編一程序,求兩個矩陣的乘積

returnp;

}47、計算某日是某年的第幾天

boolisLeapYear(inty)〃判斷某一年是否閏

42.寫一個字符串比較函數(shù)年

intstrcmp(char*strl,constchar*str2)(

(return(y%4==0&&y%100!=0)||(y%400==

while(*strl&&*str2&&*strl==*str2)0);

{strl++:str2++:}}

return*strl-*str2;

}intmainO

(

intstrcmp(char*strl,constchar*str2)intyear,month,day,i,s=0;

(int

while(*strl==*str2)a[13]=(0,31,28,31,30,31,30,31,31,30,31,30,3

1):swap(a[k],a[bl-i]);

cm?ycar?nionth?day:}

for(i=l;i<month;i++)for(i=0;i<13;i++)cout?b[i]?*

s=s+a[i];)

s=s+day;

if(isLeapYear(year)&&month>2)s++;50、求100!

cout?s;#include<fstream>

}#include<cmath>

#include<iomanip>

48、編寫一個幫助小學生學習加法的程序,隨機產(chǎn)usingnamespacestd;

生2個數(shù),讓學生輸入答案。constlongMOD-10000;

#include<iostream>intmainO

#include<cstdlib>

溫馨提示

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

最新文檔

評論

0/150

提交評論