返回题解分享
讨论 / 题解分享/ 帖子详情

幸运数字(结果填空) - 题解

public class Main {
	public static void main(String[] args) {
		int count = 0, index = 0;
		while (count < 2023) {
			index++;
			if (check(index, 2) && check(index, 8) && check(index, 10) && check(index, 16)) {
				count++;
			}
		}
		System.out.println(index);
	}

	static boolean check(int n, int x) {
		int mod = 0;
		//将n转化为x进制的字符串
		String s = Integer.toString(n, x);
		for (int i = s.length() - 1; i >= 0; i--) {
			//截取字符串每一位,并且求和
			mod += Integer.valueOf(String.valueOf(s.charAt(i)), x);
		}
		return n%mod==0;
	}
}
0 回复 0 转发 0 喜欢 1 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!