本帖最后由 北方童话 于 2015-8-3 01:19 编辑
所有题目答案在附件中,谢谢! 所有题目答案在附件中,谢谢! 所有题目答案在附件中,谢谢! 第一题: public class Prog2{ public staticvoid main(String[] args){ int m =1; int n = 1000; int count = 0; //统计素数个数 for(int i=m;i<n;i++){ if(isPrime(i)){ count++; System.out.print(i+""); if(count%10==0){ System.out.println(); } } } System.out.println(); System.out.println("在"+m+"和"+n+"之间共有"+count+"个素数"); } //判断素数 private static boolean isPrime(int n){ boolean flag = true; if(n==1) flag = false; else{ for(int i=2;i<=Math.sqrt(n);i++){ if((n%i)==0|| n==1){ flag= false; break; } else flag = true; } } return flag; } } 第二题: public classProg4{ public static void main(String[] args){ intn = 13; decompose(n); } private static void decompose(int n){ System.out.print(n+"="); for(int i=2;i<n+1;i++){ while(n%i==0 && n!=i){ n/=i; System.out.print(i+"*"); } if(n==i){ System.out.println(i); break; } } } } 第三题: importjava.util.Scanner; public classProg7_1{ public static void main(String[] args){ System.out.print("请输入一串字符:"); Scanner scan = new Scanner(System.in); String str = scan.nextLine();//将一行字符转化为字符串 scan.close(); count(str); } //统计输入的字符数 private static void count(String str){ String E1 ="[\u4e00-\u9fa5]";//汉字 String E2 = "[a-zA-Z]"; String E3 = "[0-9]"; String E4 = "\\s";//空格 int countChinese = 0; int countLetter = 0; int countNumber = 0; int countSpace = 0; int countOther = 0; char[] array_Char = str.toCharArray();//将字符串转化为字符数组 String[] array_String = newString[array_Char.length];//汉字只能作为字符串处理 for(int i=0;i<array_Char.length;i++) array_String = String.valueOf(array_Char); //遍历字符串数组中的元素 for(String s:array_String){ if(s.matches(E1)) countChinese++; else if(s.matches(E2)) countLetter++; else if(s.matches(E3)) countNumber++; else if(s.matches(E4)) countSpace++; else countOther++; } System.out.println("输入的汉字个数:"+countChinese); System.out.println("输入的字母个数:"+countLetter); System.out.println("输入的数字个数:"+countNumber); System.out.println("输入的空格个数:"+countSpace); System.out.println("输入的其它字符个数:"+countSpace); } } importjava.util.*; public classProg7_2{ public static void main(String[] args){ System.out.println("请输入一行字符:"); Scanner scan = new Scanner(System.in); String str = scan.nextLine(); scan.close(); count(str); } //统计输入的字符 private static void count(String str){ List<String> list = newArrayList<String>(); char[] array_Char = str.toCharArray(); for(char c:array_Char) list.add(String.valueOf(c));//将字符作为字符串添加到list表中 Collections.sort(list);//排序 for(String s:list){ int begin = list.indexOf(s); int end = list.lastIndexOf(s); //索引结束统计字符数 if(list.get(end)==s) System.out.println("字符‘"+s+"’有"+(end-begin+1)+"个"); } } } 第四题: importjava.util.Scanner; public classProg8{ public static void main(String[] args){ System.out.print("求s=a+aa+aaa+aaaa+...的值,请输入a的值:"); Scanner scan = newScanner(System.in).useDelimiter("\\s*");//以空格作为分隔符 int a =scan.nextInt(); int n = scan.nextInt(); scan.close();//关闭扫描器 System.out.println(expressed(2,5)+add(2,5)); } //求和表达式 private static String expressed(int a,intn){ StringBuffer sb = new StringBuffer(); StringBuffer subSB = new StringBuffer(); for(int i=1;i<n+1;i++){ subSB = subSB.append(a); sb = sb.append(subSB); if(i<n) sb = sb.append("+"); } sb.append("="); return sb.toString(); } //求和 private static long add(int a,int n){ long sum = 0; long subSUM = 0; for(int i=1;i<n+1;i++){ subSUM = subSUM*10+a; sum = sum+subSUM; } return sum; } }
|