Yesterday, a classmate talked to me about an ACM question, which was very interesting. As a result, I listened to the question's requirements:
Enter two numbers in each line as follows:
1 1234567890
Output:
2 1234567890
Output
3 1234567890
Output
I think you should know the requirements of the questions.
Analysis: The above digital output is a bit like LED digital output. I don't know if you know the seven-segment LED display. I used this in my program below, if each bit of storage is enabled, 0 indicates that the storage is not enabled, and 1 indicates that the storage is enabled.
As follows:
The above 0-6 corresponds to the LED segment, with a total of 7 segments, so we can use 7 bits to store them separately. 0 indicates none, and 1 indicates there are
For example, 2 is displayed
We can use the following binary representation (from the 6-0 order) to 1011101; similarly, the binary representation of 3 is 1101101; the binary representation of 4 is 0101110. With the above analysis, in the output. Only I = 0 \ 3 \ 6 indicates horizontal, and the rest indicates vertical, and their output is different. If a bit is set, the corresponding flag is output. If no bit is set, a space is output. The Code is as follows:
# Include <iostream> # include <string> # include <vector>/** Author: w397090770E-mail: wyphao.2007@163.com is only used for learning communication, reprint please write the above comments, thank you for your kindness. */Using namespace std; char ptr [] ={// these numbers indicate 0-9 LEDs display 119, 36, 93,109, 46,107,123, 37,127,111}; bool getBit (char c, int I) {return c & (1 <I) ;}int main () {string num = "1234890"; int n = 3; int len = 0; len = num. length (); // Save the preceding number to indicate vector <char> v; int I = 0; for (I = 0; I <len; I ++) {v. push_back (ptr [num [I]-'0']); // cout <(int) v [I] <endl ;} // whether it is horizontal bool isH = false; int j = 0, k = 0, l = 0; for (I = 0; I <7; I ++) {// horizontal for (j = 0; j <len; j ++) {for (k = 0; k <n + 2; k ++) {if (I = 0 | I = 3 | I = 6) {isH = true ;} else {isH = false;} if (isH & (k = 0 | k = n + 1) {cout <"";} else if (I = 0 | I = 3 | I = 6) & getBit (v [j], I )) {cout <"-";} else if (I = 0 | I = 3 | I = 6 )&&! GetBit (v [j], I) {cout <";}}// vertical for (k = 0; k <n; k ++) {for (j = 0; j <len; j ++) {if (I = 1 | I = 4) & getBit (v [j], i) {cout <"|"; for (l = 0; l <n; l ++) {cout <"";}} else if (I = 1 | I = 4 )&&! GetBit (v [j], I) {cout <""; for (l = 0; l <n; l ++) {cout <"";}} if (I + 1) = 2 | (I + 1) = 5) & getBit (v [j], I + 1 )) {cout <"|";} else if (I + 1) = 2 | (I + 1) = 5 )&&! GetBit (v [j], I + 1) {cout <"" ;}} if (! IsH) {cout <endl ;}// output, so you need to skip if (I = 1 | I = 4) {I ++ ;} else {cout <endl ;}} cout <endl; return 0 ;}
Output: