ThoughtWorks线上编程题解体验

线上编程体验活动地址:https://www.nowcoder.com/activity/2018tw/index

题目我忘记了,大致意思是:

输入任意数字,打印出相对应得字符

例如:
这里写图片描述

不多说了直接上题解吧

import java.util.Scanner;
import java.util.Stack;

public class ThoughtWorks_test {
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        String n=sc.nextLine();
        String  a[]={"._.\n|.|\n|_|",
                "...\n..|\n..|",
                "._.\n._|\n|_.",
                "._.\n._|\n._|",
                "...\n|_|\n..|",
                "._.\n|_.\n._|",
                "._.\n|_.\n|_|",
                "._.\n..|\n..|",
                "._.\n|_|\n|_|",
                "._.\n|_|\n..|"};
        char N[]=n.toCharArray();
         {
            Stack<Long> stack = new Stack<>();
            int count = N.length;
            for (int i=count-1;i>=0;i--){
                stack.push((long) (N[i]-'0'));
            }
            String S[][] = new String[count + 1][];
            for (int i = 0; i < count; i++) {
                S[i] = a[Math.toIntExact(stack.pop())].split("\n");
            }
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < count; j++)
                    if (j == count - 1)
                        System.out.print(S[j][i]);
                    else System.out.print(S[j][i] + " ");
                System.out.println();
            }
        }
    }
}

解析:

应该不算难,代码也相对比较得的简陋无非就是将0-9这十个字符中的每一个字符都用字符数组来表示,这样就有一个长度为10的二维字符数组,然后再利用for和if轻松的将其打印出来

小结

调试花了半个小时,题目虽然简单,但探险的过程还是花了不少的时间

猜你喜欢

转载自blog.csdn.net/Mikeoperfect/article/details/80570613
今日推荐