OJ-HDU 叠筐

                                            叠筐

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24967    Accepted Submission(s): 6594

Problem Description

需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。

 Input

输入是一个个的三元组,分别是,外筐尺寸n(n为满足0

 Output

输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。

 Sample Input

11 B A

5 @ W

 Sample Output

 AAAAAAAAA 
ABBBBBBBBBA 
ABAAAAAAABA 
ABABBBBBABA 
ABABAAABABA 
ABABABABABA 
ABABAAABABA 
ABABBBBBABA 
ABAAAAAAABA 
ABBBBBBBBBA 
 AAAAAAAAA 

 @@@ 
@WWW@ 
@W@W@ 
@WWW@ 
 @@@
Author
qianneng
Source
浙江工业大学网络选拔赛

思路

1.把这个图分为两半,只需要模拟出上一半就行

2.拿第一个案例举例:

1)发现最外框的字母跟(n+1)/2有关,其实偶数是外框花色字符,奇数是中心花色字符,这里交换一下字符,从i==0开始都打印中心花色字符

2)i==0和i==1事先都可以知道,模拟之后的就行

3)当i>1时,替换i到(n-i)之间的字符,偶数打印的是外框花色字符,奇数打印中心花色字符

AcCode:

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int count = 0;
		while (in.hasNext()) {
			int n = in.nextInt();
			if(count!=0) {
				System.out.println();
			}
			char centerChar = in.next().charAt(0);
			char outChar = in.next().charAt(0);
			if(n==1) {
				System.out.println(centerChar);
				continue;
			}
			String[] dk = new String[(n + 1) / 2];
			char temp;
			if (((n + 1) / 2) % 2 == 0) {
				temp = centerChar;
				centerChar = outChar;
				outChar = temp;
			}
			StringBuilder builder = new StringBuilder();
			for (int i = 0; i < n; i++) {
				if (i == 0 || i == n - 1) {
					builder.append(" ");
				} else {
					builder.append(centerChar);
				}
			}
			dk[0] = builder.toString();
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < n; i++) {
				if (i == 0 || i == n - 1) {
					stringBuilder.append(centerChar);
				} else {
					stringBuilder.append(outChar);
				}
			}
			for (int i = 1; i < dk.length; i++) {
				if(i==1) {
					dk[i] = stringBuilder.toString();
				}else {
					StringBuilder str = new StringBuilder();
					char pj;
					if(i%2==0) {
						pj = centerChar;
					}else {
						pj = outChar;
					}
					for (int j = i; j < n-i; j++) {
						str.append(pj);
					}
					stringBuilder.replace(i, n-i, str.toString());
					dk[i] = stringBuilder.toString();
				}
			}
			for (int i = 0; i < dk.length; i++) {
				System.out.println(dk[i]);
			}
			for (int i = dk.length-2; i>=0; i--) {
				System.out.println(dk[i]);
			}
			count++;
		}

	}
}

 

你可能感兴趣的:(HDU)