Learning job record
This question asks you to write a program to print a given symbol into an hourglass shape. 17 example given, "*", the following format printing requires
The so-called "hourglass shape" refers to each row outputs odd symbols; symbol center of each row are aligned; the difference between the number of symbols two adjacent rows 2; the number of symbols in descending order of descending to the first one, then the order of small to large increments; inclusive symbol equal numbers.
Given any N symbols, not necessarily exactly composed of an hourglass. Requirements can be printed out as much of the hourglass symbol.
Input formats:
Input gives a positive integer N (≤1000) and a symbol in a row, separated by a space.
Output formats:
First print largest hourglass shape consisting of a given symbol, the symbol number output last remaining useless fall in a row.
Sample input:
19 *
Sample output:
Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in) ;
int N = scanner.nextInt() ;
String c =scanner.next();
int num ,sum ,s;
for(num = 1 ; ; num++){
if(2 * num*(num + 2) + 1 > N){
num--;
break;
}
}
s=2*num+1;
for(int r=0;r<s;r++) {
if (r <= num) {
for (int a = 0; a < s-r; a++) {
if (a < r) {
System.out.print(' ');
} else {
System.out.print(c);
}
}
} else {
for (int a = 0; a < r+1; a++) {
if (a < s-r-1) {
System.out.print(' ');
} else {
System.out.print(c);
}
}
}
System.out.println();
}
sum=2 * num*(num + 2) + 1;
if(N>=sum){
System.out.print(N-sum);
}
}
}