Advertisement
Camer047

problem 2

May 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. public void problem2() {
  2.  
  3.         // 1 <= x, x <= 9
  4.         // 1 <= y, z <= pow(2, 31) - 1
  5.         // x*y*z <= pow(2, 31) - 1
  6.         System.out.println("Enter Digits:");
  7.         Scanner scanIn = new Scanner(System.in);
  8.         int index = 0;
  9.  
  10.         while (scanIn.hasNext()) {
  11.             String in = scanIn.nextLine();
  12.             index++;
  13.  
  14.             int x = Integer.parseInt(in.substring(0, 1));
  15.             int y = Integer.parseInt(in.substring(2, 3));
  16.             int z = Integer.parseInt(in.substring(4, in.length()));
  17.             int count = 0;
  18.            
  19.  
  20.             if (x >= 1 && x <= 9 && y >= 1 && z <= Math.pow(2, 31) - 1) {
  21.                 long product = x * y * z;
  22.  
  23.                 System.out.println();
  24.                 System.out.print("Case " + index + ": ");
  25.                 System.out.print(product);
  26.  
  27.                 // check if x appears y times in product
  28.                 if (x * y * z <= Math.pow(2, 31) - 1) {
  29.                     for (char c : new String(product + "").toCharArray()) {
  30.                         if (c == x + '0') {
  31.                             count++;
  32.                         }
  33.                     }
  34.                     if (count == y)
  35.                         System.out.print(" yes");
  36.                     else
  37.                         System.out.print(" no");
  38.                     System.out.println();
  39.                 }
  40.             }
  41.         }
  42.         scanIn.close();
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement