美国面试题:
1. The number 124 has the property that it is the smallest number whose first three multiples contain the digit 2. Observe that
124*1 = 124, 124*2 = 248, 124*3 = 372 and that 124, 248 and 372 each contain the digit 2. It is possible to generalize this property to be the smallest number whose first n multiples each contain the digit 2. Write a function named smallest(n) that returns the smallest number whose first n multiples contain the digit 2. Hint: use modulo base 10 arithmetic to examine digits.
Its signature is int smallest(int n)
You may assume that such a number is computable on a 32 bit machine, i.e, you do not have to detect integer overflow in your answer.
2. 样例:
3.java 代码实现
public class smallestN {
public static void main(String[] args) {
// 1243==4062i=4062j=1
// 1243==8124i=4062j=2
// 1243==12186i=4062j=3
// 1243==16248i=4062j=4
// 1243==24372i=4062j=6
// 1243==28434i=4062j=7
// System.out.println(isContian2(124*4));
System.out.println(smalles(1));
}
public static int smalles(int n){
int i =0;
while (true){
i++;
if(isContian2(i)==1){
int tmp=0;
for (int j = 1; j <= n ; j++) {
if(isContian2(j*i)==1){
System.out.println("1243=="+j*i+"i="+i+"j="+j);
tmp++;
}
}
if(tmp==n){
System.out.println("tmp"+tmp);
return i;
}
}
}
}
/**
* 求一个数字是否包含2
* @param n
* @return
*/
public static int isContian2(int n){
while (true){
if(n%10==2){
return 1;
}
n = n/10;
if(n==0){
return 0;
}
}
}
}