首页 > 试题广场 >

跟奥巴马一起编程(15)

[编程题]跟奥巴马一起编程(15)
  • 热度指数:13755 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算

机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!

说明:请严格按照题面说明输出即可,不必与样例格式对应

输入描述:
输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。


输出描述:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%
(四舍五入取整)。
示例1

输入

10 a

输出

aaaaaaaaaa
a        a
a        a
a        a
aaaaaaaaaa
import java.util.Scanner;
public class Main {
    public static void func(char ch,int n) {
        for (int i = 0; i <n/2+n%2; i++) {
            if(i == 0||i ==n/2+n%2-1) {
                for (int j = 0; j < n; j++) {
                    System.out.print(ch);
                }
            }else {
                for (int j = 0; j < n; j++) {
                    if(j == 0||j ==n-1) {
                        System.out.print(ch);
                    }else {
                        System.out.print(" ");
                    }
                }
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] strings = str.split(" ");
        char ch = strings[1].charAt(0);
        int n = Integer.parseInt(strings[0]);
        func(ch,n);
    }
}
发表于 2020-06-18 22:20:39 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int N = scanner.nextInt();
		
		char c = scanner.next().charAt(0);
		
		for (int i = 0; i < (N + 1) / 2; i++) {
			for (int j = 0; j < N; j++) {
				if(i == 0 || j == 0 || i == ((N+1)/2 - 1) || j == N-1) {
					System.out.print(c);
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}
非常基础的题目
发表于 2020-02-26 12:03:52 回复(0)
      Java实现,已通过。 主要是“行数是N/2四舍五入的结果”,可以用Math.round( )。代码如下:
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int N=in.nextInt();
       char C=in.next().charAt(0);
     for(int i=0;i<Math.round((float) N/2.0);i++)
     {
         if(i%((N-1)/2)==0)
         {for (int j=0;j<N;j++)
             System.out.print(C);
             System.out.println();
         continue;}
         for(int k=0;k<N;k++)
         {if(k%(N-2)==0)
             System.out.print(C);
         System.out.print(" ");}
             System.out.println();
     }
    }
}


发表于 2020-02-09 10:38:38 回复(0)
import java.util.Scanner;

public class WithObaMaProgram {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int a = sc.nextInt();
            String s = sc.next();
            for (int i = 0; i < a; i++) {
                System.out.print(s);
            }
            System.out.println();
            for (int i = 0; i < Math.ceil(a) / 2 - 2; i++) {
                System.out.print(s);
                for (int j = 0; j < a - 2; j++) {
                    System.out.print(" ");
                }
                System.out.println(s);
            }
            for (int i = 0; i < a; i++) {
                System.out.print(s);
            }
        }
    }
}

发表于 2019-11-20 22:20:41 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int size=input.nextInt();
        char c=input.next().charAt(0);
        char[][] arr=new char[size/2+size%2][size];
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){

                arr[i][j]=c;
            }
        }
        for(int i=1;i<arr.length-1;i++){
            for(int j=1;j<arr[i].length-1;j++){
                arr[i][j]=' ';
            }
        }
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
}


发表于 2019-11-20 18:19:13 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String str = in.next();
        int length = 0;
        if(n%2==0){
            length = n/2;
        }else{
            length = n/2 + 1;
        }
        for(int i=0;i<length;i++) {
            for (int j = 0; j < n; j++) {
                if (i== 0 || i == (length - 1)) {
                    int s = n - 1;
                    while (s >= 0) {
                        System.out.print(str);
                        s--;
                    }
                    System.out.println();
                    break;
                } else {
                    if (j == 0) {
                        System.out.print(str);
                    } else if (j == (n - 1)) {
                        System.out.println(str);
                    } else {
                        System.out.print("s");
                    }
                }
            }
        }
    }
}
发表于 2018-05-04 23:55:08 回复(0)

import java.util.Scanner;
public class Pat_1026 {  private int N;//长方形边长  private String symbol;//符号  private int row;//除去上下边后的行数
//获取边长、符号、计算row
 public Pat_1026() {   Scanner sc = new Scanner(System.in);   this.N = sc.nextInt();   this.symbol = sc.next();   this.row = N / 2 - 2;   if (N % 2 != 0)    this.row = N / 2 + 1 - 2;   sc.close();  }
 public void paint() {   // 画行   for (int i = 0; i < N; i++) {    System.out.print(symbol);   }   System.out.println();   // 画列   for (int j = 0; j < row; j++) {    for (int count = 0; count < N ; count++) {
    //在第一列与最后一列画符号,注意,写空格需要使用else,如果没有else,需要修改count的    //值,且要注意每行最后面是否还会多打印一个空格,多打印的话,格式错误     if (count == 0 || count == N - 1)      System.out.print(symbol);     else      System.out.print(" ");    }    System.out.println();   }   // 画行   for (int i = 0; i < N; i++) {    System.out.print(symbol);   }  }
 public static void main(String[] args) {   Pat_1026 main = new Pat_1026();   main.paint();  } }

发表于 2018-03-17 17:34:48 回复(0)
importjava.util.Scanner;
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner in  = newScanner(System.in);
        intN = in.nextInt();
        charc = in.next().charAt(0);
        ToSquare(N, c);
    }
    publicstaticvoidToSquare(intn, charc){
         
        for(inti = 1; i <= (n+1)/2; i++){
            for(intj = 1; j <= n; j ++){
                if(i == 1|| i == (n+1)/2){
                    System.out.print(c);
                }else{
                    if(j==1|| j==n){
                        System.out.print(c);
                    }else{
                        System.out.print(" ");
                    }
                }
            }
            System.out.println();
        }
    }
}

发表于 2016-09-16 22:41:00 回复(0)

问题信息

难度:
10条回答 18522浏览

热门推荐

通过挑战的用户