输入包含多组数据,每组数据包含一个字符串s。
s仅包含数字,长度不超过100。
对应每一组数据,依次为十种产品绘制高度等于其销量的“*”柱图。
直方图的高度等于数据中数量最多产品的个数,如果某种产品高度不到直方图的高度,用空格补全。
直方图下方依次输出0到9作为坐标。
0123456789123 123012312
*** ********** 0123456789 ** *** **** 0123456789
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s = sc.nextLine();
Map<Integer,Integer> map = new HashMap<>();
int max = -1;
int max_index = -1;
for(int i = 0 ; i < s.length() ; i++) { // 遍历字符串
int n = s.charAt(i) - '0';
if(map.containsKey(n)) {
map.put(n,map.get(n) + 1);
if(max <= map.get(n)) { // 更新最大值
max = map.get(n);
max_index = n;
}
} else {
map.put(n, 1);
}
}
// 输出
for(int i = max ; i > 0 ; i--) {
for(int j = 0 ; j < 10 ; j++) {
if(map.containsKey(j)) {
if(i <= map.get(j)) {
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("0123456789");
}
}
}