publicclassASum{publicstaticlongfindNb(long m){long total =1, i =2;for(; total < m; i++) total += i *i* i;return total == m ? i -1:-1;}}
答案2:
using System;publicclassASum{publicstaticlongfindNb(long m){long n =(long)Math.Floor(Math.Sqrt(2*Math.Sqrt(m)));return((n *(n +1))/2)*((n *(n +1))/2)== m ? n :-1;}}
答案3:
publicclassASum{publicstaticlongfindNb(long m){long i =1;while(m >0)
m -= i *i* i++;return m ==0?--i :-1;}}
答案4:
using System;publicclassASum{publicstaticlongfindNb(long totalVolume){int n;long volumeSoFar =0;for(n =0; volumeSoFar < totalVolume;){
n++;
volumeSoFar +=(long)Math.Pow(n,3);}return volumeSoFar == totalVolume
? n
:-1;}}
答案5:
publicclassASum{publicstaticlongfindNb(long m){long n, i;for(n =0, i =1; n < m; i++)
n += i *i* i;return n == m ? i -1:-1;}}
答案6:
using System;publicclassASum{publicstaticlongfindNb(long m){long res=0;int cnt=0;while(res<m){
cnt++;
res +=(long)Math.Pow(cnt,3);}if(res==m)return cnt;elsereturn-1;}}
答案7:
using System;publicclassASum{// this is based on the formula that the sum of the first n cubes equals (n * (n + 1) / 2) ^ 2// also, the sum of the first n cubes is always a squarepublicstaticInt64findNb(Int64 Input){var inputSqrt =2*Sqrt(Input);var result =Sqrt((Int64)inputSqrt);return(inputSqrt %1!=0)||(result %1==0)?-1:((Int64)result *((Int64)result +1)== inputSqrt)?(Int64)result :-1;}privatestaticdecimalSqrt(Int64 Input){var sqrt =(decimal)Math.Sqrt(Input);for(int index =0; index <10;++index){
sqrt =0.5m *(sqrt + Input / sqrt);}return sqrt;}}
答案8:
using System;publicclassASum{publicstaticlongfindNb(long m){int i =0;while(m >0){
i++;
m = m -(long)Math.Pow(i,3);}return m ==0? i :-1;}}
using System;publicclassASum{/* m is equal to sum of i^3 for i=0 to n which is equal to (n*(n-1)/2)^2.
* Thus if n^2 + n - 2*sqrt(m) = 0 has a positive integer solution, we
* are done.
*/publicstaticlongfindNb(long m){long squareM =(long) Math.Sqrt(m);if(squareM*squareM != m)return-1;long delta =1+8* squareM;long squareDelta =(long) Math.Sqrt(delta);if(squareDelta*squareDelta != delta)return-1;return(squareDelta -1)/2;}}
建立一堆立方体【难度:2级】:答案1:public class ASum { public static long findNb(long m) { long total = 1, i = 2; for(; total < m; i++) total += i *i* i; return total == m ? i - 1 : -1; }}答...