机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!
说明:请严格按照题面说明输出即可,不必与样例格式对应
输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%
(四舍五入取整)。
10 a
aaaaaaaaaa a a a a a a aaaaaaaaaa
import java.util.Scanner;
/**
* 跟奥巴马一起编程
* 题目描述
* 美国***奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的***。
* 2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。
* 现在你也跟他一起画吧!
* 输入描述:
* 输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。
* 输出描述:
* 输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的
* 行数实际上是列数的50%(四舍五入取整)。
* 输入例子:
* 10 a
* 输出例子:
* aaaaaaaaaa
* a a
* a a
* a a
* aaaaaaaaaa
*
* @author shijiacheng
* @date 2018/1/31
*/
public class B1026CodingWithObama {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String c = sc.next();
int row = N / 2;
if (N % 2 != 0) {
row = N / 2 + 1;
}
if (row == 2) {
for (int i = 0; i < N; i++) {
System.out.print(c);
}
System.out.println();
for (int i = 0; i < N; i++) {
System.out.print(c);
}
} else {
for (int i = 0; i < N; i++) {
System.out.print(c);
}
System.out.println();
for (int i = 0; i < row - 2; i++) {
System.out.print(c);
for (int j = 0; j < N - 2; j++) {
System.out.print(" ");
}
System.out.print(c);
System.out.println();
}
for (int i = 0; i < N; i++) {
System.out.print(c);
}
}
}
}
//个人感觉,没必要嵌套的那么复杂, #include <iostream> using namespace std; int main() { int N;cin >> N; char ch; cin >> ch; for(int i = 0; i<N; i++) cout << ch; cout << endl; int M = N/2; if(1.0*N/2 - M>=0.5) M++; for(int i = 0;i<M-2;i++){ cout << ch; for(int j = 1; j<N-1;j++) cout << ' '; cout << ch << endl; } for(int i = 0; i<N; i++) cout << ch; cout << endl; return 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(); } } }
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(); } } }
import java.util.*; public class Main{ public static void main(String args[]){ Scanner s = new Scanner(System.in); int cols = s.nextInt(); String c = s.next(); int rows = cols/2; if(cols%2!=0){ rows = cols/2+1;//四舍五入 } for(int i = 0 ; i < cols ; i++){//打印第一行 System.out.print(c); } System.out.print('\n');//换行 for(int i = 0 ; i < rows-2;i++){//打印中间 System.out.print(c); for(int j = 0 ; j < cols - 2 ; j++){ System.out.print(" ");//打印空格 } System.out.print(c); System.out.print('\n');//换行 } for(int i = 0 ; i < cols ; i++){//打印最后一行 System.out.print(c); } } }
//string大法好 #include <bits/stdc++.h> using namespace std; int main(){ int n; char c; scanf("%d %c",&n,&c); int row = (double)n*0.5 + 0.5 - 2;//中间的行数 string s(n,c),sp(n-2,' '); cout<<s<<endl; for(int i = 1; i <= row; i++) cout<<c<<sp<<c<<endl; cout<<s<<endl; return 0; }
#include<iostream> #include<cmath> using namespace std; int main() { int n; char c; cin >> n >> c; int m = (n + 1) / 2; for (int i = 1; i <= m; i++) { if (i == 1 || i == m) { for (int j = 1; j <= n; j++) { cout << c; } } else { for (int j = 1; j <= n; j++) { if (j == 1 || j == n) { cout << c; } else { cout << " "; } } } cout << endl; } return 0; }
//为什么不用string呢? #include <iostream> #include <string> using namespace std; int main() { int n;char c; while(cin>>n>>c) { int half=n/2.0+0.5; cout<<string(n,c)<<endl;//string的构造函数,初始化为n个c; for(int i=1;i<=half-2;i++) cout<<c<<string(n-2,' ')<<c<<endl; cout<<string(n,c)<<endl; } }
#include<stdio.h> int main (){//the shorter,the better. int r,c,i,j;char s; for(;~scanf("%d %c",&c,&s);printf("\n")){ for (r = (c+1)/2,i = 0; i < c;printf("%c",s),i++); for (printf("\n"),i = 0; i < r-2;printf("%c\n",s),i++)for (printf("%c",s),j = 0; j < c-2;printf(" "),j++); for (i = 0; i < c;printf("%c",s),i++); } }
四舍五入可以不用吗?-。-
#include <stdio.h> int main(){ unsigned char M, N, i, j, ch; scanf("%d %c", &N, &ch); M = N / 2 + (((int)((N / 2.0 - N / 2) * 10)) >= 5 ? 1 : 0); for (i = 0; i < M; i++){ for (j = 0 ; j < N; j++){ if (i == 0 || i == M - 1 || j == 0 || j == N - 1) putchar(ch); else putchar(' '); } putchar('\n'); } return 0; }
}
package 跟奥巴马一起编程; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); int N=sc.nextInt(); String c=sc.next(); for(int i=1;i<=N;i++){ System.out.print(c); } System.out.println(); for(int i=1;i<=Math.ceil((double)N/2)-2;i++){ System.out.print(c); for(int j=2;j<=N-1;j++){ System.out.print(" "); } System.out.println(c); } for(int i=1;i<=N;i++){ System.out.print(c); } } }
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#define MAX 20intmain(){charc;intnum;intline;inti, j;scanf("%d %c", &num, &c);for(i = 0; i < num; i++){printf("%c", c);}printf("\n");if(num % 2)line = num / 2+ 1;elseline = num / 2;line = line - 2;for(i = 0; i < line; i++){printf("%c", c);for(j = 0; j < num - 2; j++){printf(" ");}printf("%c\n", c);}for(i = 0; i < num; i++){printf("%c", c);}return0;}
#include<iostream> using namespace std; int main() { int n,k; char a[1001],b[1001]; char c; cin>>n>>c; for(int i=1;i<=n;i++) { a[i]=c; b[i]=' '; } for(int i=1;i<=(n+1)/2;i++) { if(i==1||i==(n+1)/2) { for(int j=1;j<=n;j++) { cout<<a[j]; } } else { b[1]=c; b[n]=c; for(int j=1;j<=n;j++) { cout<<b[j]; } } cout<<endl; } return 0; }
//跟奥巴马一起编程
//思路:输入的数为列,该数减半向上取整为行
// 第一行输出该字符 然后没一行的第一列和最后一列输出该字符
//简单题
#include int main(){ int n; char ch; scanf("%d %c",&n, &ch); int col=(n+1)/2; for(int i=0;i<col;i++){ for(int j=0;j<n;j++){ if(i==0||j==0||i==col-1||j==n-1) //唯一一个思考点 printf("%c",ch); else printf(" "); } printf("\n"); } return 0; }