题目解析
给定年份 和 要求拥有不同的数位
找出距离当前年份来说 第一个符合要求拥有不同数位的年份
解题思路
利用 String.format 来进行格式化数字
利用set去重 看是否符合要求拥有不同数位
然后 直接 where 循环找到第一个不同 输出 距离当前年份有多久 和 输出找到的年份即可
代码
import java.io.*;
import java.util.*;
public class Main
{
static boolean is(int n, int k)
{
char s[] = String.format("%04d", n).toCharArray();
TreeSet<Character> tr = new TreeSet<Character>();
for (int i = 0; i < 4; i++)
tr.add(s[i]);
return tr.size() == k;
}
public static void main(String[] args)
{
int n = sc.nextInt();
int k = sc.nextInt();
int cnt = 0;
while (!is(n, k))
{
cnt++;
n++;
}
out.printf("%d %04d", cnt, n);
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}