题解 | #尼科彻斯定理#

尼科彻斯定理

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);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int m = in.nextInt();
            int cube = m * m * m;
            int sum = 0;
            String factor = "";
            int count = 1;
            int pivot = cube / m;
            String result = "";
            if (pivot % 2 == 0) {
                //在pivot的前后各取m/2个奇数:不包含pivot
                //前m/2-1个
                for (int i = m / 2 - 1; i >= 1; i--) {
                    int temp = (pivot - 1) - 2 * i;
                    result += temp + "+";
                }
                result += (pivot - 1) + "+" + (pivot + 1) ;
                //后m/2-1个
                for (int i = 1; i <= m / 2 - 1; i++) {
                    int temp = (pivot + 1) + 2 * i;
                    result += "+" + temp;

                }
                System.out.println(result);
            } else {
                //在pivot的前后各取m-1/2个奇数,包含pivot
                for (int i = (m-1) / 2; i >= 1; i--) {
                    int temp = pivot - 2 * i;
                    result += temp + "+";
                }
                result += pivot;
                //后m/2-1个
                for (int i = 1; i <= (m-1) / 2; i++) {
                    int temp = pivot + 2 * i;
                    result += "+" + temp;

                }
                System.out.println(result);
            }
        }
    }
}

蠢办法

全部评论

相关推荐

学历算污点吗?
RobertWeicc:这群人真的醉了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
asdasdasdasdas:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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