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

题意

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

示例

这里写图片描述

注意点

分解的时候,可能有超过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<Integer> 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;m<nums.size()-1;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<Integer> 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;m<nums.size();m++){
        int 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<Integer> result = new ArrayList<Integer>();
    for(int i=0;i<str.length();i++){
        c = str.charAt(i);
        n = Integer.valueOf(String.valueOf(c));
        result.add(n);
    }
    printPrime(result);
    }

    public static List<Integer> getYinshu(int num){
    List<Integer> result = new ArrayList<Integer>();
    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;i<str.length()-1;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;i<str.length()-1;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;
    }
}

运行结果

这里写图片描述

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页