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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、50道C/C+編程練習題1 / 91、輸入3個數,求最大值int main() int a,b,c,m; cin>>a>>b>>c; m=a; if(b>m) m=b; if(c>m) m=c; cout<<m; 2、編程序,求方程ax2+bx+c=0的根#include<iostream>#include<cmath>using namespace std;int main() double a,b,c,d,x1,x2; cin>>a>>b>>c; if(a=0) if(b

2、=0) cout<<"errorn" else cout<< "x="<<-c/b<<endl; else d=b*b-4*a*c; if(fabs(d)<=1e-6) cout<<"x1=x2="<<-b/(2*a)<<endl; else if(d>1e-6) x1=(-b+sqrt(d)/(2*a); x2=(-b-sqrt(d)/(2*a); cout<<"x1="<<x1<<

3、",x2="<<x2<<endl; else cout<<"方程無實根n" 3、輸入一個成績,打印相應的等級int main() int a; cin >> a; if(a>=90) cout<<"A" else if(a>=80) cout<<"B" else if(a>=70) cout<<"C" else if(a>=60) cout<<"D" els

4、e cout<<"E"4、輸入3個double類型的值,判斷這3個值是否可以表示一個三角形的三條邊。int main() double a,b,c; cin>>a>>b>>c; if(a+b>c && b+c>a && c+a>b) cout<<"可以構成三角形" else cout<<"不可以構成三角形"5、輸入20個數,求其最大、最小和平均值 int main() int i; int a,max,min,s;

5、 cin>>a; max=min=a; s=a; for(i=1;i<20;i+) cin>>a; if(a>max) max=a; if(a<min) min=a; s=s+a; cout<<max<<","<<min<<","<<s/20.0<<endl; 6、輸入若干個數,設輸入的第一個數為后面要輸入的數的個數,求平均值及最大值。int main() int a,m,s=0; cin>>n; cin>>a; m=

6、a; s=a; for(int i=1; i<n; i+) cin>>a; s +=a; if(a>m) m=a; cout<<"平均值:"<<(double)s/n<<",最大值:"<<m<<endl; 7、輸入若干個數,輸入-999表示結束,求平均值及最大值。#include<iostream> #include<iomanip>#include<cstdlib> using namespace std; int main() in

7、t n, count, sum, max; double ave; count = 0; cin >> n; sum = 0; max = n; while( n != -999 ) sum = sum + n; if( n > max ) max = n; count+; cin >> n; if( count != 0 ) ave=static_cast<double>(sum) / count; cout<<setiosflags(ios:fixed) <<setprecision(2); cout<<&quo

8、t;平均值為:"<<ave<<" 最大值為:"<<max<<endl; 8、求和 s=1*1 + 2*2 + 3*3 +.+ 100*100int main() int i,t; double s=0; for(i=1; i<=100; i+) t=i*i; s=s+t; 9、印度國王的獎勵,求和 s=20 + 21 + 22 +.+ 263 int main() double t=1,s=0; for(int i=0; i<=63; i+) s=s+t; t=2*t; cout<<s/1.4

9、e8<<endl; 10、求和 s=1! + 2! + 3! +.+ 10! int main() int i; long t,s; t=1; s=0; for(i=1; i<=10; i+) t=t*i; s=s+t; 11、求 e=1 + 1/1! + 1/2! + 1/3! + .int main() int i; double t,e; i=1; t=1; e=1; while(t>=1e-7) t=t/i; e=e+t; i=i+1; cout<<e; 12、求PI值,PI/4 = 1 - 1/3 + 1/5 - 1/7 + . int main(

10、) int i,k; double pi,t; i=1; t=1; pi=0; k=1; while(fabs(t)>1e-8) pi=pi+t; i=i+2; k=-k; t=double(k)/i; cout<<4*pi; 13、求PI值,PI/2 = 1 + 1/3 + 1/3*2/5 + 1/3*2/5*3/7 + . #include<iostream> #include<cstdlib> int main() int i,j; double pi,t; i=0; j=1; t=1; pi=0; while(t>1e-18) pi=pi

11、+t; i=i+1; j=j+2; t=t*i/j; cout<<setprecision(17)<<2*pi; 14、輸入20個數,統(tǒng)計其中正數、負數和零的個數。int main() int a,n=0,m=0,s=0; for(int i=1; i<=20; i+) cin >> a; if(a>0) n+; else if(a<0) m+; else s+; cout<<n<<" "<<m<<" "<<s;15、輸入若干個整數,計算其中

12、的奇數之和與偶數之和,假設輸入0表示結束。int main() int a,n=0,m=0; cin>>a; while(a!=0) if(a%2 = 0) n += a; else m += a; cin >> a; cout<<n<<" "<<m;16、寫一函數,計算x的y次方(假設x、y都為正整數)。int pow(int x, int y) int s=1; for(int i=1; i<=y; i+) s = s * x; return s;17、求水仙花數(一個三位數,其各位數字立方和等于該數字本

13、身)int main() int i,a,b,c; for(i=100;i<=999;i+) a=i/100; b=i/10%10; c=i%10; if(i=a*a*a+b*b*b+c*c*c) cout<<i<<endl; int main() int i,a,b,c; 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) cout<<i<<endl; 18、編寫一個函數,確定一個整數是否為完

14、全數(一個數,等于他的因子之和)。用這個函數確定和打印1到1000之間的所有完全數。int perfect(int n) int i,s=1; for(i=2;i<=n/2;i+) if(n%i=0) s=s+i; if(s=n) return 1; else return 0;int main() int n; for(n=2;n<=1000;n+) if perfect(n) cout<<n<<endl; 19、寫一函數,求斐波那契數列的第n項。int fib(int n) int i,f1,f2,f; if(n=1|n=2) return 1; f1=

15、1; f2=1; for(i=3; i<=n; i+) f=f1+f2; f1=f2; f2=f; return f;20、寫一個函數,取一個整數值并返回將此整數的各數字反序的數值int reverse(int n) int s=0; while(n) s = s * 10 + n % 10; n /= 10; ; return s;21、寫一個函數,將一個整數的各位數字的反序打印void show(int n) while(n) cout << n % 10 << " " n /= 10; ;void show(int n) if(n <

16、; 10) cout << n; else cout << n % 10 << " " show(n / 10); 22、寫一個函數,將一個整數的各位數字的按順序打印出來void show(int n) int k = 1, m = n; while(m > 10) k *= 10; m /= 10; while(n) cout << n / k << " " n %= k; k /= 10; ;void show(int n) int a10, i=0; while(n) ai = n

17、% 10; n /= 10; i+; for(int j=i-1; j>=0; j-) cout<<aj<<" "void show(int n) if( n < 10 ) cout << n; else show( n / 10 ); cout << " " << n % 10; 23、求一個整數的各位數之和的函數int sum(int n) int s = 0; while(n) s += n % 10; n /= 10; ; return s;24、寫一函數,判斷某個數是否素數

18、,以及求11000之內的素數#include<iostream> #include<cmath> #include<stdlib.h> using namespace std; bool isprime(int n) float k=sqrt(float(n); for(int i=2; i<=k; i+) if(n%i=0) return false; return true; int main() for(int n=2; n<=1000; n+) if(isprime(n) cout<<setw(5)<<n; 25、

19、用篩法求11000之內的素數 #include<iostream> #include<cmath> #include<stdlib.h> #include<iomanip> using namespace std; int main() int i,k,a1001; for(i=2; i<=1000; i+) ai=1; float s=sqrt(float(1000); for(i=2; i<=s; i+) if(ai=1) k=2*i; while(k<=1000) ak=0; k=k+i; for(i=2; i<=1

20、000; i+) if(ai=1) cout<<setw(5)<<i; 26、判斷某一年是否閏年的函數bool IsLeapYear(int y) return (y%4 = 0 && y%100 != 0)|(y%400 = 0); 27、寫一個函數,交換兩個整型變量的值 void swap(int *p, int *q) int t; t=*p; *p=*q; *q=t; void swap(int &a, int &b) int t; t=a; a=b; b=t; 28、求兩個數的最大公約數,歐幾里德算法(輾轉相除法)int gcd

