: Simulating 3x5 LCD Numeric Displays

LCD displays represent each decimal digit using a 3×5 dot matrix pattern, where 'X' indicates an active pixel and '.' represents a inactive one. Given a sequence of digits, the task is to render them visually with a single column of spacing between adjacent digits.

The input consists of two lines: first, a integer n (1 ≤ n ≤ 100) specifying the number of digits, followed by a string of n digits. The output should be exactly five lines that visualize the dot matrix representation.

For example, rendering "0123456789" produces:

XXX...X.XXX.XXX.X.X.XXX.XXX.XXX.XXX.XXX
X.X...X...X...X.X.X.X...X.....X.X.X.X.X
X.X...X.XXX.XXX.XXX.XXX.XXX...X.XXX.XXX
X.X...X.X.....X...X...X.X.X...X.X.X...X
XXX...X.XXX.XXX...X.XXX.XXX...X.XXX.XXX

Each digit occupies a 3×5 grid. The following patterns define the standard representation:

  • 0: Full border with empty center
  • 1: Right vertical line only
  • 2: Top, middle, bottom horizontal lines with right top and left bottom verticals
  • 3: Top, middle, bottom horizontal lines with right vertical line
  • 4: Left and right vertical lines with middle horizontal
  • 5: Top, middle, bottom horizontal lines with left top and right bottom verticals
  • 6: Top, middle, bottom horiozntal lines with left vertical and right bottom vertical
  • 7: Top horizontal line with right vertical line
  • 8: All horizontal lines and both vertical lines
  • 9: Top, middle, bottom horizontal lines with both vertical lines

A C++ implementation can efficiently construct the output by building each of the five display lines separately. The solution predefines the matrix patterns for each digit and concatenates them with appropriate separators:

#include <iostream>
#include <string>

int main() {
    int digitCount;
    std::string inputNumber;
    
    std::cin >> digitCount >> inputNumber;
    
    std::string rows[5];
    bool isFirst = true;
    
    for (char digitChar : inputNumber) {
        int digit = digitChar - '0';
        std::string patterns[10][5] = {
            {"XXX", "X.X", "X.X", "X.X", "XXX"}, // 0
            {"..X", "..X", "..X", "..X", "..X"}, // 1
            {"XXX", "..X", "XXX", "X..", "XXX"}, // 2
            {"XXX", "..X", "XXX", "..X", "XXX"}, // 3
            {"X.X", "X.X", "XXX", "..X", "..X"}, // 4
            {"XXX", "X..", "XXX", "..X", "XXX"}, // 5
            {"XXX", "X..", "XXX", "X.X", "XXX"}, // 6
            {"XXX", "..X", "..X", "..X", "..X"}, // 7
            {"XXX", "X.X", "XXX", "X.X", "XXX"}, // 8
            {"XXX", "X.X", "XXX", "..X", "XXX"}  // 9
        };
        
        for (int i = 0; i < 5; ++i) {
            if (!isFirst) {
                rows[i] += ".";
            }
            rows[i] += patterns[digit][i];
        }
        
        isFirst = false;
    }
    
    for (int i = 0; i < 5; ++i) {
        std::cout << rows[i] << std::endl;
    }
    
    return 0;
}

This approach initializes a boolean flag to handle the leading separator column. For each digit, it appends the corresponding 3-character pattern segment to each output line. The separator column is added before each digit except the first. After processing all digits, the five completed lines are printed sequentially.

Tags: lcd-display string-manipulation simulation C++ competitive-programming

Posted on Sat, 26 Sep 2026 16:46:47 +0000 by cgrenda