必威体育Betway必威体育官网
当前位置:首页 > IT技术

赛码网-计算器的新功能(Java实现)

时间:2019-08-08 17:42:08来源:IT技术作者:seo实验室小编阅读:59次「手机版」
 

赛码网

题目描述
									

当你学一些可视化程序设计语言时,老师经常会让你设计并且编程做出一个计算器,这时也许你会仿照windows系统自带的计算器外观和功能去设计,但是现在老师要你多做出一个有新功能的计算器,实现当输入一个数时,能够将这个数分解成一个或多个素因子乘积的形式,并按素因子的大小排列显示出来。大家对计算器中数的表示应该很清楚的。下面显示出了0 — 9这十个数字的表示形式。每个数字都占据5 * 3大小的字符区域

你能实现这个新功能吗?试试看吧!

输入

输入有多组测试数据,每组包括一个正整数n(1 < n <= 1000000)。

样例输入

10

2

输出

对于每个数,将它分解成若干个素数乘积的形式,并按从小到大的顺序输出,素因子之间用“ * ”的形式连接。

样例输出

-     -

|   |

-  *  -

|       |

-     -

-

|

-

|

-

解题思路:做这个题目做的第一步就是将一个整数分解为素数相乘的形式,以前学过,但是已经忘的差不多,这就算是重新温习一下吧。做这个题目,说实话,思路很简单,但是实现起来比较麻烦。这个题目需要注意的地方还有:素数可能是一个大于10的数,这时候需要进行分解,分解的过程中我用到了栈,先进先出,正好符合题目的输出规则。

这个题目还有注意的地方(有点操蛋),就是数字1的输出,两个"|"是在5x3的最后一列,刚开始我把它放中间了,然后交上去,wa,改了之后,直接AC了,唉~~~

代码

import java.util.*;

public class Main {
    public static void printChar(int k,int num){
        String[] line1 = {" - ","   "," - "," - ","   "," - "," - "," - "," - "," - "," "};
        String[] line2 = {"| |","  |","  |","  |","| |","|  ","|  ","  |","| |","| |"," "};
        String[] line3 = {"   ","   "," - "," - "," - "," - "," - ","   "," - "," - ","*"};
        String[] line4 = {"| |","  |","|  ","  |","  |","  |","| |","  |","| |","  |"," "};
        String[] line5 = {" - ","   "," - "," - ","   "," - "," - ","   "," - "," - "," "};
        switch (k){
            case 0:
                System.out.print(line1[num]);
                break;
            case 1:
                System.out.print(line2[num]);
                break;
            case 2:
                System.out.print(line3[num]);
                break;
            case 3:
                System.out.print(line4[num]);
                break;
            case 4:
                System.out.print(line5[num]);
                break;
            default:
                break;
        }
    }
    public static void main(String[] args) {
        scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextint();
            ArrayList<integer> list = new ArrayList<>();
            int count = 0;
            int tmp = n;
            for(int i=2;i<=tmp;) {
                if (tmp % i == 0) {
                    if(i>10){
                        int num = i;
                        Stack<Integer> s = new Stack<Integer>();
                        while(num>0){
                            s.push(num%10);
                            num/=10;
                        }
                        list.add(s.pop());
                        while(!s.empty()){
                            list.add(s.pop());
                        }
                    }else{
                        list.add(i);
                    }
                    list.add(10);
                    tmp /= i;
                    i = 2;
                    count++;
                } else {
                    i++;
                }
            }
            //System.out.println(count);
            for(int k = 0;k<5;k++){
                for(int i=0;i<list.size()-1;i++){
                    //System.out.println(list.get(i));
                    printChar(k,list.get(i));
                }
                System.out.println();
            }


        }
    }
}

相关阅读

分享到:

栏目导航

推荐阅读

热门阅读