360公司2016笔试题
题目现在没有现成的,未来晚上有了再补充,大概题目是:一个数输出他的素数乘积和如:
具体代码如下:知道题目的同学帮忙给个链接。
package com.test; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class T360_2 { static List<Integer> list = new ArrayList<Integer>(); private final static int TOTAL = 1000000; static{ list = prime(TOTAL); } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int num = 0; List<Integer> list_prime; while(sc.hasNextInt()){ num = sc.nextInt();//读取素数 int temp = num; int line = 0;//每次输出的行号 list_prime = list_prime(num); while(line<5){//以此控制输出行号 for(int i=0; i<list_prime.size(); i++){ temp = list_prime.get(i); print(temp,line); if(line == 2 && i<list_prime.size()-1) System.out.print("*"); else if(line != 2 && i<list_prime.size()-1) System.out.print(" "); } line++; System.out.println(); } } sc.close(); } //获得每个输出参数的素数列表集 public static List<Integer> list_prime(int num){ List<Integer> list_prime = new ArrayList<Integer>(); for(Integer temp : list){ while(num%temp == 0){ list_prime.add(temp); num = num/temp; } if(num <= 1){ break; } } return list_prime; } //获得前1000000中素数列表集 public static List<Integer> prime(int n){ List<Integer> list = new ArrayList<Integer>(); list.add(2);//默认添加素数2 list.add(3);//默认添加素数3 list.add(5);//默认添加素数5 int gap = 2;//每次增长的步长。4、2、4、2.。。。。 boolean f = true; for(int i=7; i<n; i+=gap){ gap = 6-gap;//步长取反 f = true;//是否是素数设置标志位。 for(int j=0; list.get(j)*list.get(j)<=i; j++){ if(i%list.get(j) == 0){//因任何一个数都可以表示成比他小的素数的乘积。所以只要判断是否有满足的素数就可以得到该数是否为素数 f = false; break; } } if(f) list.add(i);//添加素数 } return list; } /** * * @param num 输出数字 * @param line 输出行号 * 打印整个输出0-9 */ public static void print(int num, int line){ String s = num+""; for(int i=0; i<s.length(); i++){ num = Integer.parseInt(s.charAt(i)+""); switch (num) { case 0: print0(line); break; case 1: print1(line); break; case 2: print2(line); break; case 3: print3(line); break; case 4: print4(line); break; case 5: print5(line); break; case 6: print6(line); break; case 7: print7(line); break; case 8: print8(line); break; case 9: print9(line); break; default: break; } } } public static void print0(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print("| |"); break; case 2: System.out.print(" "); break; case 3: System.out.print("| |"); break; case 4: System.out.print(" - "); break; default: break; } } public static void print1(int line){ switch (line) { case 0: System.out.print(" "); break; case 1: System.out.print(" |"); break; case 2: System.out.print(" "); break; case 3: System.out.print(" |"); break; case 4: System.out.print(" "); break; default: break; } } public static void print2(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print(" |"); break; case 2: System.out.print(" - "); break; case 3: System.out.print("| "); break; case 4: System.out.print(" - "); break; default: break; } } public static void print3(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print(" |"); break; case 2: System.out.print(" - "); break; case 3: System.out.print(" |"); break; case 4: System.out.print(" - "); break; default: break; } } public static void print4(int line){ switch (line) { case 0: System.out.print(" "); break; case 1: System.out.print("| |"); break; case 2: System.out.print(" - "); break; case 3: System.out.print(" |"); break; case 4: System.out.print(" "); break; default: break; } } public static void print5(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print("| "); break; case 2: System.out.print(" - "); break; case 3: System.out.print(" |"); break; case 4: System.out.print(" - "); break; default: break; } } public static void print6(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print("| "); break; case 2: System.out.print(" - "); break; case 3: System.out.print("| |"); break; case 4: System.out.print(" - "); break; default: break; } } public static void print7(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print(" |"); break; case 2: System.out.print(" "); break; case 3: System.out.print(" |"); break; case 4: System.out.print(" "); break; default: break; } } public static void print8(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print("| |"); break; case 2: System.out.print(" - "); break; case 3: System.out.print("| |"); break; case 4: System.out.print(" - "); break; default: break; } } public static void print9(int line){ switch (line) { case 0: System.out.print(" - "); break; case 1: System.out.print("| |"); break; case 2: System.out.print(" - "); break; case 3: System.out.print(" |"); break; case 4: System.out.print(" - "); break; default: break; } } }