C++編程練習(xí)題及代碼_第1頁
C++編程練習(xí)題及代碼_第2頁
C++編程練習(xí)題及代碼_第3頁
C++編程練習(xí)題及代碼_第4頁
C++編程練習(xí)題及代碼_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、C+褊程練習(xí)題及代碼5000/*Problem Description 判斷一個(gè)數(shù)是否為素?cái)?shù)InputT 組測試數(shù)據(jù)。輸入數(shù)據(jù)首先包含一個(gè)整數(shù)T,表示測試實(shí)例的個(gè)數(shù),然后是每組測試數(shù)據(jù)包含一個(gè)正整數(shù)n. (n1)Output如果 n 為素?cái)?shù)則輸出 yes反之輸出 no*/#include using namespace std; int main()int T,c,n;cinT;for(int i=0;in;for(c=2; cn; c+)if(n%c= =0)break;if(c= =n)coutYESendl;else coutNOendl;return 0;5004/*Problem D

2、escription 求1,n之和。Input輸入一個(gè)正整數(shù)n。本題只有一組測試 !Output輸出1+2+n之和。*/#include using namespace std; int main()int i,sum=0,n;cinn;for (i=1;i=n;i+)sum=sum+i;printf(The sum=%dn,sum);return 0;6011/*Problem Description求兩個(gè)正整數(shù) m和n的最大公約數(shù)Input測試數(shù)據(jù)有多組,處理到文件尾。每組測試數(shù)據(jù)在同一行輸入兩個(gè)正整數(shù)n。Output每組輸出m和n的最大公約數(shù)。*/#include #includeusi

3、ng namespace std; int main()第 1 頁 共 8 頁int m,n,x;while(cinmn)if (mn) x=n;else x=m;while(m%x0|n%x0)x=x-1;coutxendl;return 0;6012/* 從 5 個(gè)數(shù)中取最大數(shù)及最小數(shù)Input測試數(shù)據(jù)有多組,處理到文件尾。每組測試數(shù)據(jù)在同一行輸入 5 個(gè)浮點(diǎn)數(shù)。Output每組輸出其中的最大數(shù)及最小數(shù)( 保留 6 位小數(shù) ) 。 */ #include#includeusing namespace std; int main()double a,b,c,d,e,min,max;while

4、(cinabcde)min=a;max=a;if (bmin) min=b;if (cmin) min=c;if (dmin) min=d;if (emax) max=b;if (cmax) max=c;if (dmax) max=d;if (emax) max=e;printf(%.6lf ,max);printf(%.6lfn,min);return 0;6013/*Problem Description 判斷所輸入年份是否為閏年。Input對于每組輸入年份n,若n為閏年,輸出is LEAP!,否則輸出is NOT LEAP!*/#include#includeusing namespac

5、e std; int main()int n;while(cinn)if (n%4=0) coutis LEAP!endl;第 2 頁 共 8 頁else coutis NOT LEAP!endl;return 0;6014/*Problem Description由鍵盤輸入 3 個(gè)浮點(diǎn)數(shù) ( 建議使用 double 型) ,計(jì)算以這3 個(gè)數(shù)為邊長的三角形的面積。若這3 個(gè)數(shù)不構(gòu)成三角形, 則不輸出。Input測試數(shù)據(jù)有多組,處理到文件尾。每組測試輸入 3 個(gè)浮點(diǎn)數(shù)。 Output每組輸出以這3 個(gè)數(shù)為邊長的三角形的面積( 保留 6 位小數(shù) ) 。 */#include#include#inc

6、ludeusing namespace std;int main()double a,b,c,p,s;while(cinabc)if (a+bc & a+cb &b+ca)p=(a+b+c)/2;s=sqrt(p*(p-a)*(p-b)*(p-c);printf(%.6lfn,s);return 0;6015/*Problem Description輸出形狀為直角三角形的九九乘法表( 每個(gè)元素占 4 個(gè)位置 ) Sample Output1 2 3 4 5 6 7 8 912 43 6 94 4 8 12 165 5 10 15 20 256 6 12 18 24 30 367 7 14 21

7、 28 35 42 498 8 16 24 32 40 48 56 649 9 18 27 36 45 54 63 72 81*/#include#include#includeusing namespace std;int main()int i,j,a,b,n;printf(%8d,1);for(i=2;i=9;i+) printf(%4d,i);coutendl;j=1;while(j=9)第 3 頁 共 8 頁printf(%4d,j);for(i=1;i=j;i+) n=i*j;printf(%4d,n); coutendl;j=j+1;return 0;6016/*Problem

8、Description按下面的近似公式計(jì)算e=1+1/1!+1/2!+1/3!+ +1/n!+ 使誤差小于給定的誤差限s(0.0000000025=s=0.1) ,即 1/n!s 。輸出的 e 顯示 18 位小數(shù)。Input測試數(shù)據(jù)有多組,處理到文件尾。每組測試輸入誤差限 s 。 Output每組輸出以 s 為誤差限的近似值e。 */ #include#include#includeusing namespace std; int main()double e,s,x;int m,i;while(cins)e=1;m=1;i=1;x=1.0/m;while(x=s)e=e+x;i=i+1;m=

9、m*i;x=1.0/m;e=e+x;printf(%.18lfn,e);return 0;6017/*Problem Description 若一頭小母牛從第4 個(gè)年頭開始每年生育一頭母牛,按照此規(guī)律,第 n 年時(shí)有多少頭母牛。Input測試數(shù)據(jù)有多組,處理到文件尾。每組輸入第 n 年的母牛數(shù)。Output每組輸出第 n 年時(shí)的母牛數(shù)。 */#include using namespace std; int main()int i,j,n,x;int a1000;while(cinn)x=1;a1=1;a2=0;a3=0;a4=1;第 4 頁 共 8 頁if (n4) coutxendl;el

10、se for(i=4;i=n;i+) ai=0;for(j=4;j=i;j+) ai=ai+aj-3;x=x+ai;coutxendl;return 0;6018/*Problem Description 某鐵路線上共有n(5=n=100)個(gè)車站,問需要準(zhǔn)備幾 種車票 ?Input測試數(shù)據(jù)有多組,處理到文件尾。每組輸入車站個(gè)數(shù)n。 Output每組輸出要準(zhǔn)備的車票種數(shù)。 */#include using namespace std; int main()int n,x;while(cinn)x=n*(n-1);coutxendl;return 0;6019/*Problem Descripti

11、on 有 100 匹馬,馱 100 擔(dān)貨,大馬馱 3擔(dān),中馬馱2擔(dān),兩匹小馬馱1 擔(dān),問有大、中、小馬各多少匹 ? */#include using namespace std; int main()int a100,b100,c100;int i,j;for (i=0;i=99;i+)ai=i;for (j=0;j=(100-ai*3)/2;j+)bj=j;cj=100-ai-bj;if (ai*3+bj*2+cj/2=100 & cj%2=0) coutai bjcjendl;return 0;6020/*Problem Description 驗(yàn)證歐拉公式an=nX n -n+41第 5

12、 頁 共 8 頁是否是一個(gè)在-3940 范圍內(nèi)計(jì)算素?cái)?shù)n 的通項(xiàng)公式。 */ #includeusing namespace std; int main()int n,i,m,an;m=0;for (n=-39;n=40;n+)an=n*n-n+41;if (an%2=0) m=1;else for(i=3;i=an/2+1;i+)if (an%i=0) m=1;if (m=1) coutNOendl;else coutYESendl;return 0;6021/*Problem Description 愛因斯坦的階梯問題。設(shè)有一階梯,若每步跨2 階,最后余1 階; 若每步跨 3 階,最后余2

