我想要的输出是最小的正值,它可以被1到20之间的所有数字平均整除? import java.util.*; 课例{ 公共静态void main(字符串[]args){ int最小正整数,i,count,rem; 计数=0; 对于(最小正数值=1;最小正数值
我不知道这是否有多大帮助,但如果在“System.out”之后添加“break”,它可以正常工作,但仍然非常慢我想要的输出是最小的正值,它可以被1到20之间的所有数字平均整除? import java.util.*; 课例{ 公共静态void main(字符串[]args){ int最小正整数,i,count,rem; 计数=0; 对于(最小正数值=1;最小正数值,java,for-loop,nested-loops,Java,For Loop,Nested Loops,我不知道这是否有多大帮助,但如果在“System.out”之后添加“break”,它可以正常工作,但仍然非常慢 import java.util.*; class Example { public static void main(String[] args){ int smallest_positive_number,i,count,rem; count =0; for(smallest_positive_number=
import java.util.*;
class Example {
public static void main(String[] args){
int smallest_positive_number,i,count,rem;
count =0;
for(smallest_positive_number=1;smallest_positive_number<2147483647; smallest_positive_number++){
for(i=1; i<=20; i++){
rem=smallest_positive_number%i;
if(rem==0){
count++;
}
}
if(count==20){
System.out.print(smallest_positive_number+" is the smallest value that is evenly divisible by all the numbers from 1 to 20");
}
count=0;
}
}
}
希望它有点有用。它似乎大部分都有效(我得到了正确的第一个结果),但是:在第一个结果之后它不会停止,而且它的效率非常低(在我的计算机上它需要28秒,而效率稍高的版本只需不到一秒)。使用windows计算器比使用你的程序更快。
if(count==20){
System.out.print(smallest_positive_number+" is the smallest value that is evenly divisible by all the numbers from 1 to 20");
break;
}