C++作业。
//1.1 输入两个数A,B,算出结果
#include<iostream.h> int main() { int a, b; cout<<"Please enter two numbers:\n"; cin>>a>>b; cout<<a<<"+"<<b<<"="<<a+b<<endl; return 0; }
// 1.2 直角三角形已知两直角边,求斜边长 #include<iostream.h> #include<math.h> int main() { double a,b; cout<<"请输入两直角边的长: \n"; cin >>a>>b; cout.precision(4); cout<<"斜边长为:"<<sqrt(a*a+b*b) <<endl; return 0; }
// 1.3时间转换(把秒转换为标准时间) // 例:3600 s 转化为 01:00:00
//(1)李韡
#include<iostream.h> int main() { int sum, hour, min, second; cout<<"Please enter a data:"<<endl; cin>>sum; if(sum<0) cout<<"error."; else { hour = sum/3600; min = (sum-hour*3600)/60; second = (sum-hour*3600-min*60); } output(hour); output(min); output(second); cout<<"\b \n"; return 0; } void output(int a) { if(a<10) cout<<"0"<<a<<":"; else cout<<a<<":"; } //(2)査俊毅 #include<iostream.h> int main() { int time; int H,M,S; there: cout<<"请输入秒数:\n"; cin>>time; if(time<0||time>86400) { cout<<"输入有误!\a\n"; goto there; } else { H=time/3600; M=(time%3600)/60; S=(time%3600)%60; cout<<"现在的时间是:\n"<<H<<"时"<<M<<"分"<<S<<"秒"<<endl; } return 0; }
// 1.4求三个实数的和并将其输出,再输出将其四舍五入之后的值 // 翁世杰 #include<iostream.h> int main(void) { float x=0, y=0, z=0; float sum=0, temp; cout<<"Please enter three real number:"<<endl; cin>>x>>y>>z; sum = x + y + z; cout<<"sum="<<sum; cout<<"\nAfter rounding the result:\n"; temp=sum-int(sum); if(temp>=0.5) cout<<"sum="<<int(sum)+1<<endl; else cout<<"sum="<<int(sum)<<endl; return 0; }
// 1.5.1 成绩转换成等级(if else)
//(输入一个100分制的成绩,将其转换为相对应的等级,90以上A,80以上B,70以上C,60以上D,60一下E) // 李韡 #include<iostream.h> int main() { int score; cout<<"Please enter your score: "; while(cin>>score) { if(score > 100 || scor e< 0) cout<<"Wrong Data.\n"; else { if(score>=90) cout<<"Grade: A"; else if(score>=80) cout<<"Grade: B"; else if(score>=70) cout<<"Grade: C"; else if(score>=60) cout<<"Grade: D"; else cout<<"Grade: E"; cout<<endl; } } return 0; }
// 1.5.2 成绩转换成等级(switch) //(输入一个100分制的成绩,将其转换为相对应的等级,90以上A,80以上B,70以上C,60以上D,60一下E) // 李韡 #include<iostream.h> int main() { int score; cout<<"Please enter your score: "; while( cin>>score ) { if( score>100 || score<0 ) cout<<"Wrong Data.\n"; else { score/=10; switch(score) { case 10: case 9: cout<<"Grade: A\n"; break; case 8: cout<<"Grade: B\n"; break; case 7: cout<<"Grade: C\n"; break; case 6: cout<<"Grade: D\n"; break; default: cout<<"Grade: E\n"; break; } } } return 0; }
//1.6 求1000以内的所有完数
//翁世杰
#include<iostream.h> #define N 1000 int main() { int i, j, sum; for(i=1; i<=N; i++) { for(j=1,sum=0;j<=i/2;j++)//一个数的因子至少是其二分之一 { if(i%j==0) sum+=j; } if(sum==i) cout<<i<<endl; } return 0; }
//1.7 解二元一次方程组 //张世昊 #include<iostream.h> #include<math.h> void main() { int A,B,C; int a,b; cout<<"请输入一元二次方程的三个系数"<<endl; double X,Y_1,Y_2; cin>>A>>B>>C; X=B*B-4*A*C; if(A==0){ cout<<"输入值不满足一元二次方程"<<endl;} else { if(X<0) cout<<"此方程无解"<<endl; else { a=2*A; b=-B; Y_1=(b-sqrt(X))/a; Y_2=(b+sqrt(X))/a; if(Y_1==Y_2) cout<<"此方程的两个解相等为:"<<Y_1<<endl; else cout<<"次方程有两个不相等的解分别为:"<<Y_1<<" "<<Y_2<<endl; } } }
// 1.8 打印九九乘法表 // 李韡 #include<iostream.h> #include <iomanip.h> int main() { int i,j; cout<<"*"; for(i=1;i<10;++i) cout<<setw(4)<<i; cout<<"\n-------------------------------------\n"; for(i=1;i<10;++i) { cout<<"\n"<<i; for(j=1;j<10;++j ) cout<<setw(4)<<j*i; } cout<<endl; return 0; }
// 1.9 求两个数的最大公约数 // 李韡 #include<iostream.h> int main() { int a, b, c, t; cout<<"Please enter two number:"<<endl; cin>>a>>b; if(a<b) { t=a; a=b; b=t; } c=a%b; while(c!=0) { a=b;b=c;c=a%b; } cout<<"The gcd of the two numbers is: "<<b<<endl; return 0; }
// 1.10 用*输出三角形 // 李韡 #include<iostream.h> int main() { int i, j, line; cout<<"请输入要打印出的三角形的行数:"<<endl; cin>>line; for(i=1; i<=line; i++) //控制总的行数 { for(j=i; j<line; j++) //控制空格数 { cout<<" "; } for(j=1; j<=i; j++) //控制*的个数 cout<<"* "; cout<<endl; } return 0; }
//1.11 输出平行四边形 // 张世昊 #include<iostream.h> int main() { int i,j,k,t; int a,b; cout<<"输入平行四边形的高度a="; cin>>a; cout<<"输入平行四边形的长度b="; cin>>b; t=a+1; for(i=1;i<=a;i++) { for(k=t;k>=1;k--) cout<<" "; t--; for(j=1;j<=b;j++) cout<<"+ "; cout<<"\n"; } }
//1.12 输出ABA菱形 // 白磊 #include<iostream.h> void main() { int i,j,k; cout<<"请输入2~20的一个数:\n"; cin>>k; for(j=1;j<=k+1;j++)//输出第一个A { if(j==k+1) break; cout<<" "; } cout<<"A \n"; for(i=1;i<=k;i++) { for(j=1;j<=k-i;j++)//控制输出空格 cout<<" "; cout<<"A "; for(j=1;j<=i-1;j++) cout<<"B "; cout<<"A"<<"\n"; } for(i=1;i<=k-1;i++) { for(j=1;j<=i;j++) cout<<" "; cout<<"A "; for(j=1;j<=k-i-1;j++) cout<<"B "; cout<<"A"; cout<<"\n"; } for(i=1;i<=k;i++)//输出最后一个A cout<<" "; cout<<"A"<<endl; }
//1.13 杨辉三角 // 李韡 /* 用二维数组来做, 规律是每一列的第一项和最后一项都是1, 也就对应我们的二维数组(a[i][j])中的i=j或者j=0的时候的情况。 中间的数字, a[i][j] = a[i-1][j-1] +a[i-1][j] */ /* 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 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 Press any key to continue */ /* 改进,改为等腰三角形的杨辉三角,, 注意了空格的输出 */ #include<iostream.h> #include<iomanip.h> int main() { int a[10][10]={0,0}; int b[10][10]={0,0}; int i, j, k, t; for (i=0;i<10;i++) { for(k=i;k<18-i;k++) cout<<" "; for (j=i;j>=0;j--) { if (i==j || j==0) a[i][j]=1; else a[i][j] = a[i-1][j-1]+a[i-1][j]; } for (j=i;j>=0;j--) cout<<setw(4)<<a[i][j]; //域宽 cout<<endl; } return 0; }
// 1.13 杨辉三角 // 查俊毅 #include<iostream.h> #include<iomanip.h> int zjy(int m) { int i,k; k=1; for(i=1;i<=m;i++) k=i*k; return k; } int main() { int a,b,n; cout<<"请输入杨辉三角的行数:"; cin>>n; for(a=0;a<=n-1;a++) { for(b=1;b<=n-a-1;b++) cout<<" "; for(b=0;b<=a;b++) cout<<setw(8)<<zjy(a)/zjy(b)/zjy(a-b); cout<<endl; } return 0; }