C++经典编程例题(1)
1、(已验证!)计算铁路运费。已知从甲地到乙地,每张票托运行李不超过50公斤时,按每公斤0.13元,超过50公斤,超过部分按每公斤0.2元计算。输入行李重量w,计算运费y.
#include
using namespace std;
void main()
{
float money=0,weight;
cout<<"请输入货物重量(单位:千克):";
cin>>weight;
if (weight<0)
cout<<”数据错误!”< if (weight <= 50) money = weight * 0.13; else if (weight > 50) money = 50 * 0.13 + (weight - 50) * 0.2; cout<<"\n总费用是:"< } 2、(已验证!)根据一元二次方程a*x*x+b*x+c=0求解方程。输入a、b、c的值,求x1、x2的实根. #include #include using namespace std; void main() { double a,b,c,p,x1,x2,D; cout<<"请输入3个数:"< cout<<"a="; cin>>a; cout<<"b="; cin>>b; cout<<"c="; cin>>c; cout<<"方程为:"< D=b*b-4*a*c; if (D<0) cout<<"方程无解!"< if (D==0) { p=-b/(2*a); x1=p+sqrt(D)/(2*a); cout<<"方程有两个相同的实根。"< cout<<"x1=x2="< } if (D>0) { p=-b/(2*a); x1=p+sqrt(D)/(2*a); x2=p-sqrt(D)/(2*a); cout<<"方程有两个不同的实根。"< cout<<"x1="< } } 3、(已验证!)分别使用for和while求n!.注解n!=1*2*3*...*n,n由键盘输入。For: #include 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< } While: #include 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< } 4、(已验证!)百钱买百鸡。1只公鸡5元,1只母鸡3元,3只小鸡1元,现有100元钱要买100只鸡,有什么解法。分别使用三重循环和二重循环来解. 鸡翁,鸡婆,鸡雏各:12、4、84只 鸡翁,鸡婆,鸡雏各:8、11、81只 鸡翁,鸡婆,鸡雏各:4、18、78只 鸡翁,鸡婆,鸡雏各:0、25、75只 (1).三重循环: #include 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<<"鸡翁,鸡婆,鸡雏各:"< } } } } (2).两重循环: #include 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<<"鸡翁,鸡婆,鸡雏各:"< } } 5、(已验证!)求所有水仙花数。所谓水仙花数是指三位数,其各位数立方和等于该数。如153=1*1*1+5*5*5+3*3*3. (153 370 371 407) #include 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< } } cout< } 6、(已验证!)解数学灯迷。有以下算式 ABCD-CDC=ABC,其中ABCD均为一位非负整数,求ABCD的值。(1098) #include 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< } } cout< } 7、(已验证!)从键盘输入一个整数,判断此数是否为回文数。所谓回文数就是从左到右读与从右到左读是一样的数。如12321、7887等。 #include 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< else cout< } 8、(已验证!)使用函数计算y=2*x+3,输入x,求y的值。 #include using namespace std; float y(int x) { return 2*x+3; } void main() { int x; cout<<"输入X:"; cin>>x; cout<<"Y="< } 9、(已验证!)使用递归函数,求99+97+95+...+3+1之和。(和是2500。)#include 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<<"输入范围:(n~m):"< cout<<"n="; cin>>n; cout<<"m="; cin>>m; cout<<"从"< 10、(已验证!)已知杨辉三角 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 .输出前13行的值. #include using namespace std; void main() { int a[11],i,j; a[1]=1; for(i=1;i<=13;i++) { a[i]=1; for(j=i-1;j>=2;j--) a[j]=a[j]+a[j-1]; for(j=1;j<=i;j++)