百度研发A卷9.7 第二题完美数,第三题求解

public class Bd02 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            long x = in.nextLong();
            long tmp = x, d = 1, low = 0;
            while (tmp > 0) {
                low = low + (tmp % 10) * d;
                if (tmp % 10 == 0) {
                    tmp--;
                    continue;
                }
                d *= 10;
                tmp /= 10;
            }
            System.out.println(low);
            System.out.println(getInt(low));
        }

        in.close();
    }

    public static String getInt(long x) {
        String s = String.valueOf(x);
        int n = s.length();
        StringBuilder sb = new StringBuilder();
        boolean pre = false;
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if (c > '3' || pre) {
                sb.append(3);
                pre = true;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
第二题A了,还算顺利吧,第三题感觉就是组合问题,不同组合对应着不同数量(2^n-1)。。。只有15%#百度##笔试题目#
全部评论

相关推荐

投递荣耀终端等公司7个岗位 > 荣耀求职进展汇总
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务