java - 如何创建水平直方图?【算法】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
解决方案
我问了我的老师,这是我的解决方案:
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);
}
}
}
推荐阅读
- laravel - 如何将一个类别重定向到子类别?
- oracle - 由于会话状态值导致的 Oracle Apex 性能问题
- hadoop - 如何调试 map-reduce 失败的地方?
- javascript - 如何遍历多个相似元素并依次更改每个元素的属性?
- optimization - 优化 Pywinauto
- python - 如何在 Python 中切换嵌套字典的值和键?
- sql - 日期字符串中使用的日期格式是什么,“2019-01-21T19:02:25Z”
- python - 尝试定义线程时对象没有属性
- php - 使用 PHP 函数 end() 时间接修改 Illuminate\Support\Collection 的重载元素无效
- c# - 如何从字典中获取不匹配的列表