Java 采访:在两个整数的范围内找到整个立方体

Java 采访:在两个整数的范围内找到整个立方体,java,Java,我刚刚就Codibility做了一次编码采访 我被要求实现以下内容,但我无法在20分钟内完成,现在我在这里从这个社区获得想法 编写一个函数public int-thill\u-cubes\u count(int-a,int-B),它应该返回范围内的整个多维数据集 例如,如果A=8和B=65,则范围内所有可能的多维数据集都是2^3=8、3^3=27和4^3=64,因此函数应返回count 3 我不知道如何将数字识别为整个立方体。我如何解决这个问题 A和B的范围为[-20000到20000] 这就是

我刚刚就Codibility做了一次编码采访

我被要求实现以下内容,但我无法在20分钟内完成,现在我在这里从这个社区获得想法

编写一个函数
public int-thill\u-cubes\u count(int-a,int-B)
,它应该返回范围内的整个多维数据集

例如,如果A=8和B=65,则范围内所有可能的多维数据集都是2^3=8、3^3=27和4^3=64,因此函数应返回count 3

我不知道如何将数字识别为整个立方体。我如何解决这个问题

A和B的范围为[-20000到20000]

这就是我试过的

import java.util.Scanner;
class Solution1 {
  public int whole_cubes_count ( int A,int B ) {
      int count =0;

    while(A<=B)
    {
        double v = Math.pow(A, 1 / 3); // << What goes here?
        System.out.println(v);
        if (v<=B)
            {
            count=count+1;
            }
        A =A +1;
    }
    return count ;
  }

  public static void main(String[] args) 
  {
    System.out.println("Enter 1st Number");
    Scanner scan = new Scanner(System.in);
    int s1 = scan.nextInt();
    System.out.println("Enter 2nd Number");
    //Scanner scan = new Scanner(System.in);
    int s2 = scan.nextInt();
    Solution1 n = new Solution1();
     System.out.println(n.whole_cubes_count (s1,s2));
  }
}
import java.util.Scanner;
类别解决方案1{
公共整数整立方数(整数A,整数B){
整数计数=0;

而(A对于正立方体:

i = 1
while i^3 < max
    ++i
i=1
而我^3
与负立方体类似,但在比较中具有绝对值


为了使这更一般化,您需要找到
i
的值,其中
i^3>=min
,在
min
max
都是正的情况下。如果
min
max
都是负的,类似的解决方案也会起作用。

如果你只有20分钟的时间,那么他们不应该期待超级优化的代码。所以不要尝试。按照系统的约束条件,即范围仅为+20000到-20000。你知道多维数据集的值必须在27之内,因为27*27*27=19683

public int whole_cubes_count(int a, int b) {
    int count = 0;
    int cube;
    for (int x = -27; x <= 27; x++) {
        cube = x * x * x;
        if ((cube >= a) && (cube <= b))
            count++;
    }
    return count;
}
public整数整立方计数(整数a、整数b){
整数计数=0;
整数立方;
对于(intx=-27;x=a)和&(cube
intcountnoofcubes)(inta,intb){
整数计数=0;
for(int startsCube=(int)Math.ceil(Math.cbrt(a));Math.pow(

startsCube,3.0)好吧,它可以用O(1)复杂度来计算,我们需要找到适合这个范围的最大立方体和最小立方体。介于两者之间的立方体显然也都在这个范围内

def n_cubes(A, B):
    a_cr = int(math.ceil(cube_root(A)))
    b_cr = int(math.floor(cube_root(B)))

    if b_cr >= a_cr:
        return b_cr - a_cr + 1
    return 0

只需确保您的多维数据集\u根返回实际多维数据集的整数。按gist完成解决方案@Tim建议的解决方案比@Erick提供的解决方案快,尤其是当A…B范围增大时。 让我在这里引用github的观点: “我们可以注意到,对于任何x>y,x³>y³。(这称为单调函数) 因此,对于位于∛A.≤ x≤ ∛B、 立方体适合:A≤ x³≤ B

所以要得到A..B中的立方体数,只需计算A..B之间的整数数∛A和∛两个数之间的整数数就是它们的差。”

它似乎完全正确,不是吗?它适用于任何电源,而不仅仅适用于立方体。 以下是我的java多维数据集根端口方法:

/*
 * make sure your cube_root returns integers for actual cubes
 */
static double cubeRoot(int x) {
    //negative number cannot be raised to a fractional power
    double res = Math.copySign(Math.pow(Math.abs(x), (1.0d/3)) , x);
    long rounded_res = symmetricRound(res);
    if (rounded_res * rounded_res * rounded_res == x)
        return rounded_res;
    else
        return res;
}

private static long symmetricRound( double d ) {
    return d < 0 ? - Math.round( -d ) : Math.round( d );
}
/*
*确保多维数据集的根返回实际多维数据集的整数
*/
静态双立方体(int x){
//负数不能提升为分数次幂
double res=Math.copySign(Math.pow(Math.abs(x),(1.0d/3)),x);
长圆形_res=对称性(res);
if(四舍五入分辨率*四舍五入分辨率*四舍五入分辨率==x)
返回四舍五入;
其他的
返回res;
}
专用静态长对称域(双d){
返回d<0?-Math.round(-d):Math.round(d);
}

我知道java中的Math.cbrt,但使用Math.pow方法很容易将解决方案推广到其他指数。

如何解决这个问题是我的问题,我现在发布我的代码,谢谢请用四个空格缩进代码行。谢谢你用两个空格缩进代码行。@ErickRobertson好吧,我指的是显示ay格式,而不是代码本身的格式。告诉潜在雇主,
public int-whole\u cubes\u count(inta,intb)
不符合公认的Java风格指导原则。是的,他们给了我的范围是和,你只需要从-27循环到27。(27^3=19683)当只有55个可能的多维数据集时,您只需对一个数字进行多维数据集运算,算法不会花费太长时间。是的,您是对的,但这是一个简单的更改,可能已经完成了。无论如何,您有我的+1。@a.R.s.我使用了您的数字:)请注意,它并不是真的适用于所有幂次,只适用于奇数幂次(1,3,5..)。对于偶数幂(2,4,6),你不能只求边界的根并计算差,你必须考虑负数。例如,对于在[-1…17]范围内寻找正方形的任务,适合的数字应该是[-4,-3,-2,-1,0,1,2,3,4]
/*
 * make sure your cube_root returns integers for actual cubes
 */
static double cubeRoot(int x) {
    //negative number cannot be raised to a fractional power
    double res = Math.copySign(Math.pow(Math.abs(x), (1.0d/3)) , x);
    long rounded_res = symmetricRound(res);
    if (rounded_res * rounded_res * rounded_res == x)
        return rounded_res;
    else
        return res;
}

private static long symmetricRound( double d ) {
    return d < 0 ? - Math.round( -d ) : Math.round( d );
}