• 【编程题】计算器的新功能


      题目:计算器的新功能

      附上提交通过的Java版代码

    题目描述

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


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

     1 import java.util.ArrayList;
     2 import java.util.List;
     3 import java.util.Scanner;
     4 
     5 public class Computer {
     6 
     7     public static void main(String[] args) {
     8         Scanner input = new Scanner(System.in);
     9         while(input.hasNextInt()){
    10             int n = input.nextInt();
    11             List deposeList = despose(n);
    12 //            System.out.println(deposeList);
    13             printResult(deposeList);
    14         }
    15     }
    16 
    17     private static void printResult(List deposeList) {
    18         int number = deposeList.size();
    19         String[] line1 = {" - ","   "," - "," - ","   "," - "," - "," - "," - "," - "};
    20         String[] line2 = {"| |","  |","  |","  |","| |","|  ","|  ","  |","| |","| |"};
    21         String[] line3 = {"   ","   "," - "," - "," - "," - "," - ","   "," - "," - "};
    22         String[] line4 = {"| |","  |","|  ","  |","  |","  |","| |","  |","| |","  |"};
    23         String[] line5 = {" - ","   "," - "," - ","   "," - "," - ","   "," - "," - "};
    24         StringBuffer printLine1 = new StringBuffer();
    25         StringBuffer printLine2 = new StringBuffer();
    26         StringBuffer printLine3 = new StringBuffer();
    27         StringBuffer printLine4 = new StringBuffer();
    28         StringBuffer printLine5 = new StringBuffer();
    29         for(int i = 0; i < number; i++){
    30             String stringNum = deposeList.get(i).toString();
    31 //            System.out.println(stringNum);
    32             for(int j = 0; j<stringNum.length(); j++){
    33                 printLine1.append(line1[stringNum.charAt(j)-'0']);
    34                 printLine2.append(line2[stringNum.charAt(j)-'0']);
    35                 printLine3.append(line3[stringNum.charAt(j)-'0']);
    36                 printLine4.append(line4[stringNum.charAt(j)-'0']);
    37                 printLine5.append(line5[stringNum.charAt(j)-'0']);
    38             }
    39             printLine1.append(" ");
    40             printLine2.append(" ");
    41             printLine3.append("*");
    42             printLine4.append(" ");
    43             printLine5.append(" ");
    44         }
    45         int printLen = printLine1.length();
    46         printLine1.deleteCharAt(printLen-1);
    47         printLine2.deleteCharAt(printLen-1);
    48         printLine3.deleteCharAt(printLen-1);
    49         printLine4.deleteCharAt(printLen-1);
    50         printLine5.deleteCharAt(printLen-1);
    51         System.out.println(printLine1);
    52         System.out.println(printLine2);
    53         System.out.println(printLine3);
    54         System.out.println(printLine4);
    55         System.out.println(printLine5);
    56     }
    57 
    58     private static List despose(int n) {
    59         List deposeList = new ArrayList();
    60         int primer = 2;
    61         while(primer <= n){
    62             if(primer == n){
    63                 deposeList.add(primer);
    64                 break;
    65             }
    66             else if(n % primer == 0){
    67                 deposeList.add(primer);
    68                 n = n/primer;
    69             }
    70             else{
    71                 primer++;
    72             }    
    73         }
    74         return deposeList;
    75         
    76     }
    77 
    78 }

      有更好的思路,欢迎指教~

  • 相关阅读:
    Linux 性能监测:CPU
    Linux 性能监测:介绍
    数据库设计
    数据库设计
    如何将Virtualbox和VMware虚拟机相互转换
    如何将Virtualbox和VMware虚拟机相互转换
    stat()获得文件信息
    stat()获得文件信息
    网游创业为什么会失败?
    网游创业为什么会失败?
  • 原文地址:https://www.cnblogs.com/focusonepoint/p/5721533.html
Copyright © 2020-2023  润新知