2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛1007

Windows Of CCPC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2830 Accepted Submission(s): 2018

Problem Description

In recent years, CCPC has developed rapidly and gained a large number of competitors .One contestant designed a design called CCPC Windows .The 1-st order CCPC window is shown in the figure:

And the 2-nd order CCPC window is shown in the figure:

We can easily find that the window of CCPC of order k is generated by taking the window of CCPC of order k−1 as C of order k, and the result of inverting C/P in the window of CCPC of order k−1 as P of order k.
And now I have an order k ,please output k-order CCPC Windows , The CCPC window of order k is a 2k∗2k matrix.

Input

The input file contains T test samples.(1<=T<=10)

The first line of input file is an integer T.

Then the T lines contains a positive integers k , (1≤k≤10)

Output

For each test case,you should output the answer .

Sample Input

3
1
2
3

Sample Output

CC//看这些即可
PC
CCCC
PCPC
PPCC
CPPC
CCCCCCCC
PCPCPCPC
PPCCPPCC
CPPCCPPC
PPPPCCCC
CPCPPCPC
CCPPPPCC
PCCPCPPC

思路:耐心找规律。每一个图分为四个部分。 其中左下角与模板取反,其余与模板不变。
考察:二维数组的赋值。
代码

#include
#include
#include
using namespace std;
char a[1025][1025];
int main() {
     
    int T;
    cin >> T;
    int k;
    while(T--) {
     
        int n;
        cin >> n;
        k = 1;
         memset(a, 'C', sizeof(a));
        while(n--) {
     
            k *= 2;
            for(int i = 0; i < k; i++)
                for(int j = 0; j < k; j++)
                    if(i < k / 2 && j >= k / 2)
                        a[i][j] = a[i][j - k / 2];
                    else if(i >= k / 2 && j >= k / 2)
                        a[i][j] = a[i - k / 2][j - k / 2];
                    else if(i >= k / 2 && j < k / 2) {
     
                        a[i][j] = a[i - k / 2][j];
                    }
            for(int i = 0; i < k; i++)
                for(int j = 0; j < k; j++)
                    if(i >= k / 2 && j < k / 2) {
     
                        if(a[i][j] == 'C')
                            a[i][j] = 'P';
                        else
                            a[i][j] = 'C';
                    }


        }
        for(int i = 0; i < k; i++) {
     
            {
     for(int j = 0; j < k; j++)
                cout << a[i][j];}
        cout << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(思维题)