public class Main {
public static void main(String[] args) {
int x = 2019;int i = 1;
while ( loseCondition(x) ){
i++;
x = x * i;
}
System.out.println(x);
}
public static boolean loseCondition(int x){
boolean order = false;
while ( x > 0 ){
int k = x % 10;//依次取最后一位
if ( k % 2 == 0 ){
order = true;
break;
}
x /= 10;//依次去掉最后一位
}
return order;
}
}
public class Main {
public static void main(String[] args) {
int x = 2019;
while ( loseCondition(x) ){
x += 2019;
}
System.out.println(x);
}
public static boolean loseCondition(int x){
boolean order = false;
while ( x > 0 ){
int k = x % 10;//依次取最后一位
if ( k % 2 == 0 ){
order = true;
break;
}
x /= 10;//依次去掉最后一位
}
return order;
}
}
代码求解的问题是:
算出最小的整数X,且同时满足:(1)X 是 2019 的整倍数;(2)X 的每一位数字都是奇数。
为什么第一个解法不行?
1、x=x * i,第一次 是2019 * 1=2019,第二次是 2019 * 2=4038 第三次是 4038 * 3=12114,中间不全是相差2019,会丢失值
2、x += 2019; 第一次 是2019+2019=4038,第二次是 4038+2019=6057 第三次是 6057+2019=8076 中间相差2019
所以第一种不行,第二种可以;
你把第一个的循环走一遍就知道了
第一次循环:x = 2019 , i = 2 ; x = 2019 * 2 = 4038 =》 2019 * 2
第二次循环:x = 4038 , i = 3 ; x = 4038 * 3 = 12114 =》 2019 * 6
中间跳过了 2019的 3,4,5倍
把2019作为一个固定常量,不需要改变它,每次乘i+1