多组输入,一个整数(3~20),表示输出的行数,也表示组成正方形边的“*”的数量。
针对每行输入,输出用“*”组成的“空心”正方形,每个“*”后面有一个空格。
4
* * * * * * * * * * * *
5
* * * * * * * * * * * * * * * *
#include <iostream> using namespace std; int main() { int n; while (scanf("%d" ,&n) != EOF) { for (int i = 1; i <= n; i ++ ) printf("* "); puts(""); for (int i = 1; i <= n - 2; i ++ ) { printf("* "); for (int j = 1; j <= n - 2; j ++ ) printf(" "); printf("* \n"); } for (int i = 1; i <= n; i ++ ) printf("* "); puts(""); } }
import java.util.Collections; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); if (n==1){ System.out.println("* "); continue; } //以下代码为str 为重复n次"* "的字符串。 String str = Collections.nCopies(n,"* ").stream().collect(Collectors.joining()); String temp = Collections.nCopies((n-2)*2," ").stream().collect(Collectors.joining()); temp = "* "+ temp + "* "; System.out.println(str); for (int i = 0; i < n - 2; i++) { System.out.println(temp); } System.out.println(str); } } }
#include<stdio.h> int main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==0||j==0||i==n-1||j==n-1) printf("* "); else printf(" "); } printf("\n"); } } }
#include<stdio.h> int main() { int n; while( scanf("%d",&n) != EOF ) { for( int i = 0 ; i < n ; i++) { if( i == 0 || i == n - 1 ) { for( int j = 0 ; j < n ; j++ ) { printf("* "); } printf("\n"); } else { for( int m = 0 ; m < 2 * n ; m++ ) { if( m == 0 || m == 2 * n - 2 ) printf("*"); else printf(" "); } printf("\n"); } } } return 0; }
int main(){ int n; while(scanf("%d", &n) != -1){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(i > 0 && i < n - 1 && j > 0 && j < n - 1){ printf(" "); } else{ printf("* "); } } printf("\n"); } } return 0; }
#include <stdio.h> int main() { int n = 0; int i = 0; int space = 0; while(scanf("%d", &n) != EOF) { //第一行 for(i = 0; i < n; i++) { printf("* "); } printf("\n"); //中间 for(i = 0; i < n-2; i ++) //行数 { printf("*"); for(space = 0; space < 2*n-3; space ++) { printf(" "); } printf("*\n"); } //下边 for(i = 0; i < n; i++) { printf("* "); } //最下边这个换行也要写的 呜呜呜··· printf("\n"); } return 0; }
//BC69 空心正方形图案 #include<stdio.h> int main() { int i, j, n; while (scanf("%d", &n) != EOF) { //首行:输出n次 (* ) for (i = 0; i < n; i++) { printf("* "); } printf("\n"); //中间行:输出n-2行 //每行内容:0(* ) n(* ) else( ) for (i = 0; i < (n - 2); i++) { for (j = 0; j < n; j++) { if (j == 0) printf("* "); else if (j == n - 1) printf("* "); else printf(" "); } printf("\n"); } //尾行:同首行 for (i = 0; i < n; i++) { printf("* "); } printf("\n"); } return 0; }
//i==0||i=n-1 //或j==0||j==n-1 #include <stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { for(int i=0;i<n;i++) { for (int j=0;j<n;j++) { if(i==0||i==n-1) { printf("* "); } else if (j==0||j==n-1) { printf("* "); } else { printf(" "); } } printf("\n"); } } return 0; }
#include <stdio.h> int main(){ int n = 0; while(~ scanf("%d", &n)){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(i >= 1 && i <= n - 2 && j >= 1 && j <= n - 2) printf(" "); else printf("* "); } printf("\n"); } } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i==0||i==(n-1)||j==0||j==(n-1)){ System.out.print("* "); }else{ System.out.print(" "); } } System.out.println(" "); } } } }