概念



示例


习题
题目1

题目2

答案1

答案2

课后习题
题目0

题目1-5


答案0
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main() {
// 初始化随机数种子
srand(time(0));
string a; // 是否来自高风险地区
string b; // 核酸检测结果
cout << "是否来自疫情高风险地区(是/否): ";
cin >> a;
if (a == "是") {
cout << "请进行核酸检测" << endl;
// 随机生成核酸结果(0为阴,1为阳)
int r = rand() % 2;
if (r == 0) {
b = "阴";
} else {
b = "阳";
}
cout << "核酸结果为(阳/阴): " << b << endl;
if (b == "阴") {
cout << "可以通行" << endl;
} else {
cout << "请立即隔离" << endl;
}
} else {
cout << "可以通行" << endl;
}
return 0;
}
答案1-5
1.
#include <iostream>
using namespace std;
int main() {
int age;
char gender;
cout << "请输入年龄和性别(M/F): ";
cin >> age >> gender;
if(age <= 12) {
cout << "儿童";
}
else if(age <= 19) {
cout << "青少年";
}
else if(age <= 59) {
cout << "成人";
if(gender == 'M') {
cout << "先生";
}
else {
cout << "女士";
}
}
else {
cout << "老年";
}
return 0;
}
2.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入三角形三边长: ";
cin >> a >> b >> c;
if(a + b > c && a + c > b && b + c > a) {
if(a == b && b == c) {
cout << "等边三角形";
}
else if(a == b || b == c || a == c) {
cout << "等腰三角形";
}
else if(a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a) {
cout << "直角三角形";
}
else {
cout << "普通三角形";
}
}
else {
cout << "不能构成三角形";
}
return 0;
}
3.
#include <iostream>
using namespace std;
int main() {
int score;
char extraWork;
cout << "请输入成绩和是否完成额外作业(Y/N): ";
cin >> score >> extraWork;
if(score >= 90) {
cout << "A级";
if(extraWork == 'Y') {
cout << ",获得奖励";
}
else {
cout << ",未完成额外作业";
}
}
else if(score >= 80) {
cout << "B级";
}
else if(score >= 70) {
cout << "C级";
}
else {
cout << "D级";
}
return 0;
}
4.
#include <iostream>
using namespace std;
int main() {
float price;
int level;
cout << "请输入商品价格和会员等级(1-3): ";
cin >> price >> level;
if(price >= 1000) {
price -= 200;
cout << "已减200元,";
if(level == 1) {
price *= 0.9;
cout << "白金会员再享9折";
}
else if(level == 2) {
price *= 0.95;
cout << "黄金会员再享95折";
}
else {
cout << "普通会员无额外折扣";
}
}
else if(price >= 500) {
price -= 100;
cout << "已减100元";
}
else {
cout << "未达到满减条件";
}
cout << "\n最终价格: " << price;
return 0;
}
5.
#include <iostream>
using namespace std;
int main() {
int year, month;
cout << "请输入年份和月份: ";
cin >> year >> month;
bool isLeap = false;
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
isLeap = true;
}
if(month == 2) {
if(isLeap) {
cout << "29天(闰年)";
}
else {
cout << "28天";
}
}
else if(month == 4 || month == 6 || month == 9 || month == 11) {
cout << "30天";
}
else {
cout << "31天";
}
return 0;
}
综合题
题目1-2

题目3

答案1-2
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int num = rand() % 100 + 1; // 神秘数字
int input; // 用户输入
int count = 0; // 猜测计数
bool win = false; // 胜利标志
cout << "猜数字游戏(1-100),限5次:" << endl;
while(count < 5 && !win) {
cout << "第" << count+1 << "次猜: ";
cin >> input;
count++;
if(input == num) {
win = true;
cout << "太棒了!第" << count << "次猜中!" << endl;
}
else if(input < num) {
cout << "猜小了" << endl;
}
else {
cout << "猜大了" << endl;
}
}
if(!win) {
cout << "游戏结束,数字是:" << num << endl;
}
return 0;
}
2.
#include <iostream>
using namespace std;
int main() {
char light;
int seconds;
cout << "请输入当前信号灯颜色(R/G/Y)和黄灯剩余秒数(0-10): ";
cin >> light >> seconds;
if(light == 'R' || light == 'r') {
cout << "红灯停!必须停车等待" << endl;
}
else if(light == 'G' || light == 'g') {
cout << "绿灯行!可以安全通过" << endl;
}
else if(light == 'Y' || light == 'y') {
if(seconds >= 3) {
cout << "黄灯亮,还有" << seconds << "秒,请谨慎通过" << endl;
}
else {
cout << "黄灯即将变红,只剩" << seconds << "秒,请停车等待" << endl;
}
}
else {
cout << "错误:无效的信号灯颜色!" << endl;
return 1;
}
return 0;
}
答案3
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // 随机种子
// 敌机属性
int ep = rand() % 3 + 1; // 位置:1左 2前 3右
int eh = rand() % 3 + 1; // 血量
bool es = rand() % 2; // 护盾
// 显示信息
cout << "敌机在" << (ep == 1 ? "左" : ep == 2 ? "前" : "右")
<< ",血:" << eh << (es ? ",有盾" : "") << endl;
// 玩家攻击
cout << "攻击(W前 A左 D右):";
char cmd;
cin >> cmd;
// 攻击位置
int atk = (cmd == 'A') ? 1 : (cmd == 'W') ? 2 : 3;
// 战斗判断
if(atk == ep) { // 命中
if(es) {
cout << "破盾!" << endl;
es = false;
}
else {
eh--;
cout << "命中!血:" << eh << endl;
// 暴击
if(eh == 1 && rand() % 2) {
cout << "暴击!击落" << endl;
eh = 0;
}
}
}
else {
cout << "未命中!" << endl;
}
// 结果
if(eh <= 0) {
cout << "胜利!" << endl;
}
else if(eh == 1 && !es) {
cout << "敌机残血" << endl;
}
else {
cout << "继续战斗" << endl;
}
return 0;
}

被折叠的 条评论
为什么被折叠?



