hdu-6708(打表)

Windows Of CCPC

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

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

思路:

对于行列数为2k 的矩阵,它的左上、右上、右下均为2k-1 的矩阵,左下为2k-1 的逆矩阵。那么打表时,每次都从k=2开始做起,填补每一级K的右上右下左下,注意初始化k=1的表。循环的方法可以是:

for(每一个k次的矩阵){
	int x=(1<

这样可以比较方便地处理。以下为AC代码:

import java.io.PrintWriter;
import java.util.Scanner;

public class Main{
     
	public static void main(String args[]) {
     
		Scanner sc=new Scanner(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int T,n,maxn=(1<<12);
		StringBuffer s[]=new StringBuffer[maxn];
		T=sc.nextInt();
		while(T-->0) {
     
			n=sc.nextInt();
			for(int i=0;i<maxn;i++) {
     
				s[i]=new StringBuffer();
			}
			s[0].append("CC");
			s[1].append("PC");
			
			for(int k=2;k<=n;k++) {
     
				int x=(1<<k);
				for(int i=0;i<x;i++) {
     
					for(int j=0;j<2;j++) {
     
						if(i<x/2&&j==1) {
     
							s[i].append(s[i]);
						}else if(i>=x/2&&j==0) {
     
							for(int l=0;l<x/2;l++) {
     
								char ch=s[i-x/2].charAt(l);
								if(ch=='C') {
     
									s[i].append("P");
								}else {
     
									s[i].append("C");
								}
								
							}
						}else if(i>=x/2&&j==1) {
     
							for(int l=0;l<x/2;l++) {
     
								s[i].append(s[i-x/2].charAt(l));
							}
						}
					}
				}
			}
			for(int i=0;i<(1<<n);i++) {
     
				for(int j=0;j<(1<<n);j++) {
     
					out.print(s[i].charAt(j));
				}
				out.println();
			}
			out.flush();
		}
		out.close();
	}

}

你可能感兴趣的:(打表)