如何创建水平直方图? [算法] JAVA

时间:2019-01-09 19:35:05

标签: 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 

2 个答案:

答案 0 :(得分:0)

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

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);
        }

    }

}

答案 1 :(得分:0)

您只需要计算数字并在其空间中打印即可。如您所见,行数是最大计数列数是9 [注意:循环必须运行以达到最大计数]

let query;
category.queryParams.query.fieldFilters.forEach(function(fieldFilter) {
    if (typeof query === "undefined") {
       query = collection.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
    } else {
       query = query.where(fieldFilter.field, filterOperationString(fieldFilter.filterType), fieldFilter.filterValue);
    }
});

输出:

enter image description here