Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- * @author Bruce Feldman
- */
- public class Decal_Solver {
- public static void main(String[] args) {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- do {
- System.out.println("Please enter the # (Less than 2^31 - 1) of the decal you want to find.");
- String inputString = null;
- try {
- inputString = reader.readLine();
- } catch (IOException e1) {
- System.out.println("There was an internal error when reading your input.");
- System.out.println("We apologize for this error, the script will now stop");
- System.exit(0);
- }
- Integer decal = null;
- try {
- decal = Integer.parseInt(inputString);
- } catch (NumberFormatException e) {
- System.out.println("You did not enter in a valid number! Shame on you!");
- continue;
- }
- if (decal < Integer.MAX_VALUE) {
- System.out.println("The value of decal #" +decal +" is " +decal(decal));
- break;
- }
- } while (true);
- }
- static String decal(int n) {
- long end = 0, length = 0;
- while (end < n)
- end += Math.pow(9, ++length / 2);
- long d = end - n, slots = length / 2;
- StringBuilder b = new StringBuilder();
- while (b.length() < slots)
- b.append(9 - (d / (int) Math.pow(9, slots - b.length() - 1) % 9));
- if (length % 2 != 0)
- b.append('5');
- for (int i = 0; i < slots; ++i)
- b.append(10 - b.charAt((int) (slots - i - 1)) + '0');
- return b.toString();
- }
- }
Add Comment
Please, Sign In to add comment