zoukankan      html  css  js  c++  java
  • Build a pile of Cubes

    Instructions
    Output
    Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3.

    You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build?

    The parameter of the function findNb (find_nb, find-nb, findNb) will be an integer m and you have to return the integer n such asn^3 + (n-1)^3 + ... + 1^3 = mif such a n exists or -1 if there is no such n.

    Examples:
    findNb(1071225) --> 45
    findNb(91716553919377) --> -1

    #include"stdio.h"
    #include"math.h"
    int fun(int n){
        int i=1, sum=0; 
        while(sum<n){
           if(sum<n){
              sum+=pow(i, 3);
           }else if(sum==n){
              return i;
           }else{
              return -1;
           }
           printf("%d ", i);
           i++;
        }
    }
    main(){
       printf("%d", fun(1071225));
    }
    

    ❤️有则改之,无则加勉。如有错误、建议、疑问,评论或联系飞沙QQ:2602629646
    ❤️本文来自作者:MrFlySand,转载请注明原文链接:https://www.cnblogs.com/MrFlySand/p/14145020.html

  • 相关阅读:
    第七次上机练习
    第六次作业
    第六次上机练习
    第五次作业
    第五次上机练习
    第四次作业
    第四次上机练习
    第三次上机练习
    第三次作业
    第十一周上机练习
  • 原文地址:https://www.cnblogs.com/MrFlySand/p/14145020.html
Copyright © 2011-2022 走看看