百度研发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%#百度##笔试题目#
全部评论

相关推荐

06-25 16:00
武汉大学 Java
工科研究生底薪工资就开3k啊??
机械打工仔:写文章提成的岗位工资低,你怪工科?
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务