这道题考察的是找规律

尼科彻斯定理

https://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int start = 1;
        /**
         * 规律为:开始的奇数=上一个数+从0开始到该数时一直累加2的数
         * 1 1 1+0
         * 2 3 1+0+2
         * 3 7 1+0+2+4
         * 4 13 1+0+2+4+6
         */
        // 注意累加时是从0开始,所以为i < n
        for (int i = 0, j = 0; i < n; i++, j += 2) {
            start += j;
        }
        StringBuilder builder = new StringBuilder();
        builder.append(start);

        int result = start;
        // n的3次方
        int pow = (int) Math.pow(n, 3);
        while (result != pow) {
            // 奇数递增2为下一个奇数
            start += 2;
            // 每次加下一个奇数
            result += start;

            builder.append("+").append(start);
        }
        System.out.println(builder);
    }
}

全部评论

相关推荐

面了100年面试不知...:被割穿了才想起来捞人了
投递哔哩哔哩等公司6个岗位
点赞 评论 收藏
分享
饿魔:看到在线简历了吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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