21、(int m, int n) int k; while(n!=0) k=m%n; m=n; n=k; return m; int gcd(int m, int n) int k; while(k=m%n)!=0) m=n; n=k; return n; int gcd(int m, int n) while(m!=n) if(m>n) m=m-n; else n=n-m; return m; 29、求兩個數的最小公倍數int lcm(int m, int n) int t,s; if(m<n) t=m; m=n; n=t; s=m; while(s%n != 0) s=s+m; i

22、nt lcm(int m, int n) return m*n/gcd(m,n); 30、百錢買百雞問題:雞翁一值錢五,雞母一值錢三,雞雛三值錢一,百錢買百雞,問雞翁、母、雛各幾何?int main() int cock,hen,chick; for(cock=0; cock<=20; cock+) for(hen=0; hen<=33; hen+) chick=100-cock-hen; if(5*cock+3*hen+chick/3.0=100) cout<<setw(4)<<cock<<setw(4)<<hen <<

23、setw(4)<<chick<<endl; 31、編一程序,輸入一行字符串,統(tǒng)計其中的小寫英文字母的個數。int main() char s100; cin.getline(s,100); int i=0,count=0; while(si!='0') if(si>='a' && si<='z') count+; i+; cout<<count<<endl; 32、編一程序,輸入一行字符串,將其中的大寫英文字母改為小寫,再輸出。int main() char s100;

