打印图形
时间限制: 1.0s 内存限制: 512.0MB
【问题描述】
小明刚学习完条件语句和循环语句,并且也打印了许多图形,比如菱形或者三角形。然后他突发奇想要打印一个六芒星,果然,他用了半小时就把六芒星给打印出来了,你能比他更快吗?小明为你加油哦!
【输入格式】
输入一行包含一个整数 n。
【输出格式】
输出一个六芒星,表示满足题意的图形。
【样例输入】
3
4
【样例输出】
【评测用例规模与约定】
对于所有评测用例,1 ≤ n ≤ 20。
class Main{
static char [][]array=new char[20*20-3][((20*3)-2)*2-1];
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
while(sc.hasNext()) {
int n=sc.nextInt();
int length=((n*3)-2)*2-1;
int width=n*3-2;
clear(width,length,n);
f(width,length,n);
print(width,length,n);
}
}
public static void f(int width,int length,int n) {
int i=0;
int k=width+n-2;
int mid1=length/2;
int mid2=length/2;
while(i<width&&mid2>=0) {
array[i][mid1]='*';
array[i][mid2]='*';
array[k][mid1]='*';
array[k][mid2]='*';
mid1++;
mid2--;
i++;
k--;
}
i=0;
while(i<length) {
array[n-1][i]='*';
array[width-1][i]='*';
i+=2;
}
}
public static void print(int width,int length,int n) {
for(int i=0;i<width+n-1;i++) {
for(int j=0;j<length;j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
public static void clear(int width,int length,int n) {
for(int i=0;i<width+n-1;i++) {
for(int j=0;j<length;j++) {
array[i][j]=' ';
}
}
}
}