题解 | #尼科彻斯定理#

尼科彻斯定理

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

#include <bits/stdc++.h>
using namespace std;

int main() {
    int m;
    while (cin >> m) { // 注意 while 处理多个 case
        // a的立方
        int a = m * m * m;

        vector<int> res;
        int y = 1;
        for (int i = 0;i < m;i++) {
            res.push_back(y);
            y += 2;
        }

        while (true) {
            // 对连续奇数求和
            int sum = accumulate(res.begin(), res.end(), 0);
            if (sum != a) {
                // 求和的结果不匹配就增加所有连续奇数的值
                for (int &e : res) {
                    e += 2;
                }
            } else {
                break;
            }
        }
        
        for (int i = 0;i < res.size() - 1;i++) {
            cout << res.at(i) << "+";
        }
        cout << res.at(res.size() - 1) << endl;
    }
}

全部评论

相关推荐

10-02 19:29
已编辑
浙江科技大学 运营
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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