360公司2016校园招聘笔试题大题一

题意

大概是说,输入多行,每行一个数,然后分解成素数相乘的形式,并以7段数码管的显示方式打印。

示例

360公司2016校园招聘笔试题大题一_第1张图片

注意点

分解的时候,可能有超过9的素数因子,比如13,需要转换成1和3进行打印

代码

import java.io.*;
import java.util.*;

public class Main {

    static char[][] numbers = {
    {' ', ' ','-',' ', ' ',' ',' ', ' ','-',' ', ' ','-',' ', ' ',' ',' ', ' ','-',' ', ' ','-',' ', ' ','-',' ', ' ','-',' ', ' ','-',' '},
    {' ', '|',' ','|', ' ','|',' ', ' ',' ','|', ' ',' ','|', '|',' ','|', '|',' ',' ', '|',' ',' ', ' ',' ','|', '|',' ','|', '|',' ','|'},
    {'*', ' ',' ',' ', ' ',' ',' ', ' ','-',' ', ' ','-',' ', ' ','-',' ', ' ','-',' ', ' ','-',' ', ' ',' ',' ', ' ','-',' ', ' ','-',' '},
    {' ', '|',' ','|', ' ','|',' ', '|',' ',' ', ' ',' ','|', ' ',' ','|', ' ',' ','|', '|',' ','|', ' ',' ','|', '|',' ','|', ' ',' ','|'},
    {' ', ' ','-',' ', ' ',' ',' ', ' ','-',' ', ' ','-',' ', ' ',' ',' ', ' ','-',' ', ' ','-',' ', ' ',' ',' ', ' ','-',' ', ' ','-',' '}};

    public static void main(String args[]) {
    Scanner cin = new Scanner(System.in);
    while(cin.hasNextInt()){
        int k = cin.nextInt();
        printNum(getYinshu(k));
    }
    }

    public static void printNum(List nums){    
    int k = nums.size() - 1;
    if(k==0){
        printPrime(nums.get(0));
        return;
    }
    for (int i = 0; i < 5; i++) {
        for(int m = 0;m1;m++){
        int orin = nums.get(m);
        int n = Math.abs(orin);
        for (int j = 3 * n + 1; j < 3 * (n + 1) + 1; j++) {
            System.out.print(numbers[i][j]);
        }
        if(orin>0){//超过9的质数
            if(i==2){
            System.out.print('*');
            }
            else{
            System.out.print(' ');
            }
            }
        }
        int n = nums.get(k);
        for (int j = 3 * n + 1; j < 3 * (n + 1) + 1; j++) {
        System.out.print(numbers[i][j]);
        }
        System.out.println();
    }
    }

    public static void printPrime(List nums){  
    int k = nums.size() - 1;
    if(k==0){
        printPrime(nums.get(0));
        return;
    }
    for (int i = 0; i < 5; i++) {
        for(int m = 0;mint n = nums.get(m);
        for (int j = 3 * n + 1; j < 3 * (n + 1) + 1; j++) {
            System.out.print(numbers[i][j]);
        }
        }
        System.out.println();
    }
    }

    public static void printPrime(int num){
    String str = String.valueOf(num);
    char c;
    int n;
    List result = new ArrayList();
    for(int i=0;ipublic static List getYinshu(int num){
    List result = new ArrayList();
    int prime = 2;
        while (prime <= num) {  
            if (prime == num) {
            //result.add(num);
            if(num>9){
              //超过9,进行分割
                String str = String.valueOf(num);
                char c;
                int n;
                for(int i=0;i1;i++){
                    c = str.charAt(i);
                    n = Integer.valueOf(String.valueOf(c));
                    result.add(-1*n);
                }
                c = str.charAt(str.length()-1);
                    n = Integer.valueOf(String.valueOf(c));
                    result.add(n);
            }
            else{
                result.add(num);
            }
                break;
            } else if (num % prime == 0) {
            if(prime>9){
              //超过9,进行分割
                String str = String.valueOf(prime);
                char c;
                int n;
                for(int i=0;i1;i++){
                    c = str.charAt(i);
                    n = Integer.valueOf(String.valueOf(c));
                    result.add(-1*n);
                }
                c = str.charAt(str.length()-1);
                    n = Integer.valueOf(String.valueOf(c));
                    result.add(n);
            }
            else{
                result.add(prime);
            }
                num = num / prime;                    
            } else {
                prime++;  
            }  
        }  
        return result;
    }
}

运行结果

360公司2016校园招聘笔试题大题一_第2张图片

你可能感兴趣的:(Java,Algorithm)