C++经典编程练习题.docx
精选优质文档-倾情为你奉上C+经典编程例题(1)1、(已验证!)计算铁路运费。已知从甲地到乙地,每张票托运行李不超过50公斤时,按每公斤0.13元,超过50公斤,超过部分按每公斤0.2元计算。输入行李重量w,计算运费y.#include <iostream>using namespace std;void main()float money=0,weight;cout<<"请输入货物重量(单位:千克):"cin>>weight;if (weight<0)cout<<”数据错误!”<<endl;if (weight <= 50)money = weight * 0.13;else if (weight > 50)money = 50 * 0.13 + (weight - 50) * 0.2;cout<<"n总费用是:"<<money<<endl; 2、(已验证!)根据一元二次方程a*x*x+b*x+c=0求解方程。输入a、b、c的值,求x1、x2的实根.#include <iostream>#include <cmath>using namespace std;void main() double a,b,c,p,x1,x2,D; cout<<"请输入3个数:"<<endl; cout<<"a=" cin>>a; cout<<"b=" cin>>b; cout<<"c=" cin>>c; cout<<"方程为:"<<a<<"x2+"<<b<<"x+"<<c<<"=0"<<endl; D=b*b-4*a*c; if (D<0) cout<<"方程无解!"<<endl; if (D=0) p=-b/(2*a); x1=p+sqrt(D)/(2*a); cout<<"方程有两个相同的实根。"<<endl; cout<<"x1=x2="<<x1<<endl; if (D>0) p=-b/(2*a); x1=p+sqrt(D)/(2*a); x2=p-sqrt(D)/(2*a); cout<<"方程有两个不同的实根。"<<endl; cout<<"x1="<<x1<<endl<<"x2="<<x2<<endl; 3、(已验证!)分别使用for和while求n!.注解n!=1*2*3*.*n,n由键盘输入。For:#include <iostream>using namespace std;void main() int i,n,s; s=1; cout<<"input n: " cin>>n; for (i=1;i<=n;i+) s=i*s; cout<<s<<endl; While:#include <iostream>using namespace std;void main() int i,n,s; s=1; i=1; cout<<"input n: " cin>>n; while(i<=n) s=i*s; +i; cout<<s<<endl; 4、(已验证!)百钱买百鸡。1只公鸡5元,1只母鸡3元,3只小鸡1元,现有100元钱要买100只鸡,有什么解法。分别使用三重循环和二重循环来解.鸡翁,鸡婆,鸡雏各:12、4、84只鸡翁,鸡婆,鸡雏各:8、11、81只鸡翁,鸡婆,鸡雏各:4、18、78只鸡翁,鸡婆,鸡雏各:0、25、75只(1).三重循环:#include <iostream>using namespace std;void main() const int cock=20,hen=33,chicken=100; int c,h,ck; for(c=0;c<=cock;c+) for(h=0;h<=hen;h+) for(ck=0;ck<=chicken;ck+) if(c+h+ck)=100)&&(c*5+h*3+ck/3)=100)&&(ck%3)=0) cout<<"鸡翁,鸡婆,鸡雏各:"<<c<<"、"<<h<<"、"<<ck<<"只"<<endl; (2).两重循环:#include<iostream>using namespace std;void main() int c,h,ck; for(h=0;h<33;h+) for(ck=0;ck<100;ck+) c=100-h-ck; if(5*c+h*3+ck/3=100 && ck%3=0 && h+ck<=100) cout<<"鸡翁,鸡婆,鸡雏各:"<<c<<"、"<<h<<"、"<<ck<<"只"<<endl; 5、(已验证!)求所有水仙花数。所谓水仙花数是指三位数,其各位数立方和等于该数。如153=1*1*1+5*5*5+3*3*3. (153 370 371 407)#include <iostream>using namespace std;void main() int a,b,c,d; for (d=100;d<1000;d+) a=d/100; /分解出百位 b=d/10%10;/分解出十位 c=d%10;/分解出个位 if (d=a*a*a+b*b*b+c*c*c) cout<<d<<" " cout<<endl; 6、(已验证!)解数学灯迷。有以下算式 ABCD-CDC=ABC,其中ABCD均为一位非负整数,求ABCD的值。(1098)#include <iostream>using namespace std;void main() int a,b,c,d,e; for (e=1000;e<10000;e+) a=e/1000; b=e/100%10; c=e/10%10; d=e%10; if (e-(c*100+d*10+c)=a*100+b*10+c) cout<<e; cout<<endl; 7、(已验证!)从键盘输入一个整数,判断此数是否为回文数。所谓回文数就是从左到右读与从右到左读是一样的数。如12321、7887等。#include <iostream>using namespace std;bool ifn(int x) int m=0,n; n=x; while(n) m=m*10+n%10; n=n/10; if(m=x) return true; if(m!=x) return false;void main( ) int a; cout<<"输入一个数:" cin>>a; if(ifn(a) cout<<a<<"是回文数!"<<endl; else cout<<a<<"不是回文数!"<<endl; 8、(已验证!)使用函数计算y=2*x+3,输入x,求y的值。#include <iostream>using namespace std;float y(int x) return 2*x+3;void main() int x; cout<<"输入X:" cin>>x; cout<<"Y="<<y(x)<<endl; 9、(已验证!)使用递归函数,求99+97+95+.+3+1之和。(和是2500。)#include <iostream>using namespace std;int sum(long n,long m) if(n=m) return(m); else return(m+sum(n,m-2);void main() int n,m; cout<<"输入范围:(nm):"<<endl; cout<<"n=" cin>>n; cout<<"m=" cin>>m; cout<<"从"<<n<<"到"<<m<<"的和为:"<<sum(n,m)<<endl; 10、(已验证!)已知杨辉三角11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 11 6 15 20 15 6 1.输出前13行的值.#include <iostream>using namespace std;void main() int a11,i,j; a1=1; for(i=1;i<=13;i+) ai=1; for(j=i-1;j>=2;j-) aj=aj+aj-1; for(j=1;j<=i;j+) cout<<aj<<" " cout<<"n" 11、(已验证!)约瑟夫问题:n个小孩围成一圈做游戏,给定一个数m,现从s个小孩开始,顺时计数,每数到m,该小孩出列,然后从下个小孩重新数数,数到m时,该小孩出列,如果反复,直到所以小孩出列。(太复杂不会考的!)#include<iostream>using namespace std;int main() int N,M; cout<<"请输入N,M"<<endl; cin>>N>>M; int *a=new intN; for(int i=0;i<N;i+) ai=i+1; int countN=0; int countM=0; for(i=0;i+) if(ai%N!=-1) countM+; if(countM=M) cout<<ai%N<<"t" ai%N=-1; countN+; countM=0; if(countN=N) break; delete a; return 0; 字符串编程:1、 (已验证!)输入一个字符串,反向输出其字符,如输入是"abcdef",则反向输出"fedcba".(1)、利用string流:#include<iostream>#include <string>using namespace std;string rstring(string s)if(s.length()=1)return s;elsechar c=ss.length()-1;return c+rstring(s.substr(0,s.length()-1);void main()string str;cout<<"输入一个字符串:"<<endl;cin>>str;cout<<str<<"的反向字符串是"<<rstring(str)<<endl;(2)、数组倒序输出(很简单):#include<iostream>using namespace std;void main()char str180;int l;cout<<"输入一个字符串:"<<endl;cin>>str;l=strlen(str);cout<<str<<"的反向字符串是:"for (int i=l-1;i>=0;i-)cout<<stri;cout<<endl;2、(已验证!)将输入字符串中所有的'C'去掉,输出其他字符。#include<iostream>using namespace std;void main()char str180;int l;cout<<"输入一个字符串:"<<endl;cin>>str;l=strlen(str);cout<<"过滤后为:"for (int i=0;i<l;i+)if (stri!='c' && stri!='C')cout<<stri;cout<<endl; 3、(已验证!)输入字符串,判断此字符串是否为回文,如"abcd",正数为"abcd",反数为"dcba",不是回文,而"aba",正数是"aba",反数也是"aba",是回文。#include <iostream>using namespace std;void main( ) char str180,l; int i,t=0; cout<<"输入一个字符串:" cin>>str; l=strlen(str); for(i=0;i<l/2;i+) if(stri=strl-i-1) t=1; break; if(t=0) cout<<str<<"不是回文!"<<endl; else cout<<str<<"是回文!"<<endl;专心-专注-专业