http://acm.tju.edu.cn/toj/showp1031.html
这道题参考:https://blog.csdn.net/aacm1992/article/details/7818652
之前在数字逻辑电路的课上学过7段数字显示的相关知识,可以应用到这道题。将每一个数字看作是由7段组成的,然后根据具体的数字对应起来哪一段“亮”。
此外,还学到了memset的相关知识:https://www.cnblogs.com/heyonggang/p/3419574.html
memset(MAP, 0, sizeof(MAP));
上面这句话代表将MAP数组的所有内容都设为ASCII码中的0(空字符),常用于内存空间初始化。
此外,这道题还需要注意的是'-'和'|'之间的位置关系、每个数字之间的位置关系。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char MAP[200][200];
char Display(int s, int num, int pos){
//左上“竖”
if(num == 0 || num == 4 || num == 5 || num == 6 || num == 8 || num == 9){
for(int i = 1; i < s+1; i++){
MAP[i][pos] = '|';
}
}
//上“横”
if(num == 0 || num == 2 || num == 3 || num == 5 || num == 6 || num == 7 || num == 8 || num == 9){
for(int j = pos+1; j < s+pos+1; j++){
MAP[0][j] = '-';
}
}
//右上“竖”
if(num == 0 || num == 1 || num == 2 || num == 3 || num == 4 || num == 7 || num == 8 || num == 9){
for(int i = 1; i < s+1; i++){
MAP[i][pos+s+1] = '|';
}
}
//中间“横”
if(num == 2 || num == 3 || num == 4 || num == 5 || num == 6 || num == 8 || num == 9){
for(int j = pos+1; j < s+pos+1; j++){
MAP[s+1][j] = '-';
}
}
//左下“竖”
if(num == 0 || num == 2 || num == 6 || num == 8){
for(int i = s+2; i < 2*s+2; i++){
MAP[i][pos] = '|';
}
}
//右下“竖”
if(num == 0 || num == 1 || num == 3 || num == 4 || num == 5 || num == 6 || num == 7 || num == 8 || num == 9){
for(int i = s+2; i < 2*s+2; i++){
MAP[i][pos+s+1] = '|';
}
}
//下“横”
if(num == 0 || num == 2 || num == 3 || num == 5 || num == 6 || num == 8 || num == 9){
for(int j = pos+1; j < s+pos+1; j++){
MAP[2*s+2][j] = '-';
}
}
}
int main(){
int s;
char n[9];
while(scanf("%d %s", &s, &n)){
if(s == 0 && n[0] == '0' && n[1] == '\0'){
break;
}
int len = strlen(n);
//对二维数组初始化
memset(MAP, 0, sizeof(MAP));
for(int i = 0; i < 2*s+3; i++){
for(int j = 0; j < len*(s+3)-1; j++){
MAP[i][j] = ' ';
}
}
for(int i = 0; i < len; i++){
Display(s, n[i]-'0', (s+3)*i);
}
for(int i = 0; i < 2*s+3; i++){
printf("%s\n", MAP[i]);
}
printf("\n");
}
return 0;
}