详解C++模拟算法,蓝桥杯真题(蓝桥杯c++编译环境)
什么是模拟算法?
模拟算法:根据题目给出的规则对题目要求的相关过程进行编程模拟。
解题步骤:
1. 仔细读题,记录关键信息,反复核对。
2. 分析有哪些关键要素,将关键要素抽象为程序变量与结构。
3. 逐步细化实现题目中描述的过程。
4. 测试输入样例,如果有必要还需自己构造更多样例。
水下探测器
解析:以水面作为基准线,深度为0米,水下最大深度为h米。初始深度为s米。 根据上、下指令,控制s自增1或自减1,前提是不能超出深度的合法范围。
#include <iostream>
#include <string>
using namespace std;
int h, s;
string str;
int main() {
cin >> h >> s >> str;
for(char x : str){
if(x == 'u' && s > 0) s--;
else if(x == 'd' && s < h) s++;
}
cout << s << endl;
return 0;
}
加密
解析: 由于原始密码中的英文字母不能为 x(X) , y(Y) , z(Z) ,因此,只需将字符串中每个字符向后偏移3位,注意字符型和整数型做运算,结果会隐式转换为整数型,需要用char做强制类型转换。
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main() {
string str;
cin >> str;
for (auto x : str)
cout << char(x + 3);
return 0;
}
推理
解析: 共n次循环,每一次循环要遍历2*n栈灯。 同时,处于第i次循环时,将i的倍数的门进行取反操作,即0变1,1变0,最终统计打开的门的数量。
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 210;
int n, ans = 0;
int book[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= 2 * n; j += i)
book[j] ^= 1;
}
for (int i = 1; i <= 2 * n; i++)
if (book[i]) ans++;
cout << ans << endl;
return 0;
}
节气
解析: 首先仔细阅读题目,由于题干较长,需要将题目信息转换为程序表示。 可以根据给定的节气范围进行逐个判断,也可以转换为循环结构。 比如题目输入日期为7月4日,处于夏至和小暑之间,按照要求输出为 小暑,即“XS”。
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;
const int N = 30;
struct Node {
int month, day;
string jq;
};
vector<Node> v = { {1, 5, "XH"}, {1, 20, "DH"}, {2, 4, "LC"}, {2, 19, "YS"}, {3, 5, "JZ"}, {3, 20, "CF"},
{4, 4, "QM"}, {4, 19, "GY"}, {5, 5, "LX"}, {5, 20, "XM"}, {6, 5, "MZ"}, {6, 21, "XZ"},
{7, 6, "XS"}, {7, 22, "DS"}, {8, 7, "LQ"}, {8, 22, "CS"}, {9, 7, "BL"}, {9, 22, "QF"},
{10, 8, "HL"}, {10, 23, "SJ"}, {11, 7, "LD"}, {11, 22, "XX"}, {12, 7, "DX"}, {12, 21, "DZ"} };
int main() {
int month, day;
cin >> month >> day;
if (month == 12 && day > 21) {
puts("XH");
return 0;
}
for (auto x : v) {
//cout << x.month << " " << x.day << " " << x.jq << endl;
if (x.month >= month && x.day >= day) {
cout << x.jq << endl;
return 0;
}
}
return 0;
}
垃圾分类
解析: 本题题干也极其长,合理运用数据结构可以很大程度上简化程序实现,从而 更少的出现误差。 map结构将具体的垃圾映射为某一种垃圾分类,样例程序中考虑到垃圾分类英文字母比较多,也会多次使用到,用vector存储起来,对应类别垃圾可以表示为vector的下标。
注意点: 读取完整数再读取带空格字符串,需要用到cin.ignore()清空缓冲区的回车。
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;
int cnt[5];
vector<string> v = {"0", "FOOD WASTE", "RECYCLABLE", "HAZARDOUS", "RESIDUAL WASTE"};
int main() {
int n;
string s;
scanf("%d\n", &n);
while (n--) {
getline(cin, s);
if (s == "leaves" || s == "watermelon peel" || s == "leftovers") cnt[1]++;
else if (s == "paper box" || s == "plastic bottle" || s == "clothes") cnt[2]++;
else if (s == "rechargeable battery" || s == "abandoned medicine" || s == "disinfectant") cnt[3]++;
else cnt[4]++;
}
int maxv = -1;
int idx = -1;
for (int i = 1; i <= 4; i++){
if (cnt[i] > maxv) {
maxv = cnt[i];
idx = i;
}
}
cout << v[idx] << endl << cnt[idx] << endl;
return 0;
}
相关文章
- 让主流平台CINEBENCH分数也能破万!锐龙9 3950X 16核心平台实战体验
- 111个提单及运输业务中常用的一些代码、术语及意义
- NOIP2018成绩查询,申诉,分数线等时间表!(附复赛题目解析)
- 吐糟joomla sobipro组件(joomla可视化大屏模板)
- 小苯的文章浏览【Python实现和C++】
- 详解C++模拟算法,蓝桥杯真题(蓝桥杯c++编译环境)
- CSP-NOIP信息学竞赛(28)cin(noip信息学奥林匹克竞赛)
- C++实现进程间通讯实例详解(c++进程间通信 共享内存)
- C/C++编程笔记:getline()函数和字符数组
- shell 脚本中日期运算(shell脚本 日期变量)