[Java] PTA group programming TIANTI competition exercise set (2018)

Keywords: Java

L1-001 Hello World (5 points)

This super simple question has no input.

You just need to output the famous phrase "Hello World!" in one line.

/**
 * @author let alone
 * @Description of XXX
 * @ClassName L1_001_Hello_World
 * @create 2018/8/9 20:44
 * @since 1.0.0
 */
public class Main
{
    public static void main(String[] args)
    {
        System.out.print("Hello World!");
    }
}

L1-002 print Hourglass (20 points)

This question requires you to write a program to print the given symbol into the shape of an hourglass. For example, given 17 "*", it is required to print in the following format

*****
 ***
  *
 ***
*****

The so-called "hourglass shape" refers to the output of odd symbols in each line; the center of symbols in each line is aligned; the difference between the number of symbols in two adjacent lines is 2; the number of symbols first decreases from large to small to 1, then increases from small to large; the number of first and last symbols is equal.

Given any N symbols, an hourglass may not be formed exactly. It is required that the printed hourglass can use as many symbols as possible.

Input format:

The input gives a positive integer N (< 1000) and a symbol on a line, separated by spaces.

Output format:

First, print out the largest hourglass shape composed of the given symbols, and finally output the remaining unused symbols in one line.

Input example:

19 *

Output example:

*****
 ***
  *
 ***
*****
2

 

import java.util.Scanner;

/**
 * @author let alone
 * @Description of XXX
 * @ClassName L1_002_Hourglass
 * @create 2018/8/9 20:59
 * @since 1.0.0
 */
public class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        int n = input.nextInt();
        char c = input.next().charAt(0);

        int row = (int)Math.sqrt((double)((n + 1) / 2));
        /**
         * @Author let alone
         * @Description The characteristic of odd sum is to find out the total number k of the symbols in the upper half, and to get the number of trips by the root of K
         * @Date 21:49 2018/8/9
         **/
        int sum = 0;

        for(int i = 0; i < row; i++)
        {
            int m = 2 * (row - i) - 1;
            /**
             * @Author let alone
             * @Description The number of symbols per line is: 2 * row - 1
             * @Date 21:52 2018/8/9
             **/
            for(int j = 0; j < i; j++)
            {
                System.out.print(" ");
            }
            for(int j = 0; j < m; j++)
            {
                System.out.print(c);
            }
            System.out.println();
            sum += m;
        }
        for(int i = 1; i < row; i++)
        {
            int m = 2 * (i + 1) - 1;
            for(int j = 0; j < row - 1 - i; j++)
            {
                System.out.print(" ");
            }
            for(int j = 0; j < m; j++)
            {
                System.out.print(c);
            }
            System.out.println();
            sum += m;
        }
        System.out.print(n - sum);
    }
}

 

Posted by finkrattaz on Sun, 05 Jan 2020 01:44:34 -0800