6. Counting vertical histogram output and counting vertical histogram output
1 # include <stdio. h> 2 3 int main () 4 {5 int c; // input character 6 int c_number [10]; // String Length count array 0-9 7 int n_max; // maximum number of times 8 int I; // 9 10 n_max = 0 for loop; 11 12 for (I = 0; I <10; ++ I) // array initialization 13 c_number [I] = 0; 14 15 while (c = getchar ())! = '\ N') // character count 16 {17 if (c! = ''& C! = '\ T') 18 + + c_number [c-'0']; 19} 20 21 for (I = 0; I <10; ++ I) // obtain the maximum counter value 22 {23 if (c_number [I]> = n_max) 24 n_max = c_number [I]; 25} 26 27 for (; n_max> 0; -- n_max) // loop output 28 {29 printf ("\ n % d", n_max); 30 31 for (I = 0; I <10; ++ I) 32 {33 if (c_number [I]> = n_max) 34 printf ("|"); 35 else36 printf ("-"); 37} 38} 39 printf ("\ n 0123456789"); 40 return 0; 41}
It is a headache to do this exercise at the beginning, because the histogram is drawn in the vertical direction, it must be a column from left to right.
But now the output logic is from top to bottom, conflicting. After reading the normative answers, I don't know how to start it. After thinking for a long time, I figured out the idea:
[1] The final output method must be in a regular line from left to right and output one by one
[2] How can I determine the row number in the vertical direction? The lower labels in the horizontal direction are fixed, but the vertical direction is dynamic. If a character appears for 10 times
The top row is 10. If 100 times are displayed, the top row is 100. That is to say, the maximum row number is consistent with the maximum count.
When the next row is started, the maximum row number is-1, so that the loop continues until the minimum value is 1.
[3] when outputting data from left to right in a row, it must start from c_number [0]-c_number [9, if the Count value of c_number [I] is greater than or equal to the value corresponding to the current row
Value, Mark and output a '|' symbol. If the value is smaller than the value, other symbols must be output to distinguish.
PS: currently, the output count and the lower mark are relatively small, occupying only one data bit. If the Count value is slightly larger, there will be a misposition. If it is better to display it
Added the judgment on the number of digits of the value.