Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 倍数程序_Java_Algorithm_Math - Fatal编程技术网

Java 倍数程序

Java 倍数程序,java,algorithm,math,Java,Algorithm,Math,这是我试图解决的编程问题:2520是最小的数字,可以被1到10的每个数字除,没有任何余数 能被1到20的所有数整除的最小正数是多少 这是到目前为止我的解决方案,但是每次答案都是零,所以我认为我的代码中有一个错误。任何帮助都将不胜感激 public static boolean isDiv(int num){ boolean isDiv = false; for (int i = 1; i <= 20; i++){ if (i == 20){

这是我试图解决的编程问题:2520是最小的数字,可以被1到10的每个数字除,没有任何余数

能被1到20的所有数整除的最小正数是多少

这是到目前为止我的解决方案,但是每次答案都是零,所以我认为我的代码中有一个错误。任何帮助都将不胜感激

public static boolean isDiv(int num){
    boolean isDiv = false;

    for (int i = 1; i <= 20; i++){
        if (i == 20){

            isDiv = true;

        }
        if ((num % i) == 0){
            continue;

        }
        else  {
            break;
        }

    }


return isDiv;}

public static int smallMulti(int num){
    boolean div = isDiv(num);
    int answer = 0;

    for (int i = num; num < 2520; i--){

        if (div = true){
            answer = i;
        }
    }

return answer;}
publicstaticbooleanisdiv(int-num){
布尔isDiv=假;

对于(int i=1;i你把整个问题复杂化了,加上多个逻辑错误。基本上你只需要两个循环。下面是一个代码检查,每个数字都可以设计第一个数字,直到
Integer.MAX\u VALUE
。如果你想更高,你可以采用代码来处理
long

public static int smallMulti(int num) {
    for (int i = 1; num <= Integer.MAX_VALUE; ++i) { // Check every int in the scope of the Integer
        for (int j = 2;j<=num;++j) {
            if(i % j != 0) {
                break; // If i % j is unequal to 0 then this number isn´t valid.
            }
            if(j == num) {
                return i; // If we reached j == num then everything was divisble yet so we can return i as the correct value;
            }
        }
    }
    return -1;
}

你把整个问题复杂化了,再加上多个逻辑错误。基本上你只需要两个循环。下面是一个代码检查,每个数字都可以设计第一个数字,直到
整数。MAX_VALUE
。如果你想更高,你可以使用
long

public static int smallMulti(int num) {
    for (int i = 1; num <= Integer.MAX_VALUE; ++i) { // Check every int in the scope of the Integer
        for (int j = 2;j<=num;++j) {
            if(i % j != 0) {
                break; // If i % j is unequal to 0 then this number isn´t valid.
            }
            if(j == num) {
                return i; // If we reached j == num then everything was divisble yet so we can return i as the correct value;
            }
        }
    }
    return -1;
}
我使用lcm(最小公倍数)实现

publicstaticintlcm(inta,intb){
报税表(a*b)/gcd(a、b);
}
公共静态整数gcd(整数a、整数b){
返回b==0?a:gcd(b,a%b);
}
公共静态int smallMulti(int n){
整数=1;
对于(int i=2;ii)使用lcm(最小公倍数)实现

publicstaticintlcm(inta,intb){
报税表(a*b)/gcd(a、b);
}
公共静态整数gcd(整数a、整数b){
返回b==0?a:gcd(b,a%b);
}
公共静态int smallMulti(int n){
整数=1;

对于(int i=2;i,因为它很可能不在您正在检查的
Integer.MIN_VALUE-2520
范围内。不,您不在范围内。
num<2520;i--
。您很可能没有进行一次迭代。实际上,您会循环,直到
i
溢出负值并等于
Integer.MAX_VALUE
。您也永远不会重复eassign
div
.for(int i=num;num<2520;i--){if(div=true){answer=i;}}充满了问题。请检查它。
if(div=true)
请重新检查您的Java书籍中关于比较内容的内容。因为它很可能不在您正在检查的
整数.MIN_VALUE-2520
范围内。不,您没有。
num<2520;i--
。您很可能没有进行任何迭代。实际上,您循环直到
i
溢出负值并等于Integer.MAX_VALUE
。您也从未重新分配
div
。for(int i=num;num<2520;i--){if(div=true){answer=i;}}充满了问题。请检查它。
if(div=true)
请重新检查您的Java书籍中关于比较的内容。
Smallest Value divisible by 1-2 = 2
Smallest Value divisible by 1-3 = 6
Smallest Value divisible by 1-4 = 12
Smallest Value divisible by 1-5 = 60
Smallest Value divisible by 1-6 = 60
Smallest Value divisible by 1-7 = 420
Smallest Value divisible by 1-8 = 840
Smallest Value divisible by 1-9 = 2520
Smallest Value divisible by 1-10 = 2520
Smallest Value divisible by 1-11 = 27720
Smallest Value divisible by 1-12 = 27720
Smallest Value divisible by 1-13 = 360360
Smallest Value divisible by 1-14 = 360360
Smallest Value divisible by 1-15 = 360360
Smallest Value divisible by 1-16 = 720720
Smallest Value divisible by 1-17 = 12252240
Smallest Value divisible by 1-18 = 12252240
Smallest Value divisible by 1-19 = 232792560
Smallest Value divisible by 1-20 = 232792560
public static int lcm(int a, int b) {
    return (a*b)/gcd(a, b);
}

public static int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

public static int smallMulti(int n) {
    int number = 1;

    for (int i = 2; i <= n; i++) {
        number = lcm(number, i);
    }

    return number;
}