簡體   English   中英

如何計算 Java 中隨機 int 數組元素值之間的差異

[英]How to count the difference between a random int array elements values in Java

我是初學者,需要以下方面的幫助:-)有一個隨機整數數組,值 (0-10) 顯示地形的高度,我需要先計算這個地面結構上可以儲存多少水。 例如 int[] randomTerrain = {8, 4, 8, 2, 6, 3, 2, 1, 6, 9}; 該地基結構蓄水量為0+4+0+6+2+5+6+7+2+0=32。 我有兩個問題,首先如何從隨機 int 數組中計算它? 第二個有點不同,因為我需要打印出來並將其可視化為二維字符串數組 [10][10],其中 string ground = "*"; 字符串水=“-”; 空氣是空的空間。 在此先感謝您的幫助,您可以在下面看到我所做的。 但它並沒有像預期的那樣工作。 GhostCats 的分辨率是完美的,但由於我們還沒有了解 stream 方法,如果有人可以僅使用簡單的方法幫助解決一些不同的問題,那將是非常感激的; package 水庫;

導入 java.util.Arrays; 導入 java.util.Random;

公共 class 水庫 {

private static final int TERRAIN_HEIGHT = 10;
private static final int TERRAIN_WIDTH = 10;

public static void main(String[] args) {

    int[] randomTerrain = createRandomTerrain();
    System.out.println(Arrays.toString(randomTerrain));
                    
    int waterStorageCapacity = countWaterCapacity(randomTerrain);       
    System.out.println(waterStorageCapacity);   
    System.out.println();
    String[][] terrain = {
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "}
    };

    int row = 0;
    int column = 0;

    for (column = 0 ; column < terrain[0].length ; column++) {

        for (row = 0 ; row < terrain.length ; row++) {

            if ((terrain.length -1) -row <= randomTerrain[column]) {

                terrain[column][row] = "*";
            }
            System.out.print(terrain[terrain.length-1-row][column] + "\t");
        }
        System.out.println();
    }
}
private static int countWaterCapacity(int[] randomTerrain) {
    int capacitySum = 0;
    int firstTop = 0;   
    
    for (int i = 0 ; i < randomTerrain.length -1 ; i++) {           
        
        if (randomTerrain[i] >= randomTerrain[i+1]) {
            firstTop = randomTerrain[i];            
        }               
            else if (randomTerrain[i+1] >= randomTerrain[i])    {
                firstTop = randomTerrain[i+1];                  
            }               
        int capacity = 0;
        capacity = firstTop - randomTerrain[i];
        capacitySum += capacity;            
    }
    return capacitySum;
}
private static int[] createRandomTerrain() {
    int[] randomTerrain = new int[TERRAIN_WIDTH];

    for (int i = 0 ; i < randomTerrain.length ; i ++) {
        randomTerrain[i] = (int) (Math.random() * TERRAIN_HEIGHT + 1);

    }
    return randomTerrain;       
}

}

代碼的含義基本上在注釋中解釋。 這可能會解決您的問題,但可能不是最有效的方法。 您可以自行探索更多方法。

public class Main {

    public static void main(String[] args) {
        int[] terrain = { 8, 4, 8, 2, 6, 3, 2, 1, 6, 9 };
        int[] capacity = countCapacity(terrain);
        int sum = Arrays.stream(capacity).sum();
        System.out.println(Arrays.toString(capacity));
        System.out.println(sum);
        printGraph(terrain,capacity);
    }

    public static void printGraph(int[] terrain, int[] capacity) {
        // find the max height, and it will be the row numbers of console output.
        int maxHeight = Arrays.stream(terrain).max().orElse(0);
        for (int j = maxHeight; j > 0; j--) {
            for (int i = 0; i < terrain.length; i++) {
                // if terrain height is lower than current printing height.
                if (terrain[i] < j){
                    // and terrain's capacity + height is lower, just print space
                    if (capacity[i] + terrain[i] < j){
                        System.out.print("   ");
                    // otherwise, print water
                    }else {
                        System.out.print(" - ");
                    }
                // if terrain height is higher than current printing height. print terrain
                }else {
                    System.out.print(" * ");
                }
            }
            // make a new row.
            System.out.println();
        }
    }

    public static int[] countCapacity(int[] terrain) {
        List<Integer> terranList = Arrays.stream(terrain).boxed().collect(Collectors.toList());
        int[] capacity = new int[terrain.length];
        // the first and last terrain's capacity must be 0. just set it first.
        capacity[0] = 0;
        capacity[capacity.length - 1] = 0;
        for (int i = 1; i < terrain.length - 1; i++) {
            // if there's no terrain higher than current terrain at left. then it can not hold water.
            // so it's capacity is 0, and skip to next terrain.
            int currentHeight = terrain[i];
            int leftHighest = Collections.max(terranList.subList(0, i));
            if (leftHighest <= currentHeight) {
                capacity[i] = 0;
                continue;
            }
            // if there's no higher terrain in the right. the same as above.
            int rightHighest = Collections.max(terranList.subList(i + 1, terranList.size()));
            if (rightHighest <= currentHeight) {
                capacity[i] = 0;
                continue;
            }
            // if there are higher terrain int both direction. this terrain can hold water.
            // and its capacity is the lower one minus this terrain's height.
            capacity[i] = Math.min(leftHighest, rightHighest) - currentHeight;
        }
        return capacity;
    }
}

output 可能喜歡:

[0, 4, 0, 6, 2, 5, 6, 7, 2, 0]
32
                            * 
 *  -  *  -  -  -  -  -  -  * 
 *  -  *  -  -  -  -  -  -  * 
 *  -  *  -  *  -  -  -  *  * 
 *  -  *  -  *  -  -  -  *  * 
 *  *  *  -  *  -  -  -  *  * 
 *  *  *  -  *  *  -  -  *  * 
 *  *  *  *  *  *  *  -  *  * 
 *  *  *  *  *  *  *  *  *  * 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM