Guest User

Untitled

a guest
Dec 11th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6. * @author Bruce Feldman
  7. */
  8. public class Decal_Solver {
  9.  
  10.  
  11. public static void main(String[] args) {
  12. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13. do {
  14. System.out.println("Please enter the # (Less than 2^31 - 1) of the decal you want to find.");
  15. String inputString = null;
  16. try {
  17. inputString = reader.readLine();
  18. } catch (IOException e1) {
  19. System.out.println("There was an internal error when reading your input.");
  20. System.out.println("We apologize for this error, the script will now stop");
  21. System.exit(0);
  22. }
  23. Integer decal = null;
  24. try {
  25. decal = Integer.parseInt(inputString);
  26. } catch (NumberFormatException e) {
  27. System.out.println("You did not enter in a valid number! Shame on you!");
  28. continue;
  29. }
  30. if (decal < Integer.MAX_VALUE) {
  31. System.out.println("The value of decal #" +decal +" is " +decal(decal));
  32. break;
  33. }
  34. } while (true);
  35.  
  36. }
  37.  
  38.  
  39. static String decal(int n) {
  40. long end = 0, length = 0;
  41. while (end < n)
  42. end += Math.pow(9, ++length / 2);
  43. long d = end - n, slots = length / 2;
  44. StringBuilder b = new StringBuilder();
  45. while (b.length() < slots)
  46. b.append(9 - (d / (int) Math.pow(9, slots - b.length() - 1) % 9));
  47. if (length % 2 != 0)
  48. b.append('5');
  49. for (int i = 0; i < slots; ++i)
  50. b.append(10 - b.charAt((int) (slots - i - 1)) + '0');
  51. return b.toString();
  52. }
  53.  
  54.  
  55. }
Add Comment
Please, Sign In to add comment