easyBuild-up Logical Thinking
Pattern: Rectangular Star
Print n rows, each containing n stars without spaces between stars.
No external exercise
Work through the cases
3 examplesInput notation
An integer n from 1 through 30.Expected result
The requested pattern, with one row per line.Case 1
Input
3
Expected output
*** *** ***
Case 2
Input
1
Expected output
*
Case 3
Input
2
Expected output
** **
These notes use simplified input/output notation. The linked platform defines the method signature and judge format.
FROM IDEA TO IMPLEMENTATION
Understand the approach.
Compare the documented approaches below. Brute force, better, and optimal versions appear where available; a direct exercise may need only one approach.
Compute the leading spaces and star count for each row. Every printed character is necessary. Time: O(n²); the implementation buffers at most one row, O(n) space.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Long> values = new ArrayList<>();
while (in.hasNextLong()) values.add(in.nextLong());
long[] a = values.stream().mapToLong(Long::longValue).toArray();
long n = a[0];
for (long i = 0; i < n; i++) System.out.println(" ".repeat((int)(0)) + "*".repeat((int)(n)));
}
}
Reference code. Platform solutions may use supplied node types, imports, or collection libraries; foundational examples are complete programs.