题解 | #尼科彻斯定理#

尼科彻斯定理

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

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) {
            int n = in.nextInt();

            for (int k = 1; ; k += 2) {
                List<Integer> subList = getSubList(k, n);
                int sum = subList.stream().mapToInt(e -> e).sum();
                if (sum == Math.pow(n, 3)) {
                    String collect = subList
                                     .stream()
                                     .map(String::valueOf)
                                     .collect(Collectors.joining("+"));

                    System.out.println(collect);
                    return;
                }
            }
        }
        in.close();
    }

    public static List<Integer> getSubList(int from, int size) {
        List<Integer> integers = new ArrayList<>();
        int first = from;
        for (int i = 0; i < size; i++) {
            integers.add(first);
            first += 2;
        }
        return integers;
    }
}

全部评论

相关推荐

青春运维少年不会梦到...:实习大王
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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