725.cpp

Back Download
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
// 725.cpp http://acm.uva.es/p/v7/725.html
//
// Solution to ACM problem #725 - "Division"
//
// Matthew Eagar - meagar@gmail.com
// Verified to work as of July 06 / 2009
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cassert>
#include <iomanip>
// Check to see if the given numbers contain all 10 digits between them
bool check(int n1, int n2) {
// Obvious checks for invalid pairs
if (n1 < 1234 || n2 < 1234) return false;
if (n1 < 9876 && n2 < 9876) return false;
// Add a 0 (01234 vs 12340 doesn't matter for these tests)
if (n1 < 9876) n1 *= 10;
if (n2 < 9876) n2 *= 10;
bool used[10] = { 0 };
int n;
while (n1 > 0) {
n = n1 % 10;
if (used[n]) return false;
used[n] = true;
n1 /= 10;
}
while (n2 > 0) {
n = n2 % 10;
if (used[n]) return false;
used[n] = true;
n2 /= 10;
}
return true;
}
int main() {
int n;
std::cin >> n;
while (n) {
bool noSolutions = true;
// Start with the lowest possible numerator/denominator
for (int num = 1234 * n, denom = 1234
// Loop until we hit the maximum of either value
; denom <= 98765 && num <= 98765
// Each step denominator goes up 1, numerator goes up by n
; ++denom, num += n) {
if (check(num, denom)) {
// We have a valid pair
std::cout << num << " / ";
std::cout.width(5);
std::cout.fill('0');
std::cout << denom << " = " << n << std::endl;
noSolutions = false;
}
}
if (noSolutions)
std::cout << "There are no solutions for " << n << "." << "\n";
if (std::cin >> n && n != 0)
std::cout << '\n';
}
}