Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何确定数字是否是智能数字?_Java_Arrays - Fatal编程技术网

在java中,如何确定数字是否是智能数字?

在java中,如何确定数字是否是智能数字?,java,arrays,Java,Arrays,我想解决这个问题。在过去的4个小时里,我一直在尝试编码 如果整数是无限序列中的元素,则将其定义为智能数 1、2、4、7、11、16…… 注意,2-1=1,4-2=2,7-4=3,11-7=4,16-11=5,因此对于k>1,序列的第k个元素等于k-1个元素+k-1。例如,对于k=6,16是第k个元素,等于11(第k-1个元素)+5(k-1)。 编写名为isSmart的函数,如果其参数是智能数,则返回1,否则返回0。所以isSmart(11)返回1,isSmart(22)返回1,isSmart(8

我想解决这个问题。在过去的4个小时里,我一直在尝试编码

如果整数是无限序列中的元素,则将其定义为智能数
1、2、4、7、11、16……
注意,2-1=1,4-2=2,7-4=3,11-7=4,16-11=5,因此对于k>1,序列的第k个元素等于k-1个元素+k-1。例如,对于k=6,16是第k个元素,等于11(第k-1个元素)+5(k-1)。 编写名为isSmart的函数,如果其参数是智能数,则返回1,否则返回0。所以isSmart(11)返回1,isSmart(22)返回1,isSmart(8)返回0

我已尝试使用以下代码

import java.util.Arrays;

public class IsSmart {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x = isSmart(11);
        System.out.println(x);
    }

    public static int isSmart(int n) {
        int[] y = new int[n];
        int j = 0;

        for (int i = 1; i <= n; i++) {
            y[j] = i;
            j++;
        }

        System.out.println(Arrays.toString(y));

        for (int i = 0; i <= y.length; i++) {
            int diff = 0;
            y[j] = y[i+1] - y[i] ;
            y[i] = diff;
        }

        System.out.println(Arrays.toString(y));

        for (int i = 0; i < y.length; i++) {
            if(n == y[i])
                return 1;
        }

        return 0;
    }
}
导入java.util.array;
公共类IsSmart{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
int x=isSmart(11);
系统输出println(x);
}
公共静态int isSmart(int n){
int[]y=新的int[n];
int j=0;

对于(int i=1;i,这里有一个简单的方法来考虑它,让您开始—您需要填写while()循环。需要注意的重要一点是:

序列的下一个值将是序列中的项目数+序列中的最后一个项目。

import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        System.out.println(isSmart(11));
    }

    public static int isSmart(int n) {
        ArrayList<Integer> sequence = new ArrayList<Integer>();

        // Start with 1 in the ArrayList
        sequence.add(1);

        // You need to keep track of the index, as well as
        // the next value you're going to add to your list
        int index = 1; // or number of elements in the sequence
        int nextVal = 1;

        while (nextVal < n) {
            // Three things need to happen in here:

            // 1) set nextVal equal to the sum of the current index + the value at the *previous* index

            // 2) add nextVal to the ArrayList

            // 3) incriment index by 1
        }

        // Now you can check to see if your ArrayList contains n (is Smart)
        if (sequence.contains(n)) { return 1; }

        return 0;
    }
}
import java.util.ArrayList;
公开课考试{
公共静态void main(字符串[]args){
系统输出打印LN(isSmart(11));
}
公共静态int isSmart(int n){
ArrayList序列=新的ArrayList();
//从ArrayList中的1开始
顺序.增加(1);
//您需要跟踪索引,以及
//要添加到列表中的下一个值
int index=1;//或序列中的元素数
int nextVal=1;
while(nextVal
以下是一种简单的方法,可以让您开始使用它-您需要填写while()循环。需要注意的重要一点是:

序列的下一个值将是序列中的项目数+序列中的最后一个项目。

import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        System.out.println(isSmart(11));
    }

    public static int isSmart(int n) {
        ArrayList<Integer> sequence = new ArrayList<Integer>();

        // Start with 1 in the ArrayList
        sequence.add(1);

        // You need to keep track of the index, as well as
        // the next value you're going to add to your list
        int index = 1; // or number of elements in the sequence
        int nextVal = 1;

        while (nextVal < n) {
            // Three things need to happen in here:

            // 1) set nextVal equal to the sum of the current index + the value at the *previous* index

            // 2) add nextVal to the ArrayList

            // 3) incriment index by 1
        }

        // Now you can check to see if your ArrayList contains n (is Smart)
        if (sequence.contains(n)) { return 1; }

        return 0;
    }
}
import java.util.ArrayList;
公开课考试{
公共静态void main(字符串[]args){
系统输出打印LN(isSmart(11));
}
公共静态int isSmart(int n){
ArrayList序列=新的ArrayList();
//从ArrayList中的1开始
顺序.增加(1);
//您需要跟踪索引,以及
//要添加到列表中的下一个值
int index=1;//或序列中的元素数
int nextVal=1;
while(nextVal
可以用以下更简单的方法完成

import java.util.Arrays;
public class IsSmart {

    public static void main(String[] args) {
        int x = isSmart(11);
        System.out.println("Ans: "+x);
    }

    public static int isSmart(int n) {

         //------------ CHECK THIS LOGIC ------------//
        int[] y = new int[n];
        int diff = 1;

        for (int i = 1; i < n; i++) {
            y[0] =1;
            y[i] = diff + y[i-1];
            diff++;
        }
       //------------ CHECK THIS LOGIC ------------//

        System.out.println(Arrays.toString(y));
        for (int i = 0; i < y.length; i++) {
            if(n == y[i])
                return 1;
        }

        return 0;
    }
}
导入java.util.array;
公共类IsSmart{
公共静态void main(字符串[]args){
int x=isSmart(11);
System.out.println(“Ans:+x”);
}
公共静态int isSmart(int n){
//------------检查这个逻辑------------//
int[]y=新的int[n];
int-diff=1;
对于(int i=1;i
可以用以下更简单的方法完成

import java.util.Arrays;
public class IsSmart {

    public static void main(String[] args) {
        int x = isSmart(11);
        System.out.println("Ans: "+x);
    }

    public static int isSmart(int n) {

         //------------ CHECK THIS LOGIC ------------//
        int[] y = new int[n];
        int diff = 1;

        for (int i = 1; i < n; i++) {
            y[0] =1;
            y[i] = diff + y[i-1];
            diff++;
        }
       //------------ CHECK THIS LOGIC ------------//

        System.out.println(Arrays.toString(y));
        for (int i = 0; i < y.length; i++) {
            if(n == y[i])
                return 1;
        }

        return 0;
    }
}
导入java.util.array;
公共类IsSmart{
公共静态void main(字符串[]args){
int x=isSmart(11);
System.out.println(“Ans:+x”);
}
公共静态int isSmart(int n){
//------------检查这个逻辑------------//
int[]y=新的int[n];
int-diff=1;
对于(int i=1;i
首先考虑一个数学解决方案

智能数字形成一个序列:

  • a0=1
  • an+1=n+an
这为智能数字提供了一个功能:

  • f(x)=ax²+bx+c
  • f(x+1)=f(x)+x=
所以问题是为给定的y找到一个匹配的x。 您可以通过二进制搜索来实现这一点

int isSmart(int n) {
    int xlow = 1;
    int xhigh = n; // Exclusive. For n == 0 return 1.
    while (xlow < xhigh) {
        int x = (xlow + xhigh)/2;
        int y = f(x);
        if (y == n) {
            return 1;
        }
        if (y < n) {
            xlow = x + 1;
        } else {
            xhigh = x;
        }
    }
    return 0; 
}
首先想一个数学解

智能数字形成一个序列:

  • a0=1
  • an+1=n+an
这为智能数字提供了一个功能:

  • f(x)=ax²+bx+c
  • f(x+1)=f(x)+x=
所以问题是为给定的y找到一个匹配的x。 您可以通过二进制搜索来实现这一点

int isSmart(int n) {
    int xlow = 1;
    int xhigh = n; // Exclusive. For n == 0 return 1.
    while (xlow < xhigh) {
        int x = (xlow + xhigh)/2;
        int y = f(x);
        if (y == n) {
            return 1;
        }
        if (y < n) {
            xlow = x + 1;
        } else {
            xhigh = x;
        }
    }
    return 0; 
}

问题之一是数组的填充方式

可以这样填充阵列

for(int i = 0; i < n; i++) {
  y[i] = (i == 0) ? 1 : y[i - 1] + i;
}

问题之一是数组的填充方式

可以这样填充阵列

for(int i = 0; i < n; i++) {
  y[i] = (i == 0) ? 1 : y[i - 1] + i;
}

请注意,您不需要构建阵列:

public static int isSmart(int n) {
    int smart = 1;
    for (int i = 1; smart < n; i++) {
        smart = smart + i;
    }
    return smart == n ? 1 : 0;
}
publicstaticintissmart(intn){
int smart=1