Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ws.kostin.loops.task16;
- /**
- * В американской армии считается несчастливым число 13, а в японской — 4.
- * Перед международными учениями штаб российской армии решил исключить номера боевой техники,
- * содержащие числа 4 или 13 (например, 40123, 13313, 12345 или 13040),чтобы не смущать иностранных коллег.
- * Если в распоряжении армии имеется 100 тыс. единиц боевой техники
- * и каждая боевая машина имеет номер от 00001 до 99999, то сколько всего номеров придётся исключить?
- */
- public class Solution {
- public static void main(String[] args) {
- int result = 0;
- for (int i = 1; i < 1000000; i++) {
- if (findDigit(i, 4) || findDigit(i, 13)) {
- result++;
- }
- }
- System.out.println(result);
- }
- public static boolean findDigit(int input, int digit) {
- int divisor = factor(digit);
- while (input != 0) {
- if (input % divisor == digit) {
- return true;
- }
- input /= 10;
- }
- return false;
- }
- public static int factor(int input) {
- int result = 1;
- while (input != 0) {
- input /= 10;
- result *= 10;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement