T1 扑克牌
代码
#include <bits/stdc++.h>
using namespace std;
int n;
set<string> s; // 去重
int main()
{
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
{
string str;
cin >> str;
s.insert(str); // 插入
}
cout << 52 - s.size(); // 共 52 张
}思路
使用 STL set,实现去重,输入时直接插入 set,用总张数 张减去。
T2 地图探险
代码
#include <bits/stdc++.h>
using namespace std;
class worker
{
private:
int n, m, k;
int x0, y0, d0;
int x, y, d;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool can_go[1001][1001];
set<pair<int, int>> s;
public:
int main()
{
cin >> n >> m >> k;
cin >> x0 >> y0 >> d0;
x = x0;
y = y0;
d = d0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
{
char ch;
cin >> ch;
can_go[i][j] = (ch == '.');
}
s.insert({x, y});
for (int i = 1; i <= k; ++i)
{
int new_x = x + dir[d][0];
int new_y = y + dir[d][1];
if (1 <= new_x && new_x <= n && 1 <= new_y && new_y <= m && can_go[new_x][new_y])
{
x = new_x;
y = new_y;
s.insert({new_x, new_y});
}
else
d = (d + 1) % 4;
}
cout << s.size() << '\n';
return 0;
}
};
int T;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> T;
for (int t = 1; t <= T; ++t)
worker().main();
}思路
按照题意模拟,同时使用 set 去重,最后直接输出 set 中元素个数。
T3 小木棍
代码
#include <bits/stdc++.h>
using namespace std;
string basic[15] =
{
"-1",
"-1",
"1",
"7",
"4",
"2",
"6",
"8",
"10",
"18",
"22",
"20",
"28",
"68",
"88"};
string qz[7] =
{
"108",
"188",
"200",
"208",
"288",
"688",
"888"};
int T;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> T;
for (int t = 1; t <= T; ++t)
{
int n;
cin >> n;
if (n <= 14)
cout << basic[n] << '\n';
else
{
int sy = n - 14;
int z = (sy - 1) / 7;
int bh = (sy - 1) % 7;
cout << qz[bh] << setw(z) << setfill('8') << "" << '\n';
}
}
}思路
先 dfs 暴力找规律:
#include <bits/stdc++.h>
using namespace std;
int n;
size_t mn = ULLONG_MAX;
int xmg[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
void dfs(int yy, size_t s)
{
if (yy > n)
return;
if (yy == n)
{
mn = min(mn, s); // 取最小值
return;
}
for (int i = 0; i <= 9; ++i)
if (i == 0) // 防止前导零
{
if (s)
dfs(yy + xmg[i], s * 10 + i);
}
else
dfs(yy + xmg[i], s * 10 + i);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (n = 1; n <= 32; ++n)
{
mn = ULLONG_MAX;
dfs(0, 0);
if (mn == ULLONG_MAX) // 判断无解
cout << "-1\n";
else
cout << mn << '\n';
}
}得到:
-1
1
7
4
2
6
8
10
18
22
20
28
68
88
108
188
200
208
288
688
888
1088
1888
2008
2088
2888
6888
8888
10888
18888
20088
20888前 14 个没有规律,后面部分:
108
188
200
208
288
688
888
108 8
188 8
200 8
208 8
288 8
688 8
888 8
108 88
188 88
200 88
208 88可见前三位在 中循环,后面部分是 个 ,而 ,同样,循环中的编号(从 开始)为 。对于 之前的,使用一个 basic 数组记录,直接输出。