多多君最近在研究某种数字组合:
定义为:每个数字的十进制表示中(0~9),每个数位各不相同且各个数位之和等于N。
满足条件的数字可能很多,找到其中的最小值即可。
多多君还有很多研究课题,于是多多君找到了你--未来的计算机科学家寻求帮助。
输入例子:5
输出例子:5
例子说明:符合条件的数字有:5,14,23,32,41
其中最小值为5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans = 0;
int digit = 0; // 当前正在构造的数位
if (n > 45) {
ans = -1;
} else if (n < 10) {
ans = n;
} else {
for (int i = 9; i > 0; i--) {
if (n != 0 && i <= n) {
n -= i;
ans += (int)Math.pow(10, digit) * i;
digit++;
}
}
}
System.out.println(ans);
}
}