1 条题解

  • 0
    @ 2023-12-24 23:39:55

    同P1002电子钟

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String[][] strArr = new String[][]
                    {
                            {"   "," _ ",},
                            {"  |"," _|","|_|","|_ ","| |"},
                            {"  |","|_ "," _|","|_|"}
                    };//在这个二维数组中存入所有可能出现的字符串
            while (sc.hasNextInt())
            {
                int a = sc.nextInt();
                int b = sc.nextInt();
                int c = sc.nextInt();
                int d = sc.nextInt();
                int[] arr = new int[]{a,b,c,d};//将输入存入数组,便于遍历
    
                String temp = "";
                //用for循环一行一行输出,i用来确定输出到第几行了,也确定应该调用字符串数组strArr的哪一行字符串(一共三行)
                for (int i = 0; i < 3; i++) {
                    for(int num : arr)//遍历,筛选,然后把对应的字符串接到temp上(也可以直接不换行输出)
                    {
                        if(i == 0)
                        {
                            if(num == 1 || num == 4)
                            {
                                temp += strArr[i][0];
                            }
                            else//只是我偷懒,最好还是用else-if吧(下同)
                            {
                                temp += strArr[i][1];
                            }
                        }
                        if(i == 1)
                        {
                            if(num == 1 || num == 7)
                            {
                                temp += strArr[i][0];
                            }
                            else if (num == 2 || num == 3)
                            {
                                temp += strArr[i][1];
                            }
                            else if (num == 0)
                            {
                                temp += strArr[i][4];
                            }
                            else if (num == 5 || num == 6)
                            {
                                temp += strArr[i][3];
                            }else
                            {
                                temp += strArr[i][2];
                            }
                        }
                        if(i == 2)
                        {
                            if(num == 1 || num == 7 || num == 4)
                            {
                                temp += strArr[i][0];
                            }
                            else if (num == 2)
                            {
                                temp += strArr[i][1];
                            }
                            else if (num == 0 || num == 6 || num == 8)
                            {
                                temp += strArr[i][3];
                            }
                            else if (num == 5 || num == 3 || num == 9)
                            {
                                temp += strArr[i][2];
                            }
                        }
                    }
                    System.out.println(temp);//输出temp
                    temp = "";//重置temp,准备下一行录入
                }
            }
        }
    }
    
    • 1

    信息

    ID
    12
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    3
    已通过
    3
    上传者