【问题标题】:Count all the "Good numbers" in a given range计算给定范围内的所有“好数字”
【发布时间】:2021-02-20 15:04:14
【问题描述】:

任务是关于所谓的好数字

如果一个数字% 的每个数字== 0,则该数字是好的。 任务是计算给定范围内的所有好数字。范围是 A 到 B。

例如:13 不是一个好数字,因为13 % 3 != 0

如果数字为零,我们也必须跳过。

另一个例子102 是一个很好的数字,因为102 % 1 == 0102 % 2 == 0

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] input = sc.nextLine().split(" ");
        int[] arrInt = new int[input.length];
        arrInt[0] = Integer.parseInt(input[0]);
        arrInt[1] = Integer.parseInt(input[1]);

        int counter = 0;
        boolean isGoodNumber = false;
        String s = "";

        for (int i = arrInt[0]; i <= arrInt[1]; i++) {
            s = Integer.toString(i);
            for (int j = 0; j < s.length(); j++) {
                if (s.charAt(j) == 0) {
                    continue;
                }
                int result = i / s.charAt(j);
                if (result % 1 != 0) {
                    isGoodNumber = false;
                    break;
                }
            }
            if (isGoodNumber) {
                counter += 1;
            }
            isGoodNumber = true;
        }
        System.out.println(counter);
    }
}

【问题讨论】:

    标签: java loops filter


    【解决方案1】:

    你可以先将一个数字转换成一个字符串,然后把这个字符串拆分成一个数组,然后将每个数字解析回一个整数,并在没有提示的情况下检查这个整数是否与原来的数字相除。示例:

    int[] goodNumbers = IntStream.rangeClosed(150, 200)
            .filter(number -> {
                // an array of digits as strings
                String[] arr = String.valueOf(number).split("");
                // check if good number
                return Arrays.stream(arr)
                        // string with a digit as an integer
                        .mapToInt(Integer::parseInt)
                        // avoid division by '0'
                        .filter(i -> i != 0)
                        // if the remainder of dividing a number
                        // by all its constituent digits is '0',
                        // then it is a 'Good number'
                        .allMatch(i -> number % i == 0);
            }).toArray();
    
    System.out.println(Arrays.toString(goodNumbers));
    // [150, 155, 162, 168, 175, 184, 200]
    System.out.println("Count: " + goodNumbers.length);
    // Count: 7
    

    【讨论】:

      【解决方案2】:

      我已经更新了下面的代码

      您在整数计算中使用character 来计算好数字,而不是将其转换回int

      此外,isGoodNumber 最初设置为 false,因为它应该设置为 true

      import java.util.Scanner;
      
      public class Main {
          public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
              String[] input = sc.nextLine().split(" ");
              int[] arrInt = new int[input.length];
              arrInt[0] = Integer.parseInt(input[0]);
              arrInt[1] = Integer.parseInt(input[1]);
      
              int counter = 0;
              boolean isGoodNumber = true;
              String s = "";
      
              for (int i = arrInt[0]; i <= arrInt[1]; i++) {
                  s = Integer.toString(i);
                  for (int j = 0; j < s.length(); j++) {
                      if (s.charAt(j) - '0' == 0) {
                          continue;
                      }
                      if (i % (s.charAt(j) - '0') != 0) {
                          isGoodNumber = false;
                          break;
                      }
                  }
                  if (isGoodNumber) {
                      counter += 1;
                  }
                  isGoodNumber = true;
              }
              System.out.println(counter);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-15
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        相关资源
        最近更新 更多