首页 > 解决方案 > 如何创建水平直方图?【算法】JAVA

问题描述

我正在做这个练习,但我无法解决算法,你们能帮我解决吗?

如果有人可以为我解决这个问题,这是我的代码,我非常感激我不知道我尝试了多久,我在垂直方向上做了但不能在水平方向上解决。希望有人能解决它。

谢谢

    // 10. A histogram is a graph that shows how often a given list of histograms appears.
    // different values that could form it. Make a histogram with values between
    // For example, if the list consists of: 6, 4, 4, 1, 9, 7, 5, 6, 4, 2, 3, 9, 5, 6, 4, su
    // histogram would be:
    //                 *
    //                 *   *
    //                 * * *     *
    //           * * * * * * *   *
    //         0 1 2 3 4 5 6 7 8 9
    // This indicates that 0 and 8 do not appear at all, that 1, 2, 3 and 7 appear once, 5 and 9 appear twice.
    // times, etc. Write an algorithm that reads a list of numbers between 0 and 9 (the list
    // will end when a negative number is read) and print a histogram like the previous one on the screen.

    public static void main(String[] args) {


    int num;

    System.out.println("Cantidad de elementos: ");
    num = leerInt(); // Almacenamos cantidad de elementos

    int valor[] = new int[num]; // Creamos el vector

    for (int i = 0; i < valor.length; i++) {
        System.out.printf("Que numero quieres meter en array %d: %n",i);
        num = leerInt();
        valor[i] = num;
    }

    histograma(valor); // Mostramos el histograma

}


public static void histograma(int[] v) { // Hace el histograma
    int count=0, count2=0, num=0;

    for (int i=0; i<10; i++) {
        for (int j=0; j<v.length; j++) {
            //System.out.printf("  ");
            if(v[j]==i) {
                count++;
            }
        }
        if(count>count2) {
            count2=count;
            num=i;
        }
        count=0;
    }

    for (int i=0; i<10; i++) {
        for (int j=0; j<v.length; j++) {
            if (v[j]==i) {
                count++;
            }
            if (j==num&&count<count2) {
                System.out.printf("* ");
            }else {
                System.out.printf("  ");
            }

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

    for (int i=0; i<10; i++) {
        System.out.printf("%d ",i);
    }
}

}

控制台结果:

Cantidad de elementos: 
3
Que numero quieres meter en array 0: 
2
Que numero quieres meter en array 1: 
1
Que numero quieres meter en array 2: 
2
    * 
    * 








0 1 2 3 4 5 6 7 8 9 

标签: java

解决方案


我问了我的老师,这是我的解决方案:

import static proyectoTeclado.Teclado.*;

public class Ejercicio10 {

    public static void main(String[] args) {

        int num=1,max=0;
        int valor[]=new int[10];
        boolean stop=false;

        while(!stop) {
            System.out.println("Introduce numero: ");
            num=leerInt();
            if(num>=0&&num<=9) {
                valor[num]++;
                if(valor[num]>max) {
                    max=valor[num];
                }
            }else if(num>9) {
                System.out.println("Introduce solo entre 0 o 9, si quieres parar pon un numero negativo.");
            }else {
                stop=true;
            }
        }

        histograma(valor,max);

    }


    public static void histograma(int[] v, int max) { // Hace el histograma

        for(int i=max; i>0; i--) {
            for(int j=0; j<v.length; j++) {
                if(v[j]>=i) {
                    System.out.print("* ");

                }else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }

        for(int i=0; i<10; i++) {
            System.out.printf("%d ",i);
        }

    }

}

推荐阅读