算法-经典趣题-爱因斯坦阶梯问题
作者:互联网
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/79
一、问题
爱因斯坦曾经提出过这样一道有趣的数学题:
有一个长阶梯,
若每步上2阶,最后剩下1阶;
若每步上3阶,最后剩2阶;
若每步上5阶,最后剩下4阶;
若每步上6阶,最后剩5阶;
只有每步上7阶,最后刚好一阶也不剩。
请问该阶梯至少有多少阶。
二、分析
来分析一下爱因斯坦的阶梯问题。假设阶梯的个数为minNumber,按照前述的条件,minNumber应该满足如下条件:
minNumber除以2的余数为1;
minNumber除以3的余数为2;
minNumber除以5的余数为4;
minNumber除以6的余数为5;
minNumber除以7的余数为0;
很明显这个数是7的倍数,所以,从7开始,对每个7的倍数进行判断,直到寻找到一个最小的满足条件的数据为止。
三、编程
package com.joshua317;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int minNumber;
System.out.println("爱因斯阶梯问题");
Jieti jieti = new Jieti();
minNumber = jieti.getMinNum();
System.out.printf("这个阶梯至少有%d阶",minNumber);
}
}
class Jieti {
public int getMinNum() {
int minNumber = 7;
while (true) {
if (minNumber%2==1 && minNumber%3==2 && minNumber%5==4 && minNumber%6==5) {
break;
}
minNumber = minNumber+7;
}
return minNumber;
}
}
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/79
标签:minNumber%,minNumber,joshua317,每步,阶梯,算法,趣题,余数 来源: https://www.cnblogs.com/joshua317/p/15226635.html