1.给个一个正整数n<1000000,以素数相乘的形式输出n的值,如20=2*2*5;10=2*5;2=2,而输出的素数都是按计算器上面的数字来显示的,即每个数字都是以如下形式来输出的
首先要解决这一题要分大概6个步骤:
1.首先求出前100个素数
2.将n分解为素数相乘的形式
3.将分解后的多项式转换为字符串
5.将0~9,*所对应的符号用数组存储起来
6.分5行输出字符串中每个字符对应的数字符号
代码如下:
package com.mnmlist.test2;/** 先求前一百个素数* 将整数n分解为从小到大素数相乘的形式* 将乘数多项式转换成字符串* 按行从1行到第五行输入每个字符(数字和乘号)对应的符号*/import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class Main{public static boolean isPrime(int n){int k=(int)Math.sqrt(n)+1;if((n&1)==0)return false;for(int i=3;i<=k;i++){if(n%i==0)return false;}return true;}public static int[]getPrimeArr(int primeArr[]){int num=3;int index=1;while(num>0){if(isPrime(num)==true){primeArr[index++]=num;if(index==primeArr.length)return primeArr;}num+=2;}return primeArr;}public static void specialCase(String[]chArr,Map<Character, String[]>map){for(int i=0;i<5;i++){System.out.println(map.get('1')[i]);}}public static void main(String[] args){Scanner scanner=new Scanner(System.in);String[]zero={" - ","| |"," ","| |"," - "};String[]one={" "," |"," "," |"," "};String[]two={" - "," |"," - ","| "," - "};String[]three={" - "," |"," - "," |"," - "};String[]four={" ","| |"," - "," |"," "};String[]five={" - ","| "," - "," |"," - "};String[]six={" - ","| "," - ","| |"," - "};String[]seven={" - "," |"," "," |"," "};String[]eight={" - ","| |"," - ","| |"," - "};String[]nine={" - ","| |"," - "," |"," - "};String[]flag={" "," ","*"," "," "};//primeArr=getPrimeArr(primeArr);int primeArr[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541};//将数字和对应的符号对应起来Map<Character, String[]>map=new HashMap<Character,String[]>();map.put('0', zero);map.put('1', one);map.put('2', two);map.put('3', three);map.put('4', four);map.put('5', five);map.put('6', six);map.put('7', seven);map.put('8', eight);map.put('9', nine);map.put('*', flag);int num=0,tempNum=0;while(scanner.hasNext()){//将整数n分解为从小到大素数相乘的形式StringBuilder sBuilder=new StringBuilder();num=scanner.nextInt();if(num==1){specialCase(one, map);continue;}for(int i=0;i<primeArr.length;i++){while(num!=0&&num%primeArr[i]==0){sBuilder.append(""+primeArr[i]+"*");num/=primeArr[i];}if(num==1)break;}if(num!=1)sBuilder.append(""+num+'*');String str=sBuilder.substring(0, sBuilder.length()-1);int strLen=str.length();//按行输出符号for(int i=0;i<5;i++){StringBuilder lineBuilder=new StringBuilder();for(int j=0;j<strLen;j++){lineBuilder.append(map.get(str.charAt(j))[i]);}System.out.println(lineBuilder.toString());}System.out.println();}}}
2.另外一题就很简单了,大概是研究生入学考试,有四门成绩,要求政治英语不低于60分,数学专业课不低于90分,总分不低于310,否则则考试失败,总分310~349属于自费,总分>=350属于公费,对应分别输出:Fail Zifei Gongfei
很简单,大概判断下条件就可以了
import java.util.Scanner;public class Main{public static String getResult(int arr[]){int total=0;for(int i=0;i<4;i++)total+=arr[i];if(arr[0]<60||arr[1]<60||arr[2]<90||arr[3]<90||total<310){return "Fail";}if(total>=310&&total<350)return "Zifei";return "Gongfei";}public static void main(String[] args){Scanner scanner=new Scanner(System.in);int arr[]=new int[4];int count=scanner.nextInt();for(int i=0;i<count;i++){for(int j=0;j<4;j++){arr[j]=scanner.nextInt();}System.out.println(getResult(arr));}}}