【洛谷-P1008】解题报告(水题)

原始题目

P1008 三连击

题目大意

\(1,2, \cdots ,9\)\(9\)个数分成\(3\)组,分别组成\(3\)个三位数,且使这\(3\)个三位数构成\(1:2:3\)的比例,试求出所有满足条件的\(3\)个三位数。

解题思路

注意无0的问题,暴力解。

解题代码

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 192 384 576
// 219 438 657
// 267 534 801 这个包含0去掉
// 273 546 819
// 327 654 981

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;

#define rep(i, a, n) for (int i = a; i < n; ++i)
#define per(i, a, n) for (int i = n - 1; i >= a; --i)
#define fi first
#define se second
#define endl '\n'
#define all(x) x.begin(), x.end()
#define pb push_back
#define np next_permutation
#define mp make_pair

int vis[maxn];

bool check(int num)
{
int a[6];
int num1 = 2 * num;
int num2 = 3 * num;
if (num2 >= 999)
return false;
// if (num / 100 == 1)
// cout << num1 << " " << num2 << endl;
rep(i, 0, 3)
{
a[i] = num1 % 10;
num1 /= 10;
}
rep(i, 3, 6)
{
a[i] = num2 % 10;
num2 /= 10;
}
rep(i, 0, 6)
{
if (vis[a[i]])
return false;
vis[a[i]] = 1;
}
return true;
}

int main()
{
ios::sync_with_stdio(false);
rep(i, 1, 3 + 1)
{
vis[i] = 1;
rep(j, 1, 9 + 1)
{
if (vis[j])
continue;
vis[j] = 1;
rep(k, 1, 9 + 1)
{
vis[i] = vis[j] = 1;
if (vis[k])
continue;
vis[k] = 1;
int temp = 100 * i + 10 * j + k;
if (check(temp)) {
cout << temp << " " << 2 * temp << " " << 3 * temp << endl;
}
memset(vis, 0, sizeof(vis));
}
vis[j] = 0;
}
vis[i] = 0;
}
int n;
cin >> n;
}

收获与反思