http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2423
题目大意:
给n画叉
- n=1
X
- n=2
X X X X X
- 用n-1的叉叉图表示n的叉叉图
B(n - 1) B(n - 1) B(n - 1) B(n - 1) B(n - 1)
解题思路:
n最大只到7,预处理一下所有n的字符串保存起来,然后根据输入的n输出字符串即可。
要注意的是行末不可以有多余空格,否则会PE。可以处理完n=1..7所有字符串后再对每一行去末尾空格。
源代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
freopen("ans.txt", "w", stdout);
string b[8][3000];
string space[8];
int line[8], n;
b[1][0]="X";
space[2]=" ";
line[1]=1; line[2]=3;
for (int i=3; i<=7; i++)
{
space[i]=space[i-1]+space[i-1]+space[i-1];
line[i]=line[i-1]*3;
}
for (int i=2, lh=1; i<=7; i++, lh*=3)
{
for (int j=0, k=2*lh; j<lh; j++, k++)
{
b[i][j]=b[i-1][j]+space[i]+b[i-1][j];
b[i][k]=b[i][j];
}
for (int j=lh; j<2*lh; j++)
b[i][j]=space[i]+b[i-1][j-lh]+space[i];
}
for (int i=1; i<=7; i++)
for (int j=0, k; j<line[i]; j++)
{
string tmp=b[i][j];
for (k=tmp.length()-1; k>=0; k--)
if (tmp[k]!=' ') break;
b[i][j].assign(tmp, 0, k+1);
}
while (scanf("%d", &n)==1 && n!=-1)
{
for (int i=0; i<line[n]; i++)
cout<<b[n][i]<<endl;
printf("-\n");
}
return 0;
}
解题心得:
PE了两次,太不小心了。其实都应该打到记事本看一看的。。。