Recent Posts
None | 4 sec ago
None | 4 sec ago
PHP | 11 sec ago
None | 13 sec ago
PHP | 15 sec ago
None | 35 sec ago
None | 35 sec ago
None | 39 sec ago
PHP | 49 sec ago
None | 53 sec ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
By kathirvel on the 14th of Oct 2008 10:14:27 AM
Download |
Raw |
Embed |
Report
class CrazyBobBeust
{
public static void findAll(long max) {
Digit zero
= new Digit
(null, 0
);
Digit one = zero.next;
Listener listener
= new Listener
();
for (int length = 1; length <= 10; length++) {
if (find(one, zero, length, 0, max, listener)) return;
}
listener.output();
}
private static bool find(Digit start, Digit head, int remaining, long value, long max, Listener listener) {
for (Digit current = start; current != null; current = current.next) {
long newValue = value + current.value;
if (remaining == 1) {
if (newValue > max) return true;
listener.hear(newValue);
}
else {
current.use();
Digit newHead = (current == head) ? head.next : head;
if (find(newHead, newHead, remaining - 1, newValue * 10, max, listener))
return true;
current.yield();
}
}
return false;
}
class Digit
{
public readonly int value;
public Digit previous;
public Digit next;
public Digit(Digit previous, int value) {
this.value = value;
this.previous = previous;
if (value
< 9
) next
= new Digit
(this, value
+ 1
);
}
public void use() {
if (previous != null) previous.next = next;
if (next != null) next.previous = previous;
}
public void yield() {
if (previous != null) previous.next = this;
if (next != null) next.previous = this;
}
}
class Listener
{
private int _total = 0;
public void hear(long value) {
++_total;
}
public void output() {
Console.WriteLine("Total: {0}", _total);
}
}
}
Submit a correction or amendment below.
[ previous version ] | [ difference ] | Make A New Post