描述:
给定一个三位整数N和一个整数K,通过增加整数N中任意一位数,最多增加K位,获得最大值
例如:
N = 512 and K = 10 ,return 972
N = 191 and K = 4 , return 591
public static int max3DigitNumSolution(int N, int K) {
if (K == 0) return N;
int result = 0;
int x = N;
int y = K;
for (int i = String.valueOf(N).length() - 1; i >= 0; i--) {
int divider = (int) Math.pow((double) 10, (double) i);
if (y == 0) {
result += x ;
break;
} else if (x / divider + y >= 9) {
result += 9 * divider;
} else {
result += (x / divider + y) * divider;
}
y = y + x / divider - 9;
y = y > 0 ? y : 0;
x = x % divider;
}
return result;
}
网友评论