【问题标题】:how to know array elements are perfectly centered?如何知道数组元素完全居中?
【发布时间】:2015-08-14 12:15:47
【问题描述】:

{3, 2, 10, 4, 1, 6, 9} 是完全居中的数组,因为10+4+1=15 序列之前和之后有相同数量的元素,即两个。

  mid=arraylength/2;
    for(int i=0;i<arraylength;i++){
           int sum=a[mid]+a[mid+1+i]+a[mid-1-i];
           if(sum==15) break;
}

计算sum=15后,如何知道10,4,1前后有相同数量的元素?

【问题讨论】:

  • 完美居中的数组到底是什么意思?根据您的定义,似乎所有数组都应该完全居中。中心的总和是否必须等于 15?
  • 是的,中心必须有 15 个,所以总和为 15 的 3 个元素完全居中。
  • 如果这些值等于 15,您的计算范围是?
  • 3+ 2+ 10 = 15, 1+ 6+ 9 = 15。这个是起作用的,还是随机的?
  • 如果数组的某些连续元素序列总和为 15,并且该序列前后有相同数量的元素,则该数组称为 centered-15。例如 {3, 2, 10, 4, 1, 6, 9} 以 15 为中心,因为序列 10, 4, 1 总和为 15,并且该序列前面有两个元素 (3, 2),后面有两个元素(6,9)。

标签: java arrays


【解决方案1】:

据我所知,您的范围是检查 int 数组是否居中。对于居中,您的意思是中心元素和右/左元素之和等于15。您不必检查铰孔元素的数量是否相同,因为对于奇数个元素,如果你子一个奇数(3),你得到一个偶数。 (在这种情况下,元件数为 7,居中为 3,铰孔为 4,每侧 2 个)。 这是您的问题的工作代码:

private static int SUM = 15;

public static void main(String[] args) {
    int array[] = {3,2,10,4,1,6,9};
    if(calculateSum(array)==SUM){
        System.out.println("The array is centered");
    }else{
        System.out.println("The array is not centered");
    }

}

public static int calculateSum(int array[]){
    int center = array.length/2;
    int result = array[center-1]+array[center]+array[center+1];
    return result;
}

【讨论】:

    【解决方案2】:

    我认为您在上述问题上试图实现的就是这个。

    public static int  isCentered(int [] arr) {
        int sum= 0;
        int expectedSum=15;
        int mid = arr.length/2;
        int len= arr.length;
        for (int i = 0;  i <len ; i++) {
            sum=arr[mid]+arr[mid+1+i]+arr[mid-1-i];
            if(sum==expectedSum){
                if ((len-3)%2==0){
                    return 1;
                }else{
                    return 0;
                }
            }else {
                return 0;
            }
        }
        return 0;
    }
    

    【讨论】:

      【解决方案3】:
      /* sampleData
      * {3, 2, 10, 4, 1, 6, 9}
      * {2, 10, 4, 1, 6, 9}
      * {3, 2, 10, 4, 1, 6}
      * {9, 15, 6}
      * {15}
      * {14}
      */
      
      public class IsCentered15 {
      
          public static void main(String[] args) {
              // TODO Auto-generated method stub
      
              int[] sampleData = { 11 };
              int result = isCentered15(sampleData);
      
              System.out.println(result);
      
          }
      
          static int isCentered15(int[] a) {
      
              int returnValue = 0;
              int centeredIndex = 0;
              int sum = 0;
      
              if ((a.length % 2) == 0) {
      
                  returnValue = 0;
      
              } else if (a.length == 1) {
      
                  if (a[0] == 15) {
                      returnValue = 1;
                  } else {
                      returnValue = 0;
                  }
      
              } else {
      
                  /*
                   * as we know index start from 0, example a.length = 7 and center of index will
                   * be index of 3. So int 7/2 = 3 *
                   */
      
                  centeredIndex = (a.length / 2);
                  sum = a[centeredIndex - 1] + a[centeredIndex + 1] + a[centeredIndex];
                  int centeredValue = a[centeredIndex];
      
                  if (sum == 15 || centeredValue == 15) {
      
                      returnValue = 1;
      
                  }
              }
              return returnValue;
          }
      
      }
      

      【讨论】:

      • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并最终将这些知识应用到他们自己的代码中。解释代码时,您也可能会得到用户的积极反馈/赞成。
      猜你喜欢
      • 2013-04-23
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      • 2017-04-28
      相关资源
      最近更新 更多