Gas Price Is Going Up/Down
两次 UCF 都出现了这个题,好奇怪
UCF Local Programming Contest 2013(Practice)
UCF Local Programming Contest 2015(Practice)
题意:
a
,
b
,
c
a,b,c
a,b,c 三个三位数,数中可能有模糊表示为 " - ",每个数字可能有一个或零个 " - ",可以用
[
0
,
9
]
[0,9]
[0,9] 之间的数字替换掉 " - ",且保证替换后
a
<
b
<
c
a<b<c
a<b<c,
a
+
b
+
c
a+b+c
a+b+c 最小,三个数都在
[
200
,
500
]
[200,500]
[200,500] 之间。
思路:
每个数有一个或零个需要修改的位,暴力
[
0
,
9
]
[0,9]
[0,9] ,然后每个数都要改,三重循环,每次修改后更新答案。
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
void work(string a, string b, string c, int &x, int &y, int &z, int i, int j, int k) {
x = y = z = 0;
for (int t = 0; t < 3; t++)
if (a[t] == '-')
x = x * 10 + i;
else
x = x * 10 + a[t] - '0';
for (int t = 0; t < 3; t++)
if (b[t] == '-')
y = y * 10 + j;
else
y = y * 10 + b[t] - '0';
for (int t = 0; t < 3; t++)
if (c[t] == '-')
z = z * 10 + k;
else
z = z * 10 + c[t] - '0';
}
bool judge(char &c, int i) {
if (c == '-' && (i == 0 || i == 1))
return false;
else
return true;
}
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
string a, b, c;
int a1 = 0, a2 = 0, a3 = 0, sum = INF;
cin >> a >> b >> c;
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++)
for (int k = 0; k <= 9; k++)
if (judge(a[0], i) && judge(b[0], j) && judge(c[0], k)) {
int x, y, z;
work(a, b, c, x, y, z, i, j, k);
if (sum > x + y + z && x < y && y < z) {
sum = x + y + z;
a1 = x;
a2 = y;
a3 = z;
}
}
cout << "Gas Station #" << t << ":\n"
<< " Input: " << a << " " << b << " " << c << "\n"
<< " Output: " << a1 << " " << a2 << " " << a3 << "\n\n";
}
return 0;
}