C++编程练习题 (1-10)
1. 输入3个数,求最大值
2. 编程序,求方程ax2+bx+c=0的根
3. 输入一个成绩,打印相应的等级
4. 输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边
5. 输入20个数,求其最大、最小和平均值
6. 输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值
7. 输入若干个数,输入-999表示结束,求平均值及最大值
8. 求和 s=1X1 + 2X2 + 3X3 +...+ 100X100
9. 印度国王的奖励,求和 2的0次方加到2的63次方
10. 求和 s=1! + 2! + 3! +...+ 10!
小白记录学习C++的过程~
1. 输入3个数,求最大值
#include<iostream>using namespace std;int main(){ int a, b, c, m; cout << "请输入三个数,求出最大值" << endl; cin >> a >> b >> c; //首先定义一个中间变量m,并将a赋值给m m = a; if (b > m) //若b大于被赋值的m(就是a),就将b(目前最大)再次赋值给m { m = b; } if (c > m) //若c大于被赋值的m(就是b),就将c(目前最大)再次赋值给m,就得到了最大值 { m = c; } cout << "最大值为:" << m << endl; system("pause"); return 0;}
2. 编程序,求方程ax2+bx+c=0的根
#include <iostream>using namespace std;#include<cmath> //使用了根号,必须调一下函数库//二次项系数等于0就为1次方程,不为0才为二次方程//达尔塔 有三种情况,分别是大于0,小于0,等于0,需要分开讨论int main(){double a, b, c, x1, x2;cout << "请输入二元一次方程组二次项系数a" << endl;cin >> a;cout << "请输入二元一次方程组一次项系数b" << endl;cin >> b;cout << "请输入二元一次方程组常数项系数c" << endl;cin >> c;cout << "您输入的方程组为:" << a << "x^2+" << b << "x+" << c << "=0" << endl;double d = b * b - 4 * a * c;cout << "d=" << d << endl;if (a == 0 & b == 0){cout << "方程无解" << endl;}if (a == 0) //变成一次方程,解法不同{cout << "方程的解为:" << -c / b << endl;}if (d == 0) //a不为0才是二次方程{cout << "该方程有一个解" << endl;x1 = (-b) / (2 * a);cout << "该解为" << x1 << endl;}if (d < 0){cout << "该方程组无解" << endl;}if (d > 0){cout << "该方程有两个解" << endl;x1 = (-b + sqrt(d)) / (2 * a);x2 = (-b - sqrt(d)) / (2 * a);cout << "x1=" << x1 << endl;cout << "x2=" << x2 << endl;}system("pause");return 0;}
3. 输入一个成绩,打印相应的等级
#include <iostream>using namespace std;int main(){double score;cout << "请输入您的成绩" << endl;cin >> score;cout << "您输入的成绩为" << score << endl;if (score < 60){cout << "不合格" << endl;}if (60 < score & score < 80){cout << "良好" << endl;}if (score > 80 & score<=100){cout << "优秀" << endl;}if (score > 100){cout << "输入错误,请重新输入!" << endl;}system("pause");return 0;}
4. 输入3个double类型的值,判断这3个值是否可以表示一个三角形的三条边
#include<iostream>using namespace std;int main(){double a, b, c;cout << "请输入三角形的三条边" << endl;cin >> a >> b >> c;if (a+b>c || a+c>b || b+c>a){cout << "可以构成三角形" << endl;}else {cout << "不可以构成三角形" << endl;}system("pause");return 0;}
5. 输入20个数,求其最大、最小和平均值
#include<iostream>using namespace std;int main(){double a, minnumber, maxnumber,total;cout << "请输入20个数,按回车键结束" << endl;cin >> a;//将输入的a值赋给下一行作为初始值total = maxnumber = minnumber = a;//初始化的值放在循环外,也是将这三个数初始化for (int i = 1; i < 20; i++){cin >> a;if (maxnumber < a){maxnumber = a;}if (minnumber > a){minnumber = a;}total = total + a;}cout << "最大数为:" << maxnumber << endl;cout << "最小数为:" << minnumber << endl;cout << "平均数为:" << total/20 << endl;system("pause");return 0;}
6. 输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值
#include<iostream>using namespace std;int main(){int a,max_number,total,number; cout << "请输入要计算数字的个数" << endl;cin >> number;cin >> a;total = max_number = a;//一定要初始化,不然后面没法比较for (int i = 1; i < number; i++){cin >> a;if ( a > max_number){max_number = a;}total = total + a;}cout << "最大值为:" << max_number << endl; cout << "平均值为:" << total / number << endl;system("pause");return 0;}
7. 输入若干个数,输入-999表示结束,求平均值及最大值
#include <iostream>using namespace std;int main(){int a, max_number, total;int count = 0;//计数器设置为0cout << "输入若干个数,输入-999代表结束" << endl;cin >> a;max_number = a;total = 0;while (a != -999)//当输入的值为-999时,表示结束,不会计算-999。if满足一次条件就会结束{if (max_number < a){max_number = a;}total = total + a;cout << "total=" << total << endl;count = count + 1;cin >> a;}double n = total / count;cout << "最大值为:" << max_number << endl;cout << "平均值为:" << n << endl;system("pause");return 0;}
8. 求和 s=1X1 + 2X2 + 3X3 +…+ 100X100
#include <iostream>using namespace std;int main(){int total=0;int temp;for (int i = 1; i < 101; i++){temp = i * i;total += temp;}cout << "total=" << total << endl;system("pause");return 0;}
9. 印度国王的奖励,求和 2的0次方加到2的63次方
#include <iostream>#include<cmath>using namespace std;int main(){double temp;//一定要定义成double类型,不然数字太大超过范围double total=0;for (int i = 0; i < 64; i++){temp = pow(2,i);//表示2的i次方total = total+temp;}cout << "和为:" << total << endl;system("pause");return 0;}
10. 求和 s=1! + 2! + 3! +…+ 10!
#include<iostream>using namespace std;int main(){int temp=1;int total = 0;for (int i = 1; i <= 10; i++){temp = temp * i;total = total + temp;}cout << "和为:" << total << endl;system("pause");return 0;}