java答案

来源:互联网 发布:《java开发手册》 pdf 编辑:程序博客网 时间:2024/04/29 11:37

题目是从官网(http://www.miit-nstc.org/)下的,下载地址为http://www.miit-nstc.org/Article/ShowArticle.asp?ArticleID=139答案不是唯一的,也不保证全对哦~仅供参考,1-9题是自己写的,..第10题实在是不懂,于是从CSDN上看到帖子然后复制过来的..以下内容就是题目和答案=========================================================================================================== 2011 模拟 java 本科注意:本套模拟题主要模拟命题形式与考核范围。真实竞赛题的数量、难度可能与此套模拟题有差异。说明:本试卷包含两种题型:“代码填空”与“程序设计”。填空题要求参赛选手在弄清给定代码工作原理的基础上填写缺失的部分,使得程序逻辑正确、完整。所填写的代码不多于一条语句(即不能出现分号)。编程题要求选手设计的程序对于给定的输入能给出正确的输出结果。注意:在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。选手的程序必须是通用的,不能只对试卷中给定的数据有效。 1. 代码填空(满分2分)在A B C D E F 六人中随机抽取3人中奖,要求中奖人不能重复。请完善以下代码:public class MyTest{ public static void main(String[] args) { Vector a = new Vector(); for(char i='A'; i<='F'; i++) a.add("" + i); for(int k=0; k<3; k++) { int d=____(int)(Math.random()*(6-k));_______________; System.out.println(a.remove(d)); } }} 2. 代码填空(满分3分)不同进制的数值间的转换是软件开发中很可能会遇到的常规问题。下面的代码演示了如何把键盘输入的3进制数字转换为十进制。试完善之。 BufferedReader br = new BufferedReader(newInputStreamReader(System.in)); String s = br.readLine(); int n = 0; for(int i=0; i<'0' || c > '2') throw newRuntimeException("Format error"); n = ____ n+(c-48)*(int)Math.pow(3,s.length()-i-1)__________; } System.out.println(n);3. 代码填空(满分4分)有如下程序,完成的功能为:找出数组中的最大元素。请填写程序的中空白,使程序运行正确。 public class test { public static void main(String[] args) { int array[]={0,34,67,90,21,-9,98,1000,-78}; System.out.println(new test().findMax (array, 0)); } public int findMax(int array[],int index) { if(array==null || array.length==0) { return 0; } int max=array[0]; if(index< 5) { System.out.println("恭喜中了:iphone手机"); }else if (i < 17) { System.out.println("恭喜中了:mp3"); } else if (i < 47) { System.out.println("恭喜中了:洗衣粉"); } else { System.out.println("恭喜中了:KFC优惠券"); } } 5. 代码填空(满分6分)下列代码求出一个二进制串中连续的1或连续的0出现的最大次数。请填缺失代码。例如:s = “101100111100011”则返回:4又例如:s=”0111100000”则返回:5publicstatic int getMaxContinuity(String s){ int max_1 = 0; int max_0 = 0; int n_1 = 0; // 当前1连续的次数 int n_0 = 0; // 当前0连续的次数 for(int i=0; i max_1) max_1 = n_1; if(n_0 > max_0) max_0 = n_0; } return max_1>max_0? max_1 : max_0);} 6. 代码填空(满分9分) 下列代码把16进制表示的串转换为3进制表示的串。试完善之。例如:x=“5”则返回:“12”又例如:x=”F”则返回:“120” private static int getRealValue(char x) { if(x>='0' && x<='9') return x-'0'; if(x>='a' && x<='f') return x-'a'+10; if(x>='A' && x<='F') return x-'A'+10; return 0; } public static String jin_zhi_16_3(String x) { int n = 0; // 累加真值 for(int i=0; i0;i--){ last=n-i; if(last==0){System.out.println(i);continue;} find(i+"",last,i); System.out.println(""); } } public static void find(String Str,int last,int max) { if(last<0) return; if(last==0) System.out.print(Str+";"); for(int i=max;i>0;i--) { find(Str+"+"+i,last-i,i); } } 10. 代码设计(满分20分)一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。例如:当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,这样的数字也被称为水仙花数(其中,“^”表示乘方,5^3表示5的3次方,也就是立方)。当N=4时,1634满足条件,因为 1^4 + 6^4 + 3^4 + 4^4 = 1634。当N=5时,92727满足条件。实际上,对N的每个取值,可能有多个数字满足条件。 程序的任务是:求N=21时,所有满足条件的花朵数。注意:这个整数有21位,它的各个位数字的21次方之和正好等于这个数本身。如果满足条件的数字不只有一个,请从小到大输出所有符合条件的数字,每个数字占一行。因为这个数字很大,请注意解法时间上的可行性。要求程序在3分钟内运行完毕。 import java.math.BigInteger;import java.util.Arrays; public class Narcissistic { private static BigInteger[] table = new BigInteger[10]; public static void main(String[] args) { long time = System.nanoTime(); find(21); time = System.nanoTime() - time; System.out.println(time / 1000000000.0 + "s"); } public static void find(int n) { for (int i = 0; i < 10; i++) table[i] = BigInteger.valueOf(i).pow(n); int[] nums = new int[n]; int index = 0; int num = 0; BigInteger sum = BigInteger.ZERO; BigInteger MIN = BigInteger.TEN.pow(n - 1); BigInteger MAX = BigInteger.TEN.pow(n).subtract(BigInteger.ONE); while (true) { if (index < nums.length && num < 10) { BigInteger temp = sum.add(table[num]); if (temp.compareTo(MAX) < 0) { nums[index] = num; index++; sum = temp; continue; } } else if (index >= nums.length && sum.compareTo(MIN) > 0) { int[] temp = getArray(sum); if (check(nums, true, temp, false)) System.out.println(sum); } else if (index <= 0) { break; } index--; num = nums[index]; sum = sum.subtract(table[num]); num++; } } public static boolean check(int[] a1, boolean copy1, int[] a2, boolean copy2) { if (a1.length != a2.length) return false; if (copy1) a1 = a1.clone(); if (copy2) a2 = a2.clone(); Arrays.sort(a1); Arrays.sort(a2); return Arrays.equals(a1, a2); } public static int[] getArray(BigInteger big) { String s = String.valueOf(big); int length = s.length(); int[] res = new int[length]; for (int i = 0; i < length; i++) res[i] = s.charAt(i) - '0'; return res; }}

原创粉丝点击