已知正整数 A 中有多个b,则由A中的b可以组成一个新数Pa. 例如: A = 3862767 , b = 6 , 则 Pa = 66 , 因为A中有 2 个 6 ; A = 1275924559 , b = 5 , 则 Pa = 555.
写一个方法 int part(int a , int b) , 输入 A 和 b , 返回 Pa , 其中 0 < A < 10^10
part(1337458543, 4) return 44;
part(1337458543, 3) return 333;
part(1337458543, 8) return 8;
part(1337458543, 2) return 0;
当时面试官提示了一下,想起来了但是没写出来可惜了
摸鱼法.
搞错了
模余法.
public class Test {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.part(1337458543,8));
}
}
class Solution{
int part(int a, int b){
int target = 0;
int end = a;
while (end != 1){
int c = end%10;
if (c == b){
target = target * 10 + c;
}
end=end/10;
}
return target;
}
}
总结一波:
其实取一个大的数的里面的数都可以使用这个方法, 摸鱼法(模余法) 很常用吧,在刷题的时候
欢迎留言讨论