【问题标题】:How to create an histogram of asteriks from a Java array of number repetitions? [closed]如何从数字重复的Java数组创建星号直方图? [关闭]
【发布时间】:2015-08-26 01:14:44
【问题描述】:

我有一个包含重复数字的数组,我需要将它们显示在由“*”制作的“直方图”中。直方图应如下所示:

            *
        *   *
  *     *   *
  *     * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
1 2 3 4 5 6 7 8 9 10

我有这个数组

int[] repetition = new int[] {4,6,4,4,6,5,7,4,3,3};

我可以像这样水平打印它:

1*****
2******
3****
4****
5******
6*****
7*******
8****
9***
10***

如何创建垂直直方图?

【问题讨论】:

  • 请发表您的尝试以及您遇到的任何具体问题。
  • P.S.你应该确保你想要的输出是正确的,或者我猜你的 6 列有7 asterisks 和你的 7 列有8 asterisks 的原因
  • 人们应该停止投票和投票结束。这个问题很狭隘,可以简明扼要地回答。你只是不高兴有人在逃避他们的家庭作业。这并不能证明对问题进行错误分类是合理的。
  • @erickson 你是对的。这个答案很容易回答,因为已经有 5 个答案。 +1 给你和 OP

标签: java arrays histogram repeat


【解决方案1】:
public static final void printHistogram(int[] coll) {
        int max = coll[0];
        for (int i : coll)
            max = i > max ? i : max;
        boolean[][] array = new boolean[max][coll.length];
        for (int i = coll.length - 1; i >= 0; i--) {
            for (int j = 0; j < coll[i]; j++) {
                array[j][i] = true;
            }
        }
        for (int i = array.length - 1; i >= 0; i--) {
            boolean[] booleans = array[i];
            for (boolean b : booleans) {
                System.out.print(b ? '*' : ' ');
            }
            System.out.println();
        }

        for (int i = 1; i <= array.length; i++) {
            System.out.print(i);
        }
    }

这按预期工作。

【讨论】:

  • 其实没有,应该是:for (int i = 1; i &lt;= array.length; i++)
【解决方案2】:

首先计算直方图的最大高度。

max=0;
for(int i: repetition)
    if(i>max)
        max=i;

然后,你这样打印,从上到下:

for(j=max;j>=1;j--){                  //For each possible height of the histogram.
    for(k=0;k<repetition.length;k++)  //Check height of each element
        if(repetition[k]>=j)
            System.out.print("*");    //Print * if the k-th element has at least a height j.
        else
            System.out.print(" ");    //Else, do print blank space
    System.out.println();             //Newline after every row.
}

PS:这只是一个想法,不是一个完整的工作代码,它只适用于正高度值!

【讨论】:

  • 这个很好用。但是,为了大家,请把它放在一个方法中或指定变量。
  • repetition是asker指定的直方图数组,max存储直方图的最大高度。 i 是一个变量,它保存增强的 for 循环中的当前值。其余变量都是索引变量。
  • 感谢您的澄清。
【解决方案3】:

我不会给你任何代码,但我可以给你一个开始的想法。

对于垂直直方图,尽管它看起来像是从上到下打印的,但事实并非如此;你只是不能垂直打印。使用循环(数组中的最大值为起始值,0 为标记值,​​每次迭代减 1),其中每次迭代表示直方图上的一条水平线,您应该确定哪个 x-label 的星号需要在该行上打印,对于那些在该行上没有星号的标签,请在此处放置一个空格(用于格式化)。然后,创建另一个循环并使用它来列出所有 x-labels底部。

希望这会有所帮助!

【讨论】:

    【解决方案4】:

    Integer 用于使用 Collections max 函数。

    public static void main(String[] args) {
            Integer[] repetition = new Integer[] { 4, 6, 4, 4, 6, 5, 7, 4, 3, 3 };
            int maxValue = Collections.max(Arrays.asList(repetition));
            System.out.println("Maximum: " + maxValue);
            for (int i = maxValue; i > 0; i--) {
                for (int j = 0; j < repetition.length; j++) {
                    if (repetition[j] >= i) {
                        System.out.print(" * ");
                    } else {
                        System.out.print("   ");
                    }
                }
                System.out.println();
            }
            for (int j = 0; j < repetition.length; j++) {
                System.out.print(" " + (j + 1) + " ");
            }
        }
    

    【讨论】:

      【解决方案5】:

      听起来你有代码来“bin”数据,有效地创建直方图。添加一些代码来跟踪所有 bin 的最大计数。因此,在上面的示例中,最大值为 8(来自 bin 7)。

      然后,设置一个阈值,从最大值开始,倒数到一。在每次迭代中,在对应于达到或超过阈值的 bin 的列中打印带有星号的行。所以在第一行,只有第 7 列会得到星号,因为它是唯一满足当前阈值 8 的 bin。下一行(阈值 7),第 5 列和第 7 列会得到星号。以此类推。

      希望这对您自己编写代码有足够的帮助。

      【讨论】:

        猜你喜欢
        • 2013-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 2012-10-17
        • 2018-03-11
        • 2016-04-25
        • 2015-05-30
        相关资源
        最近更新 更多