首页 > 试题广场 >

K形图案

[编程题]K形图案
  • 热度指数:28812 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的K形图案。

输入描述:

多组输入,一个整数(2~20)。



输出描述:
针对每行输入,输出用“*”组成的K形,每个“*”后面有一个空格。
示例1

输入

2

输出

* * * 
* * 
* 
* * 
* * * 
示例2

输入

3

输出

* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
示例3

输入

4

输出

* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
            int n = in.nextInt();
            // 1 到 n + 1
            for(int i = 1; i <= n + 1; i++) {
               for(int j = n + 1; j >= i; j--) {
                  System.out.print("* ");
               }
               //换行
               System.out.println();
            }
            // n + 2 到 2 * n + 1
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= i + 1; j++) {
                    System.out.print("* ");
                }
                //换行
                System.out.println();
            }
        }
    }
}
发表于 2023-10-27 16:52:28 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            for(int i=1;i<=n+1;i++){
                for(int j=n+1;j>=i;j--){
                    System.out.print("* ");
                }
                System.out.println();
            }
            for(int i=1;i<=n;i++){
                for(int j=0;j<=i;j++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-11-02 21:23:43 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int x=sc.nextInt();
            for(int i=x+1;i>1;i--){
                for(int j=1;j<=i;j++){
                    System.out.print("* ");
                }
                System.out.println();
            }
            System.out.println("*");
            for(int i=2;i<=x+1;i++){
                for(int j=1;j<=i;j++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-08-18 22:02:40 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            //上三角
            for (int c = 1; c <= n; c++) {
                for (int b = n - c + 2; b > 0; b--) {
                    System.out.print("* ");
                }
                System.out.println();
            }
            //中间线
            System.out.println("* ");
            //下三角
            for (int c = 1; c <= n; c++) {
                for (int b = 0 ; b < c + 1 ; b++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-07-06 15:29:21 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int n = scanner.nextInt();
            for(int i = 1 ; i <= n ;i++){
                for(int j = i;j <= n + 1;j++){
                    System.out.print("* ");
                }
                System.out.println("");
            }
            System.out.println("*");
            for(int i = 1;i <= n;i++){
                for(int j = 1;j <= i + 1;j++){
                    System.out.print("* ");
                }
                System.out.println("");
            }
        }
    }
}

发表于 2022-06-29 09:23:58 回复(0)
import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        while(input.hasNextInt()){
         int n=input.nextInt();
            for(int i=0;i<n+1;i++){
                for(int j=n+1;j>i;j--){
                    System.out.print("* ");
                }
                 System.out.println();
            }
            for(int l=n;l>0;l--){
             for(int k=0;k<=n+1-l;k++){
            System.out.print("* ");
        }
                 System.out.println();
        }
        }
        input.close();
    }
}

发表于 2022-05-16 09:25:56 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()) {
            int a = sc.nextInt();
            for(int i=1;i<a+1;i++) {
                for(int j=i;j<=a+1;j++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
            
            for(int i=1;i<=a+1;i++) {
                for(int j=1;j<=i;j++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}
发表于 2022-04-20 17:29:04 回复(0)
import java.util.Scanner;

/**
 * @Title: K形图案
 * @Remark: KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的K形图案。
 * 输入描述:
 * 多组输入,一个整数(2~20)。
 *
 * 输出描述:
 * 针对每行输入,输出用“*”组成的K形,每个“*”后面有一个空格。
 * @Author: ijunfu
 * @Version: 1.0.0
 * @Date: 2022-03-19
 */
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while(in.hasNextLine()) {
            Integer row = Integer.valueOf(in.nextLine());

            for(int i=0; i<=row; i++) {

                for(int j=0; j<=row-i; j++) {
                    System.out.print("* ");
                }

                System.out.println();
            }

            for(int i=0; i<row; i++) {

                for(int j=0; j<=i+1; j++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-03-19 20:53:48 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= n-i; j++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
            for (int i = 0; i < n ; i++) {
                for (int j = 0; j <= i+1 ; j++) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2021-12-16 02:17:07 回复(0)
import java.util.Scanner;

public clas***ain {
    public static void main(String[] arg***r />         Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int a = scanner.nextInt();
            for(int i=0;i<a+1;i++){
                for(int j=0;j<a+1-i;j++){
                    System.out.printf("* ");
                }
                System.out.println();
            }
            for(int i=2;i<=a+1;i++){
                for(int j=0;j<i;j++){
                    System.out.printf("* ");
                }
                System.out.println();
            }
        }
    }
}
发表于 2021-11-10 00:14:58 回复(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 = 1;i <= n; i++){
                for(int j = 1; j <= n+2-i;j++){
                    System.out.print("* ");
                }
                System.out.println();
            }
            System.out.println("*");
            for(int i = 1; i <=n ;i++){
                for(int j = 1; j <= i+1; j++){
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2021-09-30 13:31:36 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
           Integer number = sc.nextInt();
           for(int n = number; n >= 0; n--){
               for(int i = n; i >= 0; i--){
                   System.out.print("* ");
               }
               System.out.println("");
           }
           for(int n = 1;n<=number;n++){
               for(int j = n+1;j>0;j--){
                   System.out.print("* ");
               }
               System.out.println("");
           }
        }
    }
}

发表于 2021-08-18 16:27:40 回复(0)
import java.util.*;
public class Main {
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count;
        while(scanner.hasNext()) {
            count = scanner.nextInt();
            for(int i=0;i<=count;i++) {
                for(int j=0;j<=count;j++) {
                    if(j<=count-i)
                        System.out.printf("* ");
                    else
                        System.out.printf(" ");
                }
                System.out.println();
            }
            for(int i=0;i<count;i++) {
                for(int j=0;j<=count;j++) {
                    if(j<=i+1)
                        System.out.printf("* ");
                    else
                        System.out.printf(" ");
                }
                System.out.println();
            }
        }
    }
}

好家伙,浪费这么长时间,就是因为每行最后还有空格,艹,把图案复制到编辑器里就能发现了
* * * * 
* * *  
* *   
*    
* *   
* * *  
* * * * 

编辑于 2021-03-17 20:44:53 回复(0)
题目描述没说清楚!!!
这题的结果输出最后的*后的空格也是有规律的,不一定是一个...
解题方法借用上题大佬的方法:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int m = n+1;
            List<String> list = new ArrayList<>();
            for (int i = 1; i <= m ; i++) {
                String s = String.join("",Collections.nCopies(m+1-i, "* "));
                //最后* 后面的空格
                s += String.join("",Collections.nCopies(i-1," "));
                list.add(s);
            }
            Boolean flag = true;
            for (int i = 0; i >= 0; ) {
                if(i == list.size()-1){
                    flag = false;
                }
                System.out.println(list.get(i));
                i = flag ? ++i : --i;
            }
        }
    }
}


编辑于 2020-12-24 17:39:00 回复(0)
import sys
while True:
    try:
        num=int(sys.stdin.readline().strip())+1
        for i in range(num*2-1):
            if i>num&nbs***bsp;i==num:
                print("* "*(i-num+2)+" "*(2*num-i-2))
            else:
                #每一行宽度为N+1,不够的用空格填充
                print("* "*(num-i)+" "*2*i)
    except:
        break


import java.util.Collections;
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        while(input.hasNext())
        {
            int n=input.nextInt();
            for (int i = 0; i <= 2 * n; i++)
            {
                int filln = Math.abs(n - i) + 1;
                System.out.printf("%s\n", String.join("", Collections.nCopies(filln, "* ")));
            }
        }
    }
}


编辑于 2021-06-21 20:42:52 回复(1)

import java.util.Scanner;

public class Test007{

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        int m=sc.nextInt()+1;

        int j=m;

        sc.close();

        for (;j>0;j--){

        for(int i=0;i<j;i++){

        System.out.print("*"+" ");

}       System.out.println();

        

}

        for(int k=1;k<m;k++){

        for(int i=0;i<=k;i++){

        System.out.print("*"+" ");

}       System.out.println();

        

        

}

    }

}

发表于 2020-08-29 19:18:11 回复(0)

问题信息

上传者:牛客309119号
难度:
17条回答 2813浏览

热门推荐

通过挑战的用户

查看代码