我该如何做,所以每个零旁边的数字都除以2?

时间:2018-11-15 19:08:46

标签: java

到目前为止,我所做的所有事情都将代码除以2,并且由于某种原因两次执行。

CSP-阵列  一群居民代表城市及其各自的人口。例如,以下数组显示了8个城市及其各自的人口:[3、6、0、4、3、2、7、1]一些城市由于大流行的僵尸疾病正在消灭人类,因此人口为0生活。每天过后,与僵尸缠身的城市相邻的任何城市都将失去一半的人口,编写一个程序循环遍历每个城市的人口,并使其在(左或右)相邻时失去一半的人口。人口为零的城市,直到所有城市都没有人了。

package Arrays;

public class Project {
public static void main(String[] args){
    int i = 0;
    boolean hi = false;
    boolean hi1 = false;
    boolean hi2 = false;
    boolean hi3 = false;
    boolean hi4 = false;
    boolean hi5 = false;
    boolean hi6 = false;
    boolean hi7 = false;
    int[] a = {3, 6, 0, 4, 3, 2, 7, 1};
    if(a[0]==0) {
        hi=true;
    }
    if(a[1]==0) {
        hi1=true;
    }
    if(a[2]==0) {
        hi2=true;
    }
    if(a[3]==0) {
        hi3=true;
    }
    if(a[4]==0) {
        hi4=true;
    }
    if(a[5]==0) {
        hi5=true;
    }
    if(a[6]==0) {
        hi6=true;
    }
    if(a[7]==0) {
        hi7=true;
    }
    int z=1;
    while(hi!=false || hi1!=false || hi2!=false ||  hi3!=false || hi4!=false || hi5!=false || hi6!=false || hi7!=false) {
    if(hi=true){
        a[1]=a[1]/2;
    }
    if(hi1=true){
        a[0]=a[0]/2;
        a[2]=a[2]/2;
    }
    if(hi2=true){
        a[1]=a[1]/2;
        a[3]=a[3]/2;
    }
    if(hi3=true){
        a[2]=a[2]/2;
        a[4]=a[4]/2;
    }
    if(hi4=true){
        a[3]=a[3]/2;
        a[5]=a[5]/2;
    }
    if(hi5=true){
        a[4]=a[4]/2;
        a[6]=a[6]/2;
    }
    if(hi6=true){
        a[5]=a[5]/2;
        a[7]=a[7]/2;
    }
    if(hi7=true){
        a[6]=a[6]/2;
    }
    System.out.println("Day "+i+": ["+a[0]+", "+a[1]+", "+a[2]+", "+a[3]+", "+a[4]+", "+a[5]+", "+a[6]+", "+a[7]+"] ");
    i++;
    }
        }
}

4 个答案:

答案 0 :(得分:0)

我也是Java的新手,但这是我的尝试。

ml-update="aware" ml-stage="preload" ml-platform="desktop"

答案 1 :(得分:0)

我认为这很简单。只需遍历数组并检查上一个和下一个值即可。

public static void calc(int[] arr) {
    for(int i = 1; i < arr.length; i++)
        if(arr[i] > 0 && arr[i - 1] == 0)
            arr[i] /= -2;

    for(int i = arr.length - 2; i >= 0; i--)
        if(arr[i] < 0)
            arr[i] = -arr[i];
        else if(arr[i] > 0 && arr[i + 1] == 0)
            arr[i] /= 2;
}

答案 2 :(得分:0)

这就是你要的。

import java.util.Arrays;

public class Project {

public static void main(String[] args) {

    int i = 0;

    boolean hi = false;
    boolean hi1 = false;
    boolean hi2 = false;
    boolean hi3 = false;
    boolean hi4 = false;
    boolean hi5 = false;
    boolean hi6 = false;
    boolean hi7 = false;

    int[] a = {3, 6, 0, 4, 3, 2, 7, 1};


    //the line of code below this comment is never used ever, what is it for?
    int z = 1;

    while ((((!hi || !hi1) || (!hi2 || !hi3)) || (!hi4 || !hi5)) || (!hi6 || !hi7)){

        if (hi) {
            a[1] = a[1] / 2;
        }
        if (hi1) {
            a[0] = a[0] / 2;
            a[2] = a[2] / 2;
        }
        if (hi2) {
            a[1] = a[1] / 2;
            a[3] = a[3] / 2;
        }
        if (hi3) {
            a[2] = a[2] / 2;
            a[4] = a[4] / 2;
        }
        if (hi4) {
            a[3] = a[3] / 2;
            a[5] = a[5] / 2;
        }
        if (hi5) {
            a[4] = a[4] / 2;
            a[6] = a[6] / 2;
        }
        if (hi6) {
            a[5] = a[5] / 2;
            a[7] = a[7] / 2;
        }
        if (hi7) {
            a[6] = a[6] / 2;
        }

        System.out.println("Day " + i + Arrays.toString(a));
        i++;

        /*if you want to update the boolean values after they are changed, then you have to include it within the block of code
        that is changing it. (if they are outside of this block of code, how will they ever update?)
        */

        if (a[0] == 0) {
            hi = true;
        }
        if (a[1] == 0) {
            hi1 = true;
        }
        if (a[2] == 0) {
            hi2 = true;
        }
        if (a[3] == 0) {
            hi3 = true;
        }
        if (a[4] == 0) {
            hi4 = true;
        }
        if (a[5] == 0) {
            hi5 = true;
        }
        if (a[6] == 0) {
            hi6 = true;
        }
        if (a[7] == 0) {
            hi7 = true;
        }

    }

}
}

答案 3 :(得分:0)

对于每天的迭代,您会找到人口为 0 的城市并将这些索引推送到一个数组中,让我们将此数组命名为“索引”。然后循环遍历这个数组并将这些索引的唯一邻居划分为人口的一半。我用javascript编写。

    const inhabitants = (Arr) => {
      let day = 0;
      let A = Arr;
      // allEqual checks if the all elements in array is 0
      // when allEqual is true loop is over
      const allEqual = (arr) => arr.every((v) => v === arr[0]);
      while (!allEqual(A)) {
      // find the array[i]= 0 and push the "i" in indices
        let indices = [];
    
        for (let i = 0; i < A.length; i++) {
          if (A[i] === 0) {
            indices.push(i);
          }
        }
        // just divide the neighbor elements of element 0 
        for (let j = 0; j < indices.length; j++) {
          if (indices[j] > 0) {
            A[indices[j] - 1] = Math.floor(A[indices[j] - 1] / 2);
          }
          if (indices[j] + 1 < A.length) {
            A[indices[j] + 1] = Math.floor(A[indices[j] + 1] / 2);
          }
        }
    
        day++;
        console.log(`population on day ${day}`, A);
      }
    };
相关问题