题目:
输入描述:
输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。
输出描述:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%
(四舍五入取整)。
源码:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String s = in.next();
int row = 0;
if (N % 2 == 0) {
row = N / 2;
} else {
row = N / 2 + 1;
}
if (row == 2) {
for (int i = 0; i < N; i++) {
System.out.print(s);
}
System.out.println();
for (int i = 0; i < N; i++) {
System.out.print(s);
}
} else {
for (int i = 0; i < N; i++) {
System.out.print(s);
}
System.out.println();
for (int i = 0; i < row - 2; i++) {
System.out.print(s);
for (int j = 0; j < N - 2; j++) {
System.out.print(" ");
}
System.out.print(s);
System.out.println();
}
for (int i = 0; i < N; i++) {
System.out.print(s);
}
}
}
}