UVa11005

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=22&page=show_problem&problem=1946

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
#include <iostream>
#include <vector>

using namespace std;


int main(){
int n;
cin >> n;
bool first = true;
for(int cases = 1; cases <= n; ++cases){
if(!first) cout << '\n';
else first = false;
cout << "Case " << cases << ":\n";

vector<int> cost;
int costtmp;
for(int i=0; i<36; ++i){
cin >> costtmp;
cost.push_back(costtmp);
}

vector<int> cheapest;
int q;
cin >> q;
// Y個輸入數字,將這個數字用各個base做mod得到餘數,然後除base直到Y=0,加總的所有餘數=base的cost
int num;
int mini;
int sum;
while(q--){
cheapest.clear();
mini = 0x3f3f3f3f;
cin >> num;
for(int i=2; i<=36; ++i){
sum = 0;
int tmp = num;
while(tmp > 0){
int digit = tmp % i;
sum += cost[digit];
tmp /= i;
}
if(sum < mini){
cheapest.clear();
cheapest.push_back(i);
mini = sum;
}
else if(sum == mini) cheapest.push_back(i);
}

cout << "Cheapest base(s) for number " << num << ":";
for(int cheap : cheapest){
cout << ' ' << cheap;
}
cout << '\n';
}
}

return 0;
}