24、int i; cin.getline(s,100); i=0; while(si!='0') if(si>='A' && si<='Z') si=si+32; cout<<s<<endl;33、打印楊輝三角形(帕斯卡三角形),打印10行。#include<iostream>#include<iomanip> using namespace std;int main() int a1010=0; for(int i=0; i<10; i+) ai0=1; aii=1;

25、 for(int i=1; i<10; i+) for(int j=1; j<i; j+) aij = ai-1j-1 + ai-1j; for(int i=0; i<10; i+) for(int j=0; j<=i; j+) cout<<setw(4)<<aij; cout<<endl; 34、打印一個九九乘法表#include<iostream>#include<iomanip> using namespace std;int main() for(int j=1; j<=9; j+) for(in

26、t i=1; i<=j; i+) cout<<i<<"*"<<j<<"="<<setw(2)<<i*j<<" " cout<<endl; 35、擲骰子10000次,統(tǒng)計得到各點數的次數。int main() int a7=0; srand(time(0); for(int i=1; i <= 10000 ; +i) +a 1 + rand()%6 ; for(int i=1; i <= 6 ; +i) cout<&l

27、t;i<<": "<<ai<<endl; 36、編寫函數distance,計算兩點(x1,y1)和(x2,y2)之間的距離。 double distance(double x1, double y1, double x2, double y2) return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );37、寫一個程序,進行體操評分,依次輸入10名評委所評分數,去除一個最高分和一個最低分,再算出平均分作為選手的得分。int main() int i; float max,min,s,x; max =

28、 0; min = 10; s=0; for(i=1;i<=10;i+) cin >> x; s = s + x; if(x<min) min = x; if(x>max) max = x; s = s - min - max; cout << s/8; 38、寫一函數,將一數組中的元素反轉。void reverse(int a, int n) for(int i=0; i<n/2; i+) swap(ai,an-i-1);39、寫一函數,在一個數組中找出最大元素的位置int SearchMax(int a, int n) int k = 0;

29、for(int i=1; i<n; i+) if(ai>ak) k = i; return k;40、找出一個二維數組中的鞍點,即該元素在該行上最大,在該列上最小。41、寫一個字符串拷貝函數 void strcpy(char *p, const char *q) while(*p+=*q+); char *strcpy(char *str1, const char *str2) char *p=str1; while(*str1+=*str2+); return p; 42、寫一個字符串比較函數int strcmp(char *str1, const char *str2) whi

30、le(*str1 && * str2 && *str1=*str2) str1+; str2+; return *str1-*str2; int strcmp(char *str1, const char *str2) while(*str1=*str2) if(*str1='0') return 0; str1+; str2+; return *str1-*str2; 43、寫一個字符串連接函數char *strcat(char *str1, char *str2) char *p=str1; while(*str1!=0) str1+; wh

31、ile(*str1+=*str2+); return p; 44、寫一個求字符串長度函數int strlen(char *str) int n=0; while(*str!='0') n+; str+; return n; 45、寫一函數,在一數組里查找某個值。int search(int a, int n, int key) for(int i=0; i<n; i+) if(ai=key) return i; return -1;46、編一程序,求兩個矩陣的乘積47、計算某日是某年的第幾天bool isLeapYear(int y) /判斷某一年是否閏年 return

32、(y%4 = 0 && y%100 != 0)|(y%400 = 0); int main() int year,month,day,i,s=0; int a13=(0,31,28,31,30,31,30,31,31,30,31,30,31; cin>>year>>month>>day; for(i=1; i<month; i+) s= s + ai; s = s + day; if(isLeapYear(year) && month>2) s+; cout << s; 48、編寫一個幫助小學生學習加法

33、的程序,隨機產生2個數,讓學生輸入答案。 #include<iostream>#include<cstdlib>using namespace std;int main() int x,y,z; srand( time(0) ); x = rand() % 1000; y = rand() % 1000; cout << x << " + " << y << " = " cin >> z; while( z != 0 ) while( z != x+y ) cout<<" × 錯誤!請重做n" ; cout<<x<<" + "<<y<<" = " cin>>z; cout<<" 正確!n" ; x = rand() % 1000; y = rand() % 1000; cout<<x

溫馨提示

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

評論

0/150

提交評論