C++经典编程练习题_第1页
C++经典编程练习题_第2页
C++经典编程练习题_第3页
C++经典编程练习题_第4页
C++经典编程练习题_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、50公斤时,按每公 w,计算运费y.c的值,求x1、x2C+经典编程例题(1)1、(已验证!)计算铁路运费。已知从甲地到乙地,每张票托运行李不超过斤0.13元,超过50公斤,超过部分按每公斤0.2元计算。输入行李重量#in elude <iostream>using n amespace std;void mai n()float mon ey=0,weight;cout<<"请输入货物重量(单位:千克):"cin> >weight;if (weight<0)cout<< 数据错误! ” <<endl;if

2、(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、 的实根.#in clude <iostream>#in clude <cmath>using n amespace std;void mai n()double a,

3、b,c,p,x1,x2,D;cout<<"请输入 3 个数:"<<endl;cout<<"a="cin> >a;cout<<"b="cin> >b;cout<<"c="cin> >c;cout<<"方程为:"<<a<<"xA2+"<<b<<"x+"<<c<<"=0&qu

4、ot;<<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<<"x 1= x2="<<x1<<e ndl;if (D>0)p=-b/(2*a);x1= p+sqrt(D)/(2*a);x2=p-sqrt(D)/(2*a);cout<<&qu

5、ot;方程有两个不同的实根。"<<endl;cout<<"x 1="<<x1<<e ndl<<"x2="<<x2<<e ndl;3、(已验证!)分别使用for和while求n!.注解n!=1*2*3*.*n,n由键盘输入。For:#in clude <iostream>using n amespace std;void mai n()int i,n,s;s=1;cout<<"i nput n:"cin»n;f

6、or (i=1;i<=n ;i+)s=i*s;cout<<s<<e ndl;While: #in elude <iostream>using n amespace std;void mai n()int i,n,s;s=1;i=1;cout<<"i nput n:"cin»n;while(i<=n)s=i*s;+i;cout<<s<<e ndl;4、(已验证!)百钱买百鸡。1只公鸡5元,1只母鸡3元,3只小鸡1元,现有100元钱要买 100只鸡,有什么解法。分别使用三重循环和二重循

7、环来解鸡翁,鸡婆,鸡雏各:12、4、84只鸡翁,鸡婆,鸡雏各:& 11、81只鸡翁,鸡婆,鸡雏各:4、18、78只鸡翁,鸡婆,鸡雏各:0、25、75只(1).三重循环:#in clude <iostream>using n amespace std;void mai n()const int cock=20,he n=33,chicke n=100;int c,h,ck;for(c=0;c<=cock;c+)for(h=0;h<=he n;h+)for(ck=0;ck<=chicke n; ck+)if(c+h+ck)=100)&&(c*5

8、+h*3+ck/3)=100) &&(ck%3)=0) cout<<"鸡翁,鸡婆,鸡雏各:"<<c<<"、"<<h<<"、"<<ck<<"只"<<endl;(2) 两重循环:#in clude<iostream>using n amespace std;void mai n()int c,h,ck;for(h=0;h<33;h+)for(ck=0;ck<100;ck+) c=100

9、-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)#in clude <

10、iostream>using n amespace std;void mai n()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<<e ndl;6、(已验证!)解数学灯迷。有以下算式 ABCD-CDC=ABC ,其中ABCD均为一位非负整数, 求 ABCD 的值。(1098)#in elude <iostream>using n a

11、mespace std;void mai n()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<<e ndl;7、(已验证!)从键盘输入一个整数,判断此数是否为回文数。所谓回文数就是从左到右读与从右到左读是一样的数。如12321、7887等。#in clude <iostream>using n amespace std;bool ifn(i nt x)int

12、m=0, n;n=x;while( n)m=m*10+n%10;if(m=x)return true;if(m!=x)return false;void mai n()int a;cout<<"输入一个数:"cin> >a;if(ifn(a)cout<<a<<"是回文数!"<<endl;elsecout<<a<<"不是回文数!"<<endl;8、(已验证!)使用函数计算y=2*x+3,输入x,求y的值。#in clude <iostr

13、eam>using n amespace std;float y(i nt x)return 2*x+3;void mai n()int x;cout<<"输入 X :"cin> >x;cout<<"Y="<<y(x)<<e ndl;9、 (已验证!)使用递归函数,求99+97+95+.+3+1之和。(和是2500。)#in clude <iostream>using n amespace std;int sum(l ong n ,lo ng m)if(n=m)return(m

14、);elsereturn(m+sum( n, m-2);void mai n()int n,m;cout<<"输入范围:(nm):"<<endl;cout<<" n="cin»n;cout<<"m="cin»m;cout<<"从"<<n<<"到"<<m<<"的和为:"<<sum(n,m)<<endl;10、(已验证!)已知杨辉

15、三角11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 11 6 15 20 15 6 1输出前13行的值.#in clude <iostream>using n amespace std;void mai n()int a11,i,j;a1=1;for(i=1;i<=13;i+)for(j=i-1;j>=2;j-)aj=aj+aj-1;for(j=1;j<=i;j+)cout<<aj<<""cout<<"n"11、(已验证!)约瑟夫问题:n个小孩围成一圈做游戏,给定

16、一个数m,现从s个小孩开始,顺时计数,每数到 m,该小孩出列,然后从下个小孩重新数数,数到m时,该小孩出列,如果反复,直到所以小孩出列。(太复杂不会考的!)#in clude<iostream>using n amespace std;int mai n()int N,M;cout<<"请输入 N,M"<<endl;cin>> N»M;int *a=new in tN;for(int i=0;i<N;i+)ai=i+1;int cou ntN=0;in t cou ntM=0;for(i=0;i+)if(ai%

17、N!=-1)cou ntM+;if(cou ntM=M)cout<<ai%N<<"t"ai%N=-1;cou ntN+;cou ntM=0;if(cou ntN=N)break;delete a;return 0;字符串编程:"fedcba".1、(已验证!)输入一个字符串,反向输岀其字符,如输入是"abcdef",则反向输岀(1) 、利用 string 流:#in clude<iostream>#in clude <stri ng>using n amespace std;string

18、 rstring(string s)if(s.len gth()=1)return s;elsechar c=ss .len gth()-1;return c+rstri ng(s.substr(O,s .len gth()-1);void mai n()string str;cout«"输入一个字符串:"<<endl;cin> >str;coutvvstrvv"的反向字符串是"rstring(str)<<endl;(2) 、数组倒序输岀(很简单):#in clude<iostream>using

19、 n amespace std;void mai n()char str180;int l;cout«"输入一个字符串:"<<endl;cin> >str;l=strle n(str);coutvvstrvv"的反向字符串是:";for (int i=l-1;i>=0;i-)cout<<stri;cout«e ndl;2、 (已验证!)将输入字符串中所有的C去掉,输出其他字符。#in clude<iostream>using n amespace std;void mai n()char str180;int l;cout&#

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论