1

I have been struggling with this java problem for several days now, and now I have to give up. I have been told the answer which should be 5 5 3 3, but I cannot in any way see how it is possible to get that result.

Given the following java method:

public int[] methodName(int[] nums)
{
     int largestOdd=0;
     for(int i=nums.length-2;i>=0;i--)
     {
          if (nums[i+1] % 2 != 0 && nums[i+1] > largestOdd)
               largestOdd = nums[i+1];

          if (nums[i] == 0)
               nums[i] = largestOdd;
     }
     return(nums);
}

What is printed when the following Java statements are executed?

int[] nums = {0,5,0,3};
nums = methodName(nums)
for (int i = 0; i<nums.length;i++)
    System.out.print(nums[i] + "");
System.out.println();

It just doesnt make any sense for me that first of all it will start printing "5". In my opinion it should be "3" because nums[2+1] = 3 (last index element)

Second of all why will it print four numbers when the loop in the method only will loop through 3 times until hitting -1 ?

If someone can explain how to get the result in a understandable way, I would be very happy.

Thanks in advance

4

4 回答 4

2

methodName向后遍历数组,检查每一对数字。在这种情况下,检查的第一对将是 (0,3)。

当它遍历成对时,methodName跟踪看到的最大奇数(为此它查看每对的第二个数字)。

每当第一个数字为零时,它将其设置为迄今为止看到的最大奇数。

所以在这种情况下,它将:

  1. 看 (0,3)。
  2. 3是奇数吗?是的。它是迄今为止见过的最大的奇数吗?是的。因此,请跟踪 3。
  3. 0 是零吗?是的。所以将其设置为 3。现在数组为 {0, 5, 3, 3}。
  4. 将我们的索引向后移动 1 并查看 (5, 3)。
  5. 3是奇数吗?是的。它是迄今为止见过的最大的奇数吗?不。
  6. 5 是零吗?不。
  7. 将我们的索引向后移动 1 并查看 (0, 5)。
  8. 5是奇数吗?是的。它是迄今为止见过的最大的奇数吗?是的。因此,请跟踪 5。
  9. 0 是零吗?是的。所以将它设置为 5。现在数组是 {5, 5, 3, 3}。
  10. 我们已经在数组的开头,所以我们不能再往前走了。
  11. 返回 main 方法,打印数组的内容。
于 2013-11-13T06:16:23.480 回答
0
Loop 1 start i=2,nums=[0,5,0,3],largestOdd=0
because:(nums[2+1]=3)/2 !=0 && (nums[2+1]=3)>(largestOdd=0)
so:largestOdd=(nums[2+1]=3)=3
beacuse:(nums[2]=0) ==0
so:nums[2]=(largestOdd=3)=3
Loop 1 end   i=2,nums=[0,5,3,3],largestOdd=3

Loop 2 start i=1,nums=[0,5,3,3],largestOdd=3
because:(nums[1+1]=3)/2 !=0 && (nums[1+1]=3)>(largestOdd=3)
so:next
beacuse:(nums[1]=5) !=0
so:next
Loop 2 end   i=1,nums=[0,5,3,3],largestOdd=3

Loop 3 start i=0,nums=[0,5,3,3],largestOdd=3
because:(nums[0+1]=5)/2 !=0 && (nums[0+1]=5)>(largestOdd=3)
so:largestOdd=(nums[0+1]=5)=5
beacuse:(nums[0]=0) ==0
so:nums[0]=(largestOdd=5)=5
Loop 3 end   i=0,nums=[5,5,3,3],largestOdd=5

END LOOP
于 2013-11-13T06:56:37.400 回答
0
{0,5,0,3}

for(int i=nums.length-2;i>=0;i--) // starts at 0 (index 2) <-> num.length 4 - 2

   // runs 3 times  num.length(4) - 2 = 0

   if (nums[i+1] % 2 != 0 && nums[i+1] > largestOdd)
           largestOdd = nums[i+1]; <-->  // 3

           // if odd and grater than 0 
           // first iteration largestOdd = 3


   if (nums[i] == 0)   // still i is index 2 {0, 5, 0, 3} = 0, so true
          nums[i] = largestOdd;  // nums[i] (index 2) = 3

          // after first iteration
          {0, 5, 3, 3}


  Second iteration do nothing (0 is not odd)

  Third iteration, do same as first iteration, making final array
  {5, 5, 3, 3}

下一部分

 // This method returns the same array you passed into to
 public int[] methodName(int[] nums)
 {
     return(nums);
  }

 // So
 nums = methodName(nums) = {5, 5, 3, 3} The new array produced by the method
于 2013-11-13T06:23:46.420 回答
0
you can analyse step by step.
In the for-loop: i starts from 2 to 0;
num = [0,5,0,3]
1.
    i = 2; largestOdd = 0;   nums[i+1] = nums[3] = 3;   nums[i] = nums[2] = 0;
    first condition "(nums[i+1] % 2 != 0 && nums[i+1] > largestOdd)" is true
        largestOdd = nums[i+1] = 3
    second condition (nums[i] == 0) is true
        nums[i] = nums[2] = largestOdd = 3
    nums = [0,5,3,3];
 2.
    i=1;   largestOdd = 3;   nums[i+1] = nums[2] = 3;   nums[i] = nums[1] = 5;
    first condition is false;
    second condition is false;
    nums = [0,5,3,3];
 3.
    i=0;  largestOdd = 3;   nums[i+1] = nums[1] = 5;   nums[i] = nums[0] = 0;
    first condition is true
         largestOdd = 5;
    second condition nums[0] = 0    is true
         nums[0] = largestOdd = 5;
    nums = [5,5,3,3];
于 2013-11-13T06:43:38.347 回答