美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!
输入格式:
输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。
输出格式:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。
输入样例:
10 a
输出样例:
aaaaaaaaaa a a a a a a aaaaaaaaaa
1 package com.hone.basical; 2 3 import java.util.Scanner; 4 5 /** 6 * 原题目:https://www.patest.cn/contests/pat-b-practise/1036 7 * @author Xia 注意点:四舍五入 8 * 核心:利用for循环来控制输出 9 */ 10 public class basicalLevel1036programWithObama { 11 12 public static void main(String[] args) { 13 Scanner s = new Scanner(System.in); 14 int n = s.nextInt(); 15 String e = s.next(); 16 int col = (int) Math.round(n / 2.0); 17 for (int j = 0; j < n; j++) { 18 System.out.print(e); 19 } 20 System.out.println(); 21 for (int i = 1; i < col - 1; i++) { 22 System.out.print(e); 23 for (int j = 0; j < n - 2; j++) { 24 System.out.print(" "); 25 } 26 System.out.println(e); 27 } 28 for (int j = 0; j < n; j++) { 29 System.out.print(e); 30 } 31 System.out.println(); 32 } 33 }