【问题标题】:Sum Experiment array / loop dilemmaSum 实验数组/循环困境
【发布时间】:2017-08-29 19:19:35
【问题描述】:

我正在编写一个程序,但我似乎无法制作 IF 动作循环并检查 main 中的所有数组。我的工作是弄清楚在这个升序排列的数组中是否存在任何一对数字(即任意两个元素),它们的总和为 20。你需要做的就是将这两个指针指向的值相加,看看是否它们等于 20,如果是,则输出。否则,检查总和,如果总和大于 20,则减少第二个指针,如果总和小于 20,则增加第一个指针。不能使用嵌套的 for 循环方法!!不知道如何解决这个问题......我已经做了几个小时并且没有运气手写它。谢谢!!

// if val of arr at index i is smaller than at arr j, then return
    // smaller value
    // This function will inspect the input to find any pair of values that
    // add up to 20
    // if it find such a pair, it will return the *index* of the smallest
    // value
    // if it does not find such as pair, it will return -1; 

public class SumExperiment {

public static int check_sum(int[] array) {
int i = array[0];
int y = array.indexOf(array.length); // need value @ index of array.length to begin

//loop to repeat action

for (int arraysChecked = 0; arraysChecked < 5; arraysChecked++ )
{
    if ( i + y == 20)
        {
        return i;
    //  System.out.print(array[i]);
        }
            else if ( i + y > 20)
                {
                y--; //index @y
                }
            else if (i + y < 20)
                {
                i++; //index @x
                }

    if ( i + y != 20)
    {
        return -1;
    }
    arraysChecked++;
}
return -1;  //because must return int, but this isn't correct
}


public static void main(String[] args) {
    int[] array1 = new int[] { 5, 7, 8, 9, 10, 15, 16 };
    if (check_sum(array1) != 0)
        System.err.println("TEST1 FAILED");

    int[] array2 = new int[] { 3, 5, 8, 9, 10, 15, 16 };
    if (check_sum(array2) != 1)
        System.err.println("TEST2 FAILED");

    int[] array3 = new int[] { 3, 4, 6, 9, 10, 14, 15 };
    if (check_sum(array3) != 2)
        System.err.println("TEST3 FAILED");

    int[] array4 = new int[] { 6, 7, 8, 9, 10, 15, 16 };
    if (check_sum(array4) != -1)
        System.err.println("TEST4 FAILED");

    System.out.println("Done!!!");
}
}

【问题讨论】:

  • array.indexOf(array.length); 甚至无法编译!并提示:格式化和缩进很重要。您非常规的风格使阅读代码的难度增加了 10 倍。
  • 看了你的代码 5 分钟,我也是 - 我放弃了。您的大部分代码只是有意义。我的建议:完全退后一步。扔掉你的检查方法,然后,首先自己解决这个难题。如果该数组中的 any 对加起来为 20,则 you 将如何计算。然后再次编写代码。

标签: java arrays string loops if-statement


【解决方案1】:

我认为您对数组中的值和值的索引感到困惑。这是一个带有变量名称的工作版本,可以更容易地理解正在发生的事情:

public static int check_sum(int[] array) {
  int leftIndex = 0;
  int rightIndex = array.length - 1;

  for (int arraysChecked = 0 ; arraysChecked < 5 ; arraysChecked++) {
    if (leftIndex == rightIndex) {
      return -1;
    }

    int smallerValue = array[leftIndex];
    int largerValue = array[rightIndex];
    int sum = smallerValue + largerValue;

    if (sum == 20) {
      // Returns INDEX of smaller value
      return leftIndex;
    } else if (sum > 20) {
      rightIndex--;
    } else if (sum < 20) {
      leftIndex++;
    }

    // NO NEED FOR THIS: arraysChecked++; (for loop does it for you)
  }
}

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 2014-05-28
    • 2012-11-23
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多