PAT乙级:1027 打印沙漏(JAVA)

import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		String fu = reader.next();
		int yu = 0;
		int count = 2;
		n=n-1;
		while(true) {
			int judge = 2*(2*count -1);
			if(n>=judge) {
				n-=judge;
				count++;
			}else {
				yu=n;
				break;
			}
		}
		int cheng = count - 1;
		for(int i = cheng;i>=1;i--) {
			for(int j=0;j<cheng-i;j++) {
				System.out.print(" ");
			}
			for(int j=0;j<(2*i-1);j++) {
				System.out.print(fu);
			}
			System.out.println();
		}
		for(int i=2;i<=cheng;i++) {
			for(int j=0;j<cheng-i;j++) {
				System.out.print(" ");
			}
			for(int j=0;j<(2*i-1);j++) {
				System.out.print(fu);
			}
			System.out.println();
		}
        System.out.println(yu);

PAT乙级:1027 打印沙漏(JAVA)_第1张图片
测试点0和测试点3格式错误原因:
号后面不能由空格输完号后就要换行,不然会有格式错误

你可能感兴趣的:(PAT乙级)