Digit Solitaire: Difference between revisions
imported>Dz54 Created page with "==Introduction== http://speedyguy17.info/data/mcpc/mcpc2012/pdf/B-Digit%20Solitaire.pdf Despite the glorious fall colors in the midwest, there is a great deal of time to spen..." |
imported>Dz54 |
||
Line 95: | Line 95: | ||
[[Category:ecna2010]] | [[Category:ecna2010]] |
Latest revision as of 01:38, 1 December 2015
Introduction
http://speedyguy17.info/data/mcpc/mcpc2012/pdf/B-Digit%20Solitaire.pdf
Despite the glorious fall colors in the midwest, there is a great deal of time to spend while on a train from St. Louis to Chicago. On a recent trip, we passed some time with the following game. We start with a positive integer S. So long as it has more than one digit, we compute the product of its digits and repeat. For example, if starting with 95, we compute 9 × 5 = 45. Since 45 has more than one digit, we compute 4 × 5 = 20. Continuing with 20, we compute 2 × 0 = 0. Having reached 0, which is a single-digit number, the game is over. As a second example, if we begin with 396, we get the following computations: 3 × 9 × 6 = 162 1 × 6 × 2 = 12 1 × 2 = 2 and we stop the game having reached 2.
Input: Each line contains a single integer 1 ≤ S ≤ 100000, designating the starting value. The value S will not have any leading zeros. A value of 0 designates the end of the input. Output: For each nonzero input value, a single line of output should express the ordered sequence of values that are considered during the game, starting with the original value.
Idea
This problem is quite easy; the idea is to write a function that checks the number of digits and the number, and then play the game. A more elegant way to check the number of digits is to convert it to a string and check the length.
Code
Solution - Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class digits {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int counter = 1;
while (true) {
int next = scan.nextInt();
if (next == 0) {
break;
}
scan.nextLine();
// System.out.print("Case " + counter + ":");
ArrayList<Integer> input = new ArrayList<Integer>();
input.add(next);
ArrayList<Integer> list = play(input);
Iterator<Integer> iter = list.iterator();
while (iter.hasNext()) {
System.out.print(iter.next());
if (iter.hasNext()) {
System.out.print(" ");
}
}
System.out.println();
counter++;
}
}
private static ArrayList<Integer> play(ArrayList<Integer> in) {
int last = in.get(in.size() - 1);
if (length(last) > 1) {
int product = 1;
for (int i = length(last); i > 0; i--) {
// System.out.println((int) Math.pow(10, i - 1));
int digit = last / ((int) Math.pow(10, i - 1));
product *= digit;
last -= digit * (((int) Math.pow(10, i - 1)));
}
in.add(product);
return play(in);
} else {
return in;
}
}
private static int length(int in) {
if (in == 100000) {
return 6;
} else if (in >= 10000) {
return 5;
} else if (in >= 1000) {
return 4;
} else if (in >= 100) {
return 3;
} else if (in >= 10) {
return 2;
} else {
return 1;
}
}
}