如何从数组中找到除数最多的数字

发布于 2025-01-09 19:24:28 字数 331 浏览 0 评论 0原文

我有一个数组,我想找到除数最多的数字。问题是我可以找到数字,但无法打印它是多少除数。

static void printDivisors(int n)
{
    for (int i=1;i<=n;i++)
        if (n%i==0)
            System.out.print(i+" ");
}


public static void main(String args[])
{
    System.out.println("The divisors of 100 are: ");
    printDivisors(100);;
}

}

I have an array and i want to find number with most divisor. The problem is that i can find the number but i cant print how many divisor it is.

static void printDivisors(int n)
{
    for (int i=1;i<=n;i++)
        if (n%i==0)
            System.out.print(i+" ");
}


public static void main(String args[])
{
    System.out.println("The divisors of 100 are: ");
    printDivisors(100);;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2025-01-16 19:24:28

首先,您只需检查 1n/2 之间的值即可找到 n 的除数。您不会在 n/2n 之间找到除数(n 本身除外)。这将有效地将您的执行时间减少一半。

因此,您的除数查找方法可以像下面这样改进,以计算给定数字的除数数量。

static int getNumDivisors(int n) {
    int noOfDivisors = 0;
    for (int i = 1; i <= n / 2; i++) {
        if (n % i == 0) {
            noOfDivisors++;
        }
    }
    if (n > 1) {
        noOfDivisors++; // n itself is a divisor of n
    }
    return noOfDivisors;
}

然后,您需要迭代用户给出的 numbers 数组并找到每个数字的除数数。

迭代时,您需要 2 个变量

  • currentMaxDivisors - 存储迄今为止找到的最大除数
  • numWithMaxDivisors - 哪个数字具有上述除数数量

如果您碰巧找到一个具有更多除数的新数字,您可以使用新值更新这些变量。最后,numWithMaxDivisors 变量将包含具有最大除数的数字。

static int getNumDivisors(int n) {
    int noOfDivisors = 0;
    for (int i = 1; i <= n / 2; i++) {
        if (n % i == 0) {
            System.out.print(i + " ");
            noOfDivisors++;
        }
    }
    if (n > 1) {
        noOfDivisors++; // n itself is a divisor of n
    }
    return noOfDivisors;
}

static int getNumWithMaxDivisors(int[] numbers) {
    // Assuming numbers array has at least one element
    int currentMaxDivisors = 0;
    int numWithMaxDivisors = numbers[0];
    for (int i = 0; i < numbers.length; i++) {
        int numDivisors = getNumDivisors(numbers[i]);
        if (numDivisors > currentMaxDivisors) {
            numWithMaxDivisors = numbers[i];
        }
    }
    return numWithMaxDivisors;
}

public static void main(String[] args) {
    int[] numbers = new int[]{100, 55, 67, 2003, 12};
    int numWithMaxDivisors = getNumWithMaxDivisors(numbers);
    System.out.println(numWithMaxDivisors);
}

First of all, you only need to check values between 1 and n/2 to find the divisors of n. You won't find a divisor between n/2 and n (except for n itself). This will effectively reduce your execution time by half.

So your divisor finding method can be improved like below to count the number of divisors of a given number.

static int getNumDivisors(int n) {
    int noOfDivisors = 0;
    for (int i = 1; i <= n / 2; i++) {
        if (n % i == 0) {
            noOfDivisors++;
        }
    }
    if (n > 1) {
        noOfDivisors++; // n itself is a divisor of n
    }
    return noOfDivisors;
}

Then you need to iterate through the numbers array given by user and find the number of divisors for each number.

While iterating you need 2 variables

  • currentMaxDivisors - to store the max no of divisors you have found so far
  • numWithMaxDivisors - which number had the above number of divisors

If you happen to find a new number with more divisors, you update these variables with the new values. At the end, numWithMaxDivisors variable will contain the number with maximum divisors.

static int getNumDivisors(int n) {
    int noOfDivisors = 0;
    for (int i = 1; i <= n / 2; i++) {
        if (n % i == 0) {
            System.out.print(i + " ");
            noOfDivisors++;
        }
    }
    if (n > 1) {
        noOfDivisors++; // n itself is a divisor of n
    }
    return noOfDivisors;
}

static int getNumWithMaxDivisors(int[] numbers) {
    // Assuming numbers array has at least one element
    int currentMaxDivisors = 0;
    int numWithMaxDivisors = numbers[0];
    for (int i = 0; i < numbers.length; i++) {
        int numDivisors = getNumDivisors(numbers[i]);
        if (numDivisors > currentMaxDivisors) {
            numWithMaxDivisors = numbers[i];
        }
    }
    return numWithMaxDivisors;
}

public static void main(String[] args) {
    int[] numbers = new int[]{100, 55, 67, 2003, 12};
    int numWithMaxDivisors = getNumWithMaxDivisors(numbers);
    System.out.println(numWithMaxDivisors);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文