MENU

第四届蓝桥杯决赛B组C/C++——连续奇数和

April 13, 2018 • Read: 5200 • 算法阅读设置

连续奇数和

小明看到一本书上写着:任何数字的立方都可以表示为连续奇数的和
比如:
2^3 = 8 = 3 + 5
3^3 = 27 = 7 + 9 + 11
4^3 = 64 = 1 + 3 + ... + 15
虽然他没有想出怎么证明,但他想通过计算机进行验证。
请你帮助小明写出 111 的立方之连续奇数和表示法的起始数字。如果有多个表示方案,选择起始数字小的方案。
请严格按照要求,通过浏览器提交答案。
注意:只提交一个整数,不要写其它附加内容,比如:说明性的文字。

题解

暴力就行了

代码

#include<bits/stdc++.h>
using namespace std;
const int res = 111*111*111;
int main()
{
    long long sum = 0;
    int is = 1;
    for(int i = 1;i < res/3 && is == 1;i += 2)
    {
        sum = 0;
        for(int j = i;j < res/3 && is == 1;j += 2)
        {
            sum += j;
            if(sum == res)
            {
                cout<<i<<endl;
                is = 0;
            }
            if(sum > res)
                break;
        }
    }
    return 0;
}

答案:371

Last Modified: October 7, 2018
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

3 Comments
  1. mathor mathor

    1

  2. 陌生的网友 陌生的网友

    你好啊

    1. mathor mathor

      @陌生的网友