# String Manipulation# Output Formatting# Conditional Logic

c135 - 00706 - LC-Display

🔗 前往 ZeroJudge 原題

題目描述

題目要求根據給定的數字大小 s 和數字字串 a,以類似 LCD 顯示器的格式輸出數字。每個數字由 '-' 和 '|' 字符組成,並且數字之間需要有空白行。

解題思路

程式碼直接根據每個數字的字元,輸出對應的 LCD 顯示模式。對於每個數字,程式碼會輸出五個部分:頂部橫線、左側垂直線和中間部分、中間的橫線、右側垂直線和中間部分、底部橫線。程式碼使用迴圈遍歷輸入字串 a 的每個字元,並根據字元的數值輸出相應的 LCD 模式。sp 函數用於輸出指定數量的空格。

複雜度分析

  • 時間複雜度: O(s * n),其中 s 是數字大小,n 是數字字串的長度。因為程式碼需要輸出每個數字的每個部分,並且每個部分需要輸出一定數量的字符。
  • 空間複雜度: O(1),因為程式碼只使用了常數級別的額外空間。

程式碼

#include <iostream>
using namespace std;
inline void sp(int x,char z){
	while(x--)cout << z;
}
int main(){
	cin.tie(0); ios::sync_with_stdio(false);
	int s;
	string a;
	while(cin >> s >> a){
		if(s==0)break;
		int al=a.length();
		for(int i=0;i<al;++i){
			if(a[i]=='1'||a[i]=='4')sp(s+2,' ');
			else{
				cout << " ";
				sp(s,'-');
				cout << " ";
			}
			if(i!=al-1)cout << " ";
		}
		cout << "\n";
		int sss=s;
		while(sss--){
			for(int i=0;i<al;++i){
				if(a[i]=='0'||a[i]=='4'||a[i]=='8'||a[i]=='9'){
					cout << "|";
					sp(s,' ');
					cout << "|";
				}
				else if(a[i]=='5'||a[i]=='6'){
					cout << "|";
					sp(s+1,' ');
				}
				else{
					sp(s+1,' ');
					cout << "|";
				}
				if(i!=al-1)cout << " ";
			}
			cout << "\n";
		}
		for(int i=0;i<al;++i){
			if(a[i]=='0'||a[i]=='1'||a[i]=='7')
				sp(s+2,' ');
			else{
				cout << " ";
				sp(s,'-');
				cout << " ";
			}
			if(i!=al-1)cout << " ";
		}
		cout << "\n";
		sss=s;
		while(sss--){
			for(int i=0;i<al;++i){
				if(a[i]=='0'||a[i]=='6'||a[i]=='8'){
					cout << "|";
					sp(s,' ');
					cout << "|";
				}
				else if(a[i]=='2'){
					cout << "|";
					sp(s+1,' ');
				}
				else{
					sp(s+1,' ');
					cout << "|";
				}
				if(i!=al-1)cout << " ";
			}
			cout << "\n";
		}
		for(int i=0;i<al;++i){
			if(a[i]=='1'||a[i]=='4'||a[i]=='7')sp(s+2,' ');
			else{
				cout << " ";
				sp(s,'-');
				cout << " ";
			}
			if(i!=al-1)cout << " ";
		}
		cout << "\n\n";
	}
}

Discussion