Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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
Arrays 给定一个数组,其中arr[i]=i-1,使用以下方法,输出是什么?_Arrays_Algorithm_Methods - Fatal编程技术网

Arrays 给定一个数组,其中arr[i]=i-1,使用以下方法,输出是什么?

Arrays 给定一个数组,其中arr[i]=i-1,使用以下方法,输出是什么?,arrays,algorithm,methods,Arrays,Algorithm,Methods,我得到了以下用伪代码编写的方法 for i=1 to floor(n/2) if arr[i] != 0 then for(j=2 to floor(n/i) arr[i*j] = 0 我需要找到输出并证明它确实是输出。 到目前为止,我试图用Java编写代码,并尝试不同的输入和数组大小,但没有效果。 如果有任何帮助,请将其放在此处: public class Checking { private static int method(int[] A

我得到了以下用伪代码编写的方法

for i=1 to floor(n/2)
    if arr[i] != 0 then
       for(j=2 to floor(n/i)
         arr[i*j] = 0
我需要找到输出并证明它确实是输出。

到目前为止,我试图用Java编写代码,并尝试不同的输入和数组大小,但没有效果。 如果有任何帮助,请将其放在此处:

public class Checking
{
    private static int method(int[] A,int n)
    {
        for (int i=1;i<=java.lang.Math.floor(n/2);i++)
        {
            if(A[i] != 0)
            {
                for(int j=2;j<=java.lang.Math.floor(n/i);j++)
                {
                    A[i*j]=0;
                    System.out.println("The index ofA["+i*j+"] became "+A[i*j]);
                }
            }

            //System.out.print(", "+A[i]);
        }

        for (int i=1;i<=java.lang.Math.floor(n/2);i++)
        {
            System.out.print(", "+A[i]);
        }

        return 0;
    }

    public static void main(String[] args)
    {
        int[] A = {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
        System.out.println(method(A,20));
    }
}
公共类检查
{
私有静态int方法(int[]A,int n)
{

对于(int i=1;i您可以更改以下内容:

在主方法中,删除

System.out.println(method(A,20));
因为当您从方法()返回0时,它将始终打印0

打印方法()中的所有内容时,请更改以下内容(因为它不打印完整数组,而是从索引1(缺少索引0)打印到楼层n/2)


您好,谢谢您的回答,但是缺少第一个索引是故意的,正如伪代码所指示的(对我来说也很奇怪)。其余的是固定的,但我的问题仍然没有解决:(伪代码解释了如何更新数组。第二个循环(从索引0开始)显示如何打印数组。因此,您使用伪代码来更新A,但需要一些代码来打印所有数组日期。我使用最简单的数组打印解决方案更新了答案。您的问题是什么?您的代码是否给出错误?在这种情况下,请包含错误消息。它是否给出错误的输出?在这种情况下,请包含预期和实际的o输出及其差异。
for (int i=1;i<=java.lang.Math.floor(n/2);i++)
        {
            System.out.print(", "+A[i]);
        }
for (int i=0;i<A.length; i++)
        {
            System.out.print(", "+A[i]);
        }
System.out.println(Arrays.toString(A));