/**
*
*/
import static java.
lang.
System.
out;
import java.util.ArrayList;
import java.util.List;
/**
* @description 0~9 挑k個數字, 組出最接近 A 的數字
* input(A, k)
* 1 <= A <= 10^15, 1<=k<=10
*
*
*/
public class Main {
/**
* @param args
*/
public static void main
(String[] args
) {
// long A = 3355798521L;
// int k = 10;
// long A = 262004L;
// int k = 2;
// long A = 8000L;
// int k = 1;
// long A = 1000L;
// int k = 1;
// long A = 2243L;
// int k = 2;
long A = 88449L;
int k = 2;
// long A = 123456789L;
// int k = 1;
out.println(input(A, k));
}
private static long input(long A, int k) {
List<Long> listCandidates = new ArrayList<Long>();
int len = strA.codePointCount(0, strA.length());
for(int i = len - 1; i >= 0; --i) {
List<Integer> listHead = new ArrayList<Integer>();
List<Integer> listTail = new ArrayList<Integer>();
for(int j = 0; j < i; ++j) {
listHead.
add(new Integer(strA.
codePointAt(j
) - '0')); }
for(int j = i; j < len; ++j) {
listTail.
add(new Integer(strA.
codePointAt(j
) - '0')); }
//int lenTail = listTail.size();
long mid = listIntegerToLong(listTail).longValue();
for(long lower = mid; lower >= 0L; --lower) {
List<Integer> listLower = longConnectToList(listHead, lower, len);
if(countDigits(listLower).size() <= k) {
listCandidates.add(listIntegerToLong(listLower));
break;
}
}
for(int j = 0; j < listTail.size(); ++j) {
sb.append("9");
}
long mask = 0L;
if(sb.length() > 0) {
mask
= Long.
parseLong(sb.
toString()); }
for(long higher = mid + 1; higher <= mask; ++higher) {
List<Integer> listLower = longConnectToList(listHead, higher, len);
if(countDigits(listLower).size() <= k) {
listCandidates.add(listIntegerToLong(listLower));
break;
}
}
}
long answer = -1L;
if(listCandidates.size() > 0) {
answer = listCandidates.get(0);
long distance
= Math.
abs(answer
- A
); for(int i = 1; i < listCandidates.size(); ++i) {
long tmp
= Math.
abs(listCandidates.
get(i
) - A
); if(tmp < distance) {
distance = tmp;
answer = listCandidates.get(i);
}
}
}
return answer;
}
/**
* 回傳listOrig用到的數字List
*
* @param List<Integer>
* @return List<Integer>
*/
private static List<Integer> countDigits(List<Integer> listOrig) {
boolean ary[] = new boolean[10];
List<Integer> list = new ArrayList<Integer>();
int x = i.intValue();
if(x < 10 && false == ary[x]) {
ary[x] = true;
}
}
return list;
}
/**
* 將List<Integer>轉換成Long
*
* @param listI
* @return
*/
private static Long listIntegerToLong
(List
<Integer
> listI
) { long ret = 0L;
long t = 1L;
for(int i = listI.size() - 1; i >= 0; --i) {
ret += t * listI.get(i).longValue();
t *= 10L;
}
}
/**
* 把尾數串接到List後
*
* @param listI
* @param value
* @param sz
* @return
*/
private static List<Integer> longConnectToList(List<Integer> listI, long value, int sz) {
List<Integer> list = new ArrayList<Integer>();
if(sz > 0) {
for(int i = 0; i < sz; ++i) {
}
while(sz > 0) {
if(value > 0) {
long mod = value % 10;
list.
set(sz
- 1,
new Integer((int)mod
)); value /= 10;
} else if(sz - 1 < listI.size()) {
list.
set(sz
- 1,
new Integer(listI.
get(sz
- 1).
intValue())); }
--sz;
}
while(list.size() > 1 && list.get(0).intValue() == 0) {
list.remove(0);
}
}
return list;
}
}