【Codewars】Build a pile of Cubes

19 篇文章 0 订阅
17 篇文章 0 订阅

Codewars里的 6kyu Kata。

题目说明:

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 as n^3 + (n-1)^3 + ... + 1^3 = m if such a n exists or -1 if there is no such n.

Examples:

findNb(1071225) --> 45
findNb(91716553919377) --> -1
mov rdi, 1071225
call find_nb            ; rax <-- 45

mov rdi, 91716553919377
call find_nb            ; rax <-- -1

题目的要求是给你一个数 (long) 让你判断它是否为一个立方和,如:

  • 1 -> true -> return 1
  • 8 -> false -> return -1
  • 9 -> true -> return 2

这就需要使用到数学的知识,当然更简单的方法就是直接循环,直到 sum 值大于等于目标值 m 时,退出循环判断。依据结果返回答案。

解题代码:

public class ASum {
    public static long findNb(long m) {
        // your code
        //return (long)((Math.pow((1+8*Math.pow(m,0.5)),0.5)-1)/2) == ((Math.pow((1+8*Math.pow(m,0.5)),0.5)-1)/2) ? (long)((Math.pow((1+8*Math.pow(m,0.5)),0.5)-1)/2) : -1;
        long mm = 0;
        long n = 0;
        while(mm < m){
            n = n + 1;
            mm = mm + n * n * n;
        }
        if(mm == m){
            return n;
        }
        return -1;
    }  
}

个人总结:

题目不难,但是我想用一行代码解决,但是受限于精度的问题,不能通过所有的测试实例 (2147815493455852901L 与 2147815493455852900L 计算下来最后的结果是相等的),但还有一种方式:我们可以先对 m 开方,这个时候 m 值就大大的减小了,可以减少计算的负担,也可以提高一些精度。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值