C#练习题答案: 建立一堆立方体【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

建立一堆立方体【难度: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;
  }
}

答案2:

using System;

public class ASum {
  
  public static long findNb(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:

public class ASum {
  
  public static long findNb(long m) 
  {
    long i = 1;
    while (m > 0)
        m -= i *i* i++;
    return m == 0 ? --i : -1;
  }
  
}

答案4:

using System;

public class ASum {
  
        public static long findNb(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:

public class ASum
{
    public static long findNb(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;
public class ASum {
  
  public static long findNb(long m) {
    long res=0;
    int cnt=0;
    while(res<m)
    {
      cnt++;
      res += (long)Math.Pow(cnt,3);      
    }
    if(res==m)
      return cnt;
    else
      return -1;
  }
  
}

答案7:

using System;

public class ASum {
  // 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 square
  public static Int64 findNb(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;
  }
  
  private static decimal Sqrt(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;

public class ASum {
  
  public static long findNb(long m) {
    int i = 0;
            while (m > 0)
            {
                i++;
                m = m - (long)Math.Pow(i, 3);
            }
            return m == 0 ? i : -1;
    
  }
  
}

答案9:

using System;

public class ASum {
  
  public static long findNb(long m) {
    double sqrtM = Math.Sqrt(m);
    long lsqrtM = (long)sqrtM;
    if(sqrtM%1!=0 || (lsqrtM * lsqrtM) != m) return -1;
    return ((long)Math.Sqrt(8*lsqrtM+1) - 1) / 2;
  }
  
}

答案10:

using System;
public class ASum 
{
  /* 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.
   */
  public static long findNb(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;
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值