下面分享三道比较简单的三道模拟题目. 算是一些蓝桥杯竞赛的签到题, 难度不高, 考察的是我们的一个把思路转换成代码的一个能力. 再就是分类讨论的一个能力.
1. 多项式输出
题目链接: LINK
题目思路: 模拟, 分类讨论
下面是参考代码:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
// 接收参数
int n = 0;
cin >> n;
vector<int> v;
v.resize(n+1);
for(int i = 0; i <= n; i++)
{
cin >> v[i];
}
// 输出
for(int i = n; i >= 0; i--)
{
// 1.输出数字
// - 如果不是首项
if(i != n && v[n-i] != 0)
{
if(v[n-i] < 0) cout << "-";
else cout << "+";
}
// - 如果是首项 并且 是负数
else if(i == n && v[n-i] < 0)
{
cout << "-";
}
// - 输出数字绝对值
if (abs(v[n-i]) != 0 && abs(v[n-i]) != 1) cout << abs(v[n-i]);
else if(i == 0 && abs(v[n-i]) == 1) cout << 1;
// 2.输出X问题
if (v[n-i] != 0)
{
if(i!=0 && i!=1) cout << "x^" << i;
else if(i == 1) cout << "x";
}
}
return 0;
}
2. 蛇形矩阵
题目链接: LINK
题目思路:方向向量控制方向, cnt++赋值即可.
参考代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int arr[9][9] = { 0 };
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
// arr[x][y]
int main()
{
int n;
cin >> n;
int cnt = 1;
int x = 0;
int y = 0;
int pos = 0;
while (cnt <= n * n)
{
// 赋值
arr[x][y] = cnt;
// 看下一步是否越界
if (x + dx[pos] >= n || y + dy[pos] >= n || arr[x + dx[pos]][y + dy[pos]] != 0 || x + dx[pos] < 0 || y + dy[pos] < 0)
{
pos = (pos + 1) % 4;
}
// 正式前进
x = x + dx[pos];
y = y + dy[pos];
cnt++;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%3d", arr[i][j]);
}
cout << endl;
}
return 0;
}
3. 字符串的展开
题目链接: LINK
题目思路: 模拟
参考代码:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int p1, p2, p3;
string str;
string ret;
bool my_is_digit(char ch)
{
return ch >= '0' && ch <= '9';
}
bool my_is_lower_letter(char ch)
{
return ch >= 'a' && ch <= 'z';
}
void my_add(char left, char right)
{
string t_str;
for(char i = left + 1; i < right; i++)
{
char temp_ch = i;
// p1
if(p1 == 2 && my_is_lower_letter(temp_ch)) temp_ch -= 32;
else if(p1 == 3) temp_ch = '*';
// p2
for(int j = 0; j < p2; j++)
{
t_str += temp_ch;
}
}
// p3
if(p3 == 2) reverse(t_str.begin(), t_str.end());
ret += t_str;
}
int main()
{
// 读入数据
cin >> p1 >> p2 >> p3;
cin >> str;
// 判断是否去重写
for(int i = 0; i < str.size(); i++)
{
if(str[i] != '-' || i == 0 || i == str.size()-1) ret += str[i];
else // 如果是'-', 并且不在首尾
{
// 如果左右两边同为数字, 准备展开
if((my_is_digit(str[i-1]) && my_is_digit(str[i+1])) && str[i-1] < str[i+1] || (my_is_lower_letter(str[i-1]) && my_is_lower_letter(str[i+1])) && str[i-1] < str[i+1])
{
my_add(str[i-1], str[i+1]);
}
else
{
ret += str[i];
}
}
}
cout << ret << endl;
return 0;
}
EOF.