Advertisement
FedchenkoIhor

find int x int xx

Mar 3rd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. package ws.kostin.loops.task16;
  2.  
  3. /**
  4.  * В американской армии считается несчастливым число 13, а в японской — 4.
  5.  * Перед международными учениями штаб российской армии решил исключить номера боевой техники,
  6.  * содержащие числа 4 или 13 (например, 40123, 13313, 12345 или 13040),чтобы не смущать иностранных коллег.
  7.  * Если в распоряжении армии имеется 100 тыс. единиц боевой техники
  8.  * и каждая боевая машина имеет номер от 00001 до 99999, то сколько всего номеров придётся исключить?
  9.  */
  10. public class Solution {
  11.     public static void main(String[] args) {
  12.         int result = 0;
  13.         for (int i = 1; i < 1000000; i++) {
  14.             if (findDigit(i, 4) || findDigit(i, 13)) {
  15.                 result++;
  16.             }
  17.         }
  18.         System.out.println(result);
  19.     }
  20.  
  21.     public static boolean findDigit(int input, int digit) {
  22.         int divisor = factor(digit);
  23.         while (input != 0) {
  24.             if (input % divisor == digit) {
  25.                 return true;
  26.             }
  27.             input /= 10;
  28.         }
  29.         return false;
  30.     }
  31.  
  32.     public static int factor(int input) {
  33.         int result = 1;
  34.         while (input != 0) {
  35.             input /= 10;
  36.             result *= 10;
  37.         }
  38.         return result;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement