Record a written test in grape city

Title Source:

Title website:

Title Description:

Title: Data Visualization

Programming language: Unlimited

Title Description: there is a saying: "the text is not as good as the table, and the table is not as good as the picture". It vividly describes the different efficiency and experience that charts bring to the receiver when conveying information. Therefore, with the continuous improvement of computer computing power, data scale and decision-making needs, the application of data visualization is becoming more and more common.

The scope of data visualization is very wide, involving many concepts and fields such as data acquisition, processing, modeling, graphics and human-computer interaction. If you want to get started faster and get a better experience, it is a wiser choice to use professional tools and services such as DragonFly BI.

Today, we personally realize simple data visualization through a simplified proposition. Write a program to output a histogram composed of characters for a given set of data and requirements.


In the first row, an integer N (1 < = N < = 20) represents the number of entries of this group of data.
The second line, two strings, is used to indicate the sorting method of data displayed on the histogram. The first string is "Name" or "Value", indicating that the sorting is based on the Name or Value of the data entry; The second string is "ASC" or "DESC", indicating ascending or descending order.
The next N lines, each containing a string s and a number V, separated by spaces, represent a piece of data. S is the name of the data entry, which only contains lowercase letters. V is the corresponding value, which is an integer, (0 < = V < = 1000000)


Chart outline corner symbol:

  • "┌"(\u250c)
  • "┐"(\u2510)
  • "└"(\u2514)
  • "┘"(\u2518)

Horizontal and vertical lines in the chart:

  • "─"(\u2500)
  • "│"(\u2502)

Various cross lines in the chart:

  • "├"(\u251c)
  • "┤"(\u2524)
  • "┬"(\u252c)
  • "┴"(\u2534)
  • "┼"(\u253c)

Characters used to spell columns:

  • "█"(\u2588)

Spaces in chart:

  • " "(\u0020)

The width of the name area in the chart is determined by the maximum length of the name in this group of data. All names are aligned to the right. The maximum length of the column in the chart is 20. The length of each column is obtained by multiplying the ratio of the corresponding data of the column and the maximum value in this group of data (this value must be greater than 0) by 20, and the part less than one grid is rounded off.

Input example

Value DESC
apple 5
pen 3
pineapple 10

Output example


import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class test {
    public static void main(String []args) {
    	Scanner sc = new Scanner(;
    	int num = sc.nextInt();
    	String way =;
    	String seq =;
    	String name;
    	int value;
    	int maxLength = 0;
    	int maxNum = 0;
    	Object[] arr = new Object[num];
    	HashMap<Object, Object> map = new HashMap<>();
    	for (int i=0; i<num; i++) {
    		name =;
    		value = sc.nextInt();
    		if (name.length() > maxLength)
    			maxLength = name.length();
    		if (value > maxNum)
    			maxNum = value;
    		if (way.equals("Name")) {
    			map.put(name, value);
    			arr[i] = name;
    		} else {
    			map.put(value, name);
    			arr[i] = value;
    	if (seq.equals("DESC")) {
    		int l = arr.length;
    		Object temp=0;
    		for (int i=0; i<l/2; i++) {
    			temp = arr[i];
    			arr[i] = arr[l-i-1];
    			arr[l-i-1] = temp;
    	//Output first line
    	for (int i=0; i<maxLength; i++)
    	for (int i=0; i<20; i++)
    	//Output the following lines, in groups of two lines
    	for (int k=0; k<num; k++) {
    		//Get the row data
    		if (way.equals("Name")) {
    			name = (String)arr[k];
    			value = (int)map.get(arr[k]);
    		} else {
    			value = (int)arr[k];
    			name = (String)map.get(arr[k]);
    		//Calculate column length
    		int q = (int)((double)value/maxNum*20);
    		//Output with data row
    		for (int i=1; i<=maxLength-name.length(); i++) {
    		for (int i=1; i<=q; i++) {
    		for (int i=q; i<20; i++) {
    		//Output border row
    		if (k == num-1) {
    			for (int i=0; i<maxLength; i++) {
    			for (int i=0; i<20; i++) {
    		} else {
    			for (int i=0; i<maxLength; i++) {
    			for (int i=0; i<20; i++) {

Operation screenshot:

Keywords: Java HashMap unicode

Added by jellis on Sun, 05 Sep 2021 03:56:52 +0300