Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void problem2() {
- // 1 <= x, x <= 9
- // 1 <= y, z <= pow(2, 31) - 1
- // x*y*z <= pow(2, 31) - 1
- System.out.println("Enter Digits:");
- Scanner scanIn = new Scanner(System.in);
- int index = 0;
- while (scanIn.hasNext()) {
- String in = scanIn.nextLine();
- index++;
- int x = Integer.parseInt(in.substring(0, 1));
- int y = Integer.parseInt(in.substring(2, 3));
- int z = Integer.parseInt(in.substring(4, in.length()));
- int count = 0;
- if (x >= 1 && x <= 9 && y >= 1 && z <= Math.pow(2, 31) - 1) {
- long product = x * y * z;
- System.out.println();
- System.out.print("Case " + index + ": ");
- System.out.print(product);
- // check if x appears y times in product
- if (x * y * z <= Math.pow(2, 31) - 1) {
- for (char c : new String(product + "").toCharArray()) {
- if (c == x + '0') {
- count++;
- }
- }
- if (count == y)
- System.out.print(" yes");
- else
- System.out.print(" no");
- System.out.println();
- }
- }
- }
- scanIn.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement