01 模拟 - 竞技编程&基础算法

模拟, 顾名思义, 就是题目让你做什么你就做什么, 它考察的是将思路转换为代码的代码能力, 这类题目一般较为简单, 是比赛里面的签到题

模拟 - 多项式输出

https://www.luogu.com.cn/problem/P1067

这个题讲了一大堆构成多项式的条件, 经过仔细的分析再一一对应的去模拟就行了

Pasted image 20260814232431

参考题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
bool first = true;
for (int i = n; i >= 0; i--)
{
int m;
cin >> m;

if (m == 0) continue;

if (m < 0)
cout << "-";
else if (!first)
cout << "+";

first = false;
m = abs(m);
if (m != 1 || i == 0)
cout << m;


if (i == 1)
cout << "x";
else if (i != 0)
cout << "x^" << i;
}

return 0;
}

模拟 - 蛇形方阵

https://www.luogu.com.cn/problem/P5731 使用方向向量的方式去解决所有方阵填数字的问题

Pasted image 20260815212934

参考题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

const int N = 10;
int arr[N][N];
// 1.方向向量
int pos;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main()
{
int n;
cin >> n;
int cnt = 1;
int x = 1, y = 1;
while (cnt <= n * n)
{
arr[x][y] = cnt;

// 2.判断边界
int nx = x + dx[pos];
int ny = y + dy[pos];
if (nx < 1 || nx > n || ny < 1 || ny > n || arr[nx][ny])
{
pos = (pos + 1) % 4;
nx = x + dx[pos];
ny = y + dy[pos];
}
x = nx;
y = ny;
cnt++;
}

for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
printf("%3d", arr[i][j]);
}
cout << "\n";
}
return 0;
}

模拟 - 字符串展开

https://www.luogu.com.cn/problem/P1098 纯模拟 - 考察代码能力

参考题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int p1, p2, p3;
string s;

bool sameType(char a, char b)
{
return (islower(a) && islower(b)) || (isdigit(a) && isdigit(b));
}

void changeDash(string::iterator i)
{
string dash;
for (char k = (*(i - 1) + 1); k < *(i + 1); k++)
{
for (int j = 1; j <= p2; j++)
dash += k;
}

for (auto &e : dash)
{
if (p1 == 1)
e = tolower(e);
else if (p1 == 2)
e = toupper(e);
else
e = '*';
}

if (p3 == 2)
reverse(dash.begin(), dash.end());

cout << dash;
}

int main()
{
cin >> p1 >> p2 >> p3 >> s;
for (auto i = s.begin(); i != s.end(); ++i)
{
if (*i == '-'
&& i != s.begin()
&& i + 1 != s.end()
&& *(i - 1) < *(i + 1)
&& sameType(*(i - 1), *(i + 1)))
changeDash(i);
else
cout << *i;
}
return 0;
}