题解 | 尼科彻斯定理

尼科彻斯定理

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        int start = 1;
        if (num == 1) {
            System.out.println("1");
        } else if (num == 2) {
            System.out.println("3+5");
        } else {
            if (num % 2 == 0) {
                int middle = num * num - 1;
                start = middle - (num / 2 - 1) * 2;
            } else {
                int middle = num * num;
                start = middle - (num / 2 * 2);
            }

            StringBuffer result = new StringBuffer(String.valueOf(start));
            for (int j = 1; j < num; j++) {
                result.append("+").append(start + 2 * j);
            }

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

全部评论

相关推荐

03-06 18:20
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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