13、 階; 若每步跨 5 階,最后余4 階 ; 若每步跨 6 階,最后余5 階 ; 當(dāng)每步跨 7 階時(shí),正好到達(dá)階梯頂。問共有多少級(jí)階梯 ? */#include using namespace std; int main()int i,x;for (i=1;i=1000;i+) x=i*7;if (x%2=1 & x%3=2 & x%5=4 & x%6=5) break;coutxendl;return 0;6023/*Problem Description 求 S=a+aa+aaa+ +aa a(n 個(gè) a)之值,其中 a 是一個(gè)數(shù)字,如 2+22+222+2222+22222此時(shí) n=5),

14、 n(0n10)和 a 由鍵盤輸入。Input測試數(shù)據(jù)有多組,處理到文件尾。每組輸入 n 和 a。 Output每組輸出 a+aa+aaa+ +aa a(n 個(gè) a)之值 */ #include#includeusing namespace std; int main()int b10;int n,i,a,j,c,l,s;while(cinna)s=0;for (i=1;i=n;i+) bi=a;c=1;for (j=2;j=i;j+) c=c*10;bi=bi+a*c;第6頁共8頁s=s+bi;coutsendl;return 0;6024/*Problem Description求 S=1

15、+2+3+ +n+1X 1+2X 2+3X 3+ +nX n+1/1+1/2+1/3+ +1/n 0 (n100)Input測試數(shù)據(jù)有多組,處理到文件尾。每組輸入輸入 n0Output每組輸出 1+2+3+ +n+1X1+2X 2+3X 3+ - +nX n+1/1+1/2+1/3+ +1/n 之值。保留六位小數(shù)*/#include#includeusing namespace std; int main()double b100;int n,i;double s;while(cinn)s=0;for (i=1;i=n;i+) bi=i+i*i+1.0/i;s=s+bi;printf(%.6l

16、fn,s);return 0;6025/*Problem Description打印出所有的 水仙花數(shù) 。所謂 水仙花數(shù) 是指一個(gè) 3 位數(shù),其各位數(shù)字的立方和等于該數(shù)本身。例如, 153 是一水仙花數(shù),因?yàn)?153=1*1*1+5*5*5+3*3*3 。 */#include#includeusing namespace std;int main()(int a,b,c,i;for (a=1;a=9;a+)for (b=0;b=9;b+)for (c=0;c=9;c+)i=a*100+b*10+c;if (i=a*a*a+b*b*b+c*c*c)couti=a*a*a+b*b*b+c*c*c

17、endl;)return 0;)6028/*Problem Description猴子第一天摘下若干桃子,當(dāng)即吃了一半,還覺不過癮,又多吃了一個(gè);第二天早上又將剩下的桃子吃掉一半,又多吃了一個(gè)。以后每天天早上都吃了前一天剩下的一半零一個(gè)。到第 n(n=20)天早上想再吃時(shí),見只剩一個(gè)桃子了。求第一天共摘了多少桃子。第7頁共8頁Input測試數(shù)據(jù)有多組,處理到文件尾。每組輸入天數(shù)n。*/ #include#includeusing namespace std; int main()int i,n,s;while(cinn)s=1;for (i=n-1;i=1;i-) s=(s+1)*2;printf(%dn,s);return 0;6032/*Problem Description 輸入邊長 n(n?20) ,用 * 號(hào)打印邊長為 n 的菱形( 見 Sample Input 和 Sample Output) 。Input測試數(shù)據(jù)有多組,處理到文件尾。每組輸入邊長n。 Output每組輸出以 n 為邊長的

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論