NC214739. 原马蹄印
描述
输入描述
因为要复制出大小各异的原马蹄印,于是小鹏随机生成了许多数字。然后他将这些数字依次输入到科技武器中,每次都只有一个不大于50的正整数,直到输入EOF结束。
输出描述
小鹏的科技武器会将对应大小的原马蹄印输出(注意在每一行的开头会输出一个'|',以此来表示该行的开始)。
示例1
输入:
1 2 3 4
输出:
| * | ***.*** |***..*..*** |**..*.*..** |**.*...*.** |**.*...*.** |**..*.*..** |***..*..*** | ***.*** | * | . | ....*.... |....***.***.... |..***..*..***.. |..**..*.*..**.. |..**.*...*.**.. |..**.*...*.**.. |..**..*.*..**.. |..***..*..***.. |....***.***.... | ....*.... | . | * | *****.***** |*****....*....***** |**....***.***....** |**..***..*..***..** |**..**..*.*..**..** |**..**.*...*.**..** |**..**.*...*.**..** |**..**..*.*..**..** |**..***..*..***..** |**....***.***....** |*****....*....***** | *****.***** | * | . | ......*...... |......*****.*****...... |..*****....*....*****.. |..**....***.***....**.. |..**..***..*..***..**.. |..**..**..*.*..**..**.. |..**..**.*...*.**..**.. |..**..**.*...*.**..**.. |..**..**..*.*..**..**.. |..**..***..*..***..**.. |..**....***.***....**.. |..*****....*....*****.. |......*****.*****...... | ......*...... | .
C(clang11) 解法, 执行用时: 14ms, 内存消耗: 1400K, 提交时间: 2020-12-26 16:00:25
#include<stdio.h> #include<string.h> void work(int size,int line) { int mline=8+size*2; int i; if(size==0) { if(line==1||line==8) printf("."); if(line==2||line==7) printf("..*.."); if(line==3||line==6) printf("..*.*.."); if(line==4||line==5) printf(".*...*."); } else { if(line==1||line==mline) { if(size%2==1) printf("*"); else printf("."); } else if(line==2||line==3||line==mline-1||line==mline-2) { if(size%2==1) { for(i=0; i<2+size; i++) printf("*"); work(size-1,line-1); for(i=0; i<2+size; i++) printf("*"); } else { for(i=0; i<2+size; i++) printf("."); work(size-1,line-1); for(i=0; i<2+size; i++) printf("."); } } else if(size%2==1) { printf("**"); work(size-1,line-1); printf("**"); } else { printf(".."); work(size-1,line-1); printf(".."); } } } int main(void) { int n,line,mline,i; while((scanf("%d",&n))!=EOF) { mline=8+n*2; for(line=1; line<=mline; line++) { printf("|"); if(line<3||line>mline-2) for(i=0; i<n+1; i++) printf(" "); if(line==1||line==mline) for(i=0; i<n+2; i++) printf(" "); work(n,line); if(line<3||line>mline-2) for(i=0; i<n+1; i++) printf(" "); if(line==1||line==mline) for(i=0; i<n+2; i++) printf(" "); printf("\n"); } } return 0; }
C++(clang++11) 解法, 执行用时: 12ms, 内存消耗: 1860K, 提交时间: 2020-12-26 14:56:55
#include <stdio.h> char s[300][2000]; int t[300]; int main() { int n; while (scanf("%d", &n) != EOF) { n += 3; for (int i = 0; i < 200; ++ i) { for (int j = 0; j < 1500; ++ j) { s[i][j] = ' '; } t[i] = 0; } s[3][0] = '.'; s[2][0] = '*';s[2][1] = '.';s[2][2] = '.'; s[1][0] = '.';s[1][1] = '*';s[1][2] = '.';s[1][3] = '.'; s[0][0] = '.';s[0][1] = '.';s[0][2] = '*';s[0][3] = '.'; t[0] = 4; t[1] = 4; t[2] = 3; t[3] = 1; for (int i = 4; i <= n; ++ i) { char ch; if (i & 1) ch = '.'; else ch = '*'; s[i][t[i]++] = ch; for (int j = 0; j < i-1; ++ j) { s[i-1][t[i-1]++] = ch; } for (int j = 0; j < i-1; ++ j) { s[i-2][t[i-2]++] = ch; } for (int j = i-3; j >= 0; -- j) { for (int k = 0; k < 2; ++ k) { s[j][t[j]++] = ch; } } } for (int i = n; i >= 0; -- i) { printf("|"); for (int j = (n-2)*2+1; j > 0; -- j) { printf("%c", s[i][j]); } for (int j = 0; j < t[i]; ++ j) { printf("%c", s[i][j]); } printf("\n"); } for (int i = 0; i <= n; ++ i) { printf("|"); for (int j = (n-2)*2+1; j > 0; -- j) { printf("%c", s[i][j]); } for (int j = 0; j < t[i]; ++ j) { printf("%c", s[i][j]); } printf("\n"); } } return 0; }
pypy3(pypy3.6.1) 解法, 执行用时: 79ms, 内存消耗: 50228K, 提交时间: 2020-12-26 14:35:57
source = [ ' * ', ' ***.*** ', '***..*..***', '**..*.*..**', '**.*...*.**', '**.*...*.**', '**..*.*..**', '***..*..***', ' ***.*** ', ' * ', ] ans = [None, source] a, b = '.', '*' res = source.copy() for i in range(2, 55): res[0] = ' ' * (i + 1) + a * (i + 2) + b + a * (i + 2) + ' ' * (i + 1) res[-1] = res[0] for j in range(1, len(res) - 1): res[j] = a * 2 + res[j].replace(' ', a) + a * 2 push = ' ' * (2 * i + 3) + a + ' ' * (2 * i + 3) res = [push] + res + [push] a, b = b, a ans.append(res.copy()) try: while True: n = int(input()) for s in ans[n]: print('|' + s) except EOFError: pass