题目如下:
The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is:
Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2k vectors in 2k-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?
The only line of the input contains a single integer k (0 ≤ k ≤ 9).
Print 2k lines consisting of 2k characters each. The j-th character of the i-th line must be equal to ' * ' if the j-th coordinate of the i-th vector is equal to - 1, and must be equal to ' + ' if it's equal to + 1. It's guaranteed that the answer always exists.
If there are many correct answers, print any.
2
++** +*+* ++++ +**+
思路:这道题的关键在于要能发现n=i(i>0)的结果,可以由n=i-1的结果构造出来。即如果n=i-1时为A,那么n=i时,可以构造A A/A -A(-A就是A中每个取相反的符号,/表示换一行)。可以证明这是符合要求的。这样一来,这道题就简单了。
代码如下:
#include <iostream>
using namespace std;
const int maxn = 1<<11;
char s[maxn][maxn];
char op(char c){
if (c == '*') {
return '+';
}else return '*';
}
int main(int argc, const char * argv[]) {
int n;
cin >> n;
s[1][0] = '+';s[1][1] = '\0';
int l = 1;
for (int i=0; i<n; i++) {//构造后面的结果
for (int j=1; j<=l; j++) {
for (int k=0; k<l; k++) {
s[j][k+l] = s[j][k];
}
s[j][l*2] = '\0';
}
for (int j=l+1; j<=2*l; j++) {
for (int k=0; k<l; k++) {
s[j][k] = s[j-l][k];
s[j][k+l] = op(s[j][k]);
}
s[j][l*2] = '\0';
}
l*=2;
}
for (int i=1; i<=l; i++) {
cout << s[i] << endl;
}
return 0;
}