import java.util.Scanner;
/**
*http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3373&konwledgeId=40
*思路: 以中心为原点坐标,有一张无限大的图, 注意图中心对称*/
/*
区域1:1*1的部分,即原点
o
规则: 1区域中的点都是‘o’
区域2,包含了区域1
o
ooo
o
规则是: 四个角1*1的点都是‘ ’,
中间1*1的点由区域1负责,
上下左右1*1的点,由区域1的点平移1个单位而来,把上下左右平移回去,交给区域1判断是否为‘o’
区域3,包含了区域2
o
ooo
o
o o o
ooooooooo
o o o
o
ooo
o
规则是: 四个角的3*3的点都是‘ ’,
中间3*3个点是区域2负责,
上下左右3*3的点都是区域2平移3个单位而来,平移回去,交给区域2判断是否为‘o’
区域n,包含了区域n-1
规则是:四个角的3^(n-1)个点都是‘ ’,
中间3^(n-1)个点是区域n-1负责,
上下左右的点都是区域n-1平移3^(n-2)个单位而来,平移回去,交给区域n-1判断是否为‘o’
*/
public class Main {
private static final int POWER_OF3[] = new int[10];
private static final char T = 'o';
private static final char F = ' ';
static {
for(int i = 0;i<POWER_OF3.length;i++) {
POWER_OF3[i] = (int) Math.pow(3, i);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i=1;i<=t;i++) {
System.out.println("Case #"+i+":");
cross(in.nextInt()-1);
}
}
public static void cross(int floor) {
final int limit = POWER_OF3[floor]/2;
for(int y = limit; y >= -limit;y--) {
StringBuilder sb = new StringBuilder();
for(int x = -limit; x <= limit; x++) {
sb.append(getChar(x,y,floor));
}
System.out.println(sb);
}
}
private static char getChar(int x,int y,int n) {
if(n==0) {
return T;
}
int size = POWER_OF3[n-1];
int limit = size/2;
x = Math.abs(x);
y = Math.abs(y);
if(x<=limit) {
if(y<=limit) {
return getChar(x,y,n-1);
}else {
return getChar(x,y-size,n-1);
}
}else {
if(y<=limit) {
return getChar(x-size,y,n-1);
}else {
return F;
}
}
}
}
十字架(百度2017秋招真题)
最新推荐文章于 2020-07-29 11:57:14 发布