【C/C++基础练习题】简单语法使用练习题
🍉内容专栏:【C/C++要打好基础啊】
🍉本文内容:简单语法使用练习题(复习之前写过的实验报告)
🍉本文作者:Melon西西
🍉发布时间 :2023.2.10
目录
1、输入三个数判断能不能构成三角形,判断是什么三角形:
2.输入存银行的本金和存款年限(要求只能是1,2,3,5年,其余的非法),利用switch语句求到期时存款本金和利息之和。
3.百分制成绩转换为五分制成绩并显示在屏幕上。成绩转换规则如下:A对应90~100分,B对应80~89分,C对应70~79分,D对应60~69分,E对应60分以下。请用if-else语句和switch语句两种方法求解。
4.找素数一类
5.若员工月工作小时超过40小时,则超过部分按原工资的1.5倍的加班工资来计算。若员工月工作小时超过50小时,则超过部分按原工资的3 倍的加班工资来计算。
6.求解1000以内可以同时被9和11整除的所有整数,要求一行输出5个数据。
7.设计程序,用于统计三位数中,三个数字之和为奇数的数有多少个,并按八个数一行显示这些数,注意对齐。例如:124的三个数字之和是7
1、输入三个数判断能不能构成三角形,判断是什么三角形:
{double a = 0, b = 0, c = 0;cin >> a >> b >> c;if (a + b - c > 0 && a + c - b > 0 && b + c - a > 0){if (a == b && b == c){cout << "可以构成等边三角形";}else if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + b * b == c * c){cout << "可以构成直角三角形";}else{cout << "可以构成一般三角形";}}else{cout << "不能构成三角形" << endl;}
}
2.输入存银行的本金和存款年限(要求只能是1,2,3,5年,其余的非法),利用switch语句求到期时存款本金和利息之和。
double money = 0; int time = 0;cout << "请输入本金和存款年限,存款年限只能为1,2,3,5:" << endl;cin >> money >> time;double sum, bonus = 0;switch (time){case 1:{bonus = money * 0.0255 * time;sum = bonus + money;}case 2:{bonus = money * 0.0279 * time;sum = bonus + money;}case 3:{bonus = money * 0.0333 * time;sum = bonus + money;}case 5:{bonus = money * 0.0360 * time;sum = bonus + money;}cout << "总金是" << sum << endl;break;default:{cout << "数据非法" << endl;}}
3.百分制成绩转换为五分制成绩并显示在屏幕上。成绩转换规则如下:A对应90~100分,B对应80~89分,C对应70~79分,D对应60~69分,E对应60分以下。请用if-else语句和switch语句两种方法求解。
int fenshu = 0;cout << "输入一个分数判断等级:";cin >> fenshu;if (fenshu > 100){ cout << "请输入正确分数" << endl;}else if (fenshu > 90){cout << "A" << endl;}else if (fenshu >80 ){cout << "B" << endl;}else if (fenshu >70 ){cout << "C" << endl;}else if (fenshu >= 60){cout << "D" << endl;}else if (fenshu <60 ){cout << "E" << endl;}
#include<iostream>
using namespace std;
int main()
{int fenshu = 0;cout << "输入一个分数判断等级:";cin >> fenshu;int a;a = fenshu / 10;switch (a){case 10:{cout << "A" << endl; break;}case 9:{cout << "A" << endl; break;}case 8:{cout << "B" << endl; break;}case 7:{cout << "C" << endl; break;}case 6:{cout << "D" << endl; break;}default:{cout << "E" << endl; break;}}system("pause");return 0;
}
4.找素数一类
int main()
{int x=0;cout << "输入一个数判断是不是质数 ";cin >> x;int t = 1;for (int i = 2;i < x;i++){if (x % i == 0){t = 0;}}if (t == 1){cout << "是质数" << endl;}else cout << "不是质数" << endl;return 0;
}
int main()
{int a = 0, b = 0, big = 0, small = 0;;cout << "输入要查找质数的范围 ";cin >> a >> b;big = (a > b) ? a : b;small = a + b - big;int t = 1;int count = 0;for (int j = small; j < big; j++){for (int i = 2; i < j; i++){if (j % i == 0){t = 0;}}if (t == 1){cout << j << " ";count++;if (count % 5 == 0)cout << "\n";}t = 1;}return 0;
}
int chazhao()
{int a = 0, b = 0;cout << "输入要查找质数的范围a>10,b<1000 :";cin >> a >> b;int t = 1;int count = 0;if (a >= 10 && b <= 1000 && a < b){for (int j = a; j < b; j++){for (int i = 2; i < j; i++){if (j % i == 0){t = 0;}}if (t == 1){cout << j << " ";count++;if (count % 5 == 0)cout << "\n";}t = 1;}}else{cout << "请重新输入a,b" << endl;chazhao();}return 0;
}
int main()
{chazhao();return 0;
}
5.若员工月工作小时超过40小时,则超过部分按原工资的1.5倍的加班工资来计算。若员工月工作小时超过50小时,则超过部分按原工资的3 倍的加班工资来计算。
#include<iostream>
using namespace std;
int main()
{double time = 0, wage = 0, sum = 0;cout << "Please input employee’s work time and wage_per_hour:";cin >> time >> wage;if (time != 10 && wage != 0){if (time <= 40){sum = time * wage;}else if (time > 40 && time <= 50){double time2 = 0;time2 = time - 40;sum = 40 * wage + time2 * wage * 1.5;}else if (time > 50){double time3 = 0;time3 = time - 50;sum = 40 * wage + 10 * 1.5 * wage + wage * 3 * time3;}cout << "The employee’s wage=" << sum << endl;}else{cout << "程序结束" << endl;}return 0;
}
6.求解1000以内可以同时被9和11整除的所有整数,要求一行输出5个数据。
#include<iostream>
using namespace std;
int main()
{int count = 0;for (int i = 1; i < 1000; i++){if (i % 9 == 0 && i % 11 == 0){cout << i << " ";count++;if (count % 5 == 0){cout << "\n";}}}return 0;
}
7.设计程序,用于统计三位数中,三个数字之和为奇数的数有多少个,并按八个数一行显示这些数,注意对齐。例如:124的三个数字之和是7
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{int g = 0, s = 0, b = 0,a=0;int count = 0;for (int i = 100;i < 999;i++){g = i % 10;s = i / 10%10;b = i/100;a = g + s + b;if (a % 2 != 0){cout << i << "的三个数字之和是"<<setw(2)<<a<<" ";count++;if (count % 8 == 0){cout << "\n";}}}return 0;
}