为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。
但也并非纯粹的偶然:60是个优秀的数字,它的因子比较多。
事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。
我们希望寻找到能除尽1至n的的每个数字的最小整数。
不要小看这个数字,它可能十分大,比如n=100, 则该数为:
69720375229712477164533808935312303556800
为此,有必要使用BigInteger来记录这样的大数。
请阅读下面的代码,填写缺失的部分(下划线部分)。
注意:请把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。
直接写在题面中不能得分。
import java.math.BigInteger;
public class My1
{
// 求能除尽1~n 每个数字的最小整数
public static BigInteger f(int n)
{
int[] x = new int[n+1];
for(int i=1; i<=n; i++) x[i] = i;
for(int i=2; i<n; i++)
{
for(int j=i+1; j<=n; j++)
{
if(x[j] % x[i]==0) x[j]/=x[i] ; // 填空1
}
}
BigInteger m = BigInteger.ONE;
for(int i=2; i<=n; i++)
{
m = m.multiply( BigInteger.valueOf(x[i] )); // 填空2
}
return m;
}
public static void main(String[] args)
{
System.out.println(f(30));
}
}
import java.math.BigInteger;
import java.util.Scanner;
public class 连续数的公倍数 {
// 得到最大公约数(辗转相除)
public static BigInteger gdc(BigInteger m,BigInteger n) {
BigInteger r = m.mod(n);
while(r.compareTo(BigInteger.ZERO)!=0){
m = n;
n = r;
r = m.mod(n);
}
return n;
}
public static BigInteger f(BigInteger m,BigInteger n){
if(n.compareTo(BigInteger.ONE)==0){
return m; // n到最后
}else{
m = m.multiply(n).divide(gdc(m, n)); // 得到最小公倍数
return f(m,n.subtract(BigInteger.ONE));
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("输入的 n (n<100)");
BigInteger m = scan.nextBigInteger();
System.out.println(f(m,m.subtract(BigInteger.ONE)));
}
}
package exe66_70;
//我的程序应该没问题
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class Exe69 {
static List<String> list = new ArrayList<String>();
static int number = 10;
public static void main(String[] args) {
for (int i = 1; i <= number; i++) {
dfs(i);
}
BigInteger sum = new BigInteger("1");
for (int i = 0; i < list.size(); i++) {
sum = sum.multiply(new BigInteger(list.get(i))) ;
}
BigInteger k = sum;
for (; ; k = k.add(sum)) {
for (int j = 1; j <= number; j++) {
if (k.remainder(new BigInteger(j+"")).compareTo(new BigInteger("0"))!=0) {
break;
}
if (j==number) {
System.out.println(k);
return ;
}
}
}
}
private static void dfs( int i) {
if (i==1) {
return ;
}
for (int j = 2; j <= i; j++) {
if (i%j==0) {
i/=j;
if (!list.contains(String.valueOf(j))) {
list.add(String.valueOf(j));
dfs( i);
}
}
}
}
}
支付宝扫红包,让我能够买杯咖啡,继续创作,谢谢大家!