PTA试题2----沙漏问题
import java.util.Scanner;
public class Sand_Clock {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt(); //给定符号的总个数
String symbol = scan.next(); //给定符号
scan.close();
int record = 0;
boolean bool = false; //判断给定的N是奇数还是偶数 奇数需+1
if(N % 2 != 0) {
N += 1;
bool = true;
}
int line =0; //以沙漏中间为界 上半部分加中间的一行的行数
int num = 0; //以沙漏中间为界 上半部分加中间的一个*的个数
for(int i = 1; ;i+=2) { // 计算 上半部分*的个数
num = num + i;
if(num > N / 2) {
break;
}
line++;
}
for(int i = 0; i < line; i++) { //确定行数
for(int q = 0; q < i; q++) { // 左空格三角
System.out.print(" ");
}
for(int j = 2 * line -1; j > 2 * i; j--) { //上半部沙漏
System.out.print(symbol);
}
System.out.println();
}
for(int i = line; i >1; i--) { //下半部沙漏加空白
for(int q = i; q > 2; q--) {
System.out.print(" ");
}
for(int j = 2 * line; j >= 2 * i - 2; j--) {
System.out.print(symbol);
}
System.out.println();
}
record = N / 2 - num + 2 * line + 1;
if(record == 0) {
System.out.println(record);
} else if (bool) {
record *= 2;
System.out.println(record);
} else {
record = 2 * record + 1;
System.out.println(record);
}
}
}