AcWing 118. 提供一个非递归的版本
原题链接
简单
作者:
Windspe
,
2019-03-27 11:18:58
,
所有人可见
,
阅读 2032
C++ 代码
#include <iostream>
#include <string>
using namespace std;
int solve(int n)
{
int len = 1;
for (int i = 0; i < n; i++)
len *= 3;
for (int i = 0; i < len; i++) {
int line = i;
int cur_len = 1;
string cur_str = "X";
for (int j = 0; j < n; j++) {
int cur_loc = line % 3;
if (cur_loc == 1)
cur_str = string(cur_len, ' ') + cur_str + string(cur_len, ' ');
else
cur_str = cur_str + string(cur_len, ' ') + cur_str;
line /= 3;
cur_len *= 3;
}
cout << cur_str << endl;
}
}
int main()
{
int n;
while (true) {
cin >> n;
if (n == -1)
break;
solve(n-1);
cout << "-" << endl;
}
}