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 = new Random().nextInt(6 - k);
System.out.println(a.remove(d));
}
}
}
2. 代码填空(满分3分)
不同进制的数值间的转换是软件开发中很可能会遇到的常规问题。下面的代码演示了如何把键盘输入的3进制数字转换为十进制。试完善之。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int n = 0;
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if(c<'0' || c > '2') throw new RuntimeException("Format error");
n = Character.getNumericValue(c) * (int)Math.pow(3, i);
}
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<array.length-1)
{
max=findMax(array, index+1);
}
if(max<array[index]) max= array[index];
return max;
}
}
4. 代码填空(满分5分)
电视台开宝箱节目:打进电话的人可以开启一个宝箱。箱子中有一件礼品。礼品是iphone的机率为1/12;是mp3 的机率为1/5;是洗衣粉的机率为1/2;剩余是KFC优惠券。
每次打进电话,宝箱会重置。
以下程序模拟了该抽奖过程。请填写缺失的部分。
public static void main(String[] args)
{
int i = (int) (Math.random() * 60);
if (i < 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”
则返回:5
public static 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<s.length(); i++)
{
if(s.charAt(i)=='0')
{
n_0++;
n_1=0;
}
else
{
n_1++;
n_0=0;
}
if(n_1 > 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; i<x.length(); i++)
{
n = n + getRealValue(x.charAt(i)); // 填空
}
String t = "";
for(;;)
{
if(n==0) break;
t = (n % 3) + t;
n = n / 3; // 填空
}
return t;
}
7. 代码设计(满分5分)
625这个数字很特别,625的平方等于390625,刚好其末3位是625本身。除了625,还有其它的3位数有这个特征吗?
请编写程序,寻找所有这样的3位数:它的平方的末3位是这个数字本身。
输出结果中,从小到大,每个找到的数字占一行。比如那个625就输出为:
625
public class Demo07 {
public static void main(String[] args) {
find();
}
static void find() {
for(int i=100; i<1000; i++) {
if((i*i - i) % 1000 == 0) {
System.out.println(i * i + " /t " + i);
}
}
}
}
8. 代码设计(满分11分)
考虑方程式:a^3 + b^3 = c^3 + d^3
其中:“^”表示乘方。a、b、c、d是互不相同的小于30的正整数。
这个方程有很多解。比如:
a = 1,b=12,c=9,d=10 就是一个解。因为:1的立方加12的立方等于1729,而9的立方加10的立方也等于1729。
当然,a=12,b=1,c=9,d=10 显然也是解。
如果不计abcd交换次序的情况,这算同一个解。
你的任务是:找到所有小于30的不同的正整数解。把a b c d按从小到大排列,用逗号分隔,每个解占用1行。比如,刚才的解输出为:
1,9,10,12
不同解间的顺序可以不考虑。
public class Demo08 {
public static void main(String[] args) {
calc();
}
static void calc() {
int[] intArray = new int[30];
for (int i = 1; i < 30; i++) {
intArray[i] = i * i * i;
}
for (int i = 1; i < 30; i++) {
for (int j = i + 1; j < 30; j++) {
for (int k = j + 1; k < 30; k++) {
for (int l = k + 1; l < 30; l++) {
if(clac2(intArray[i], intArray[j], intArray[k], intArray[l])) {
System.out.println(i + ", " + j + ", " + k + ", " + l);
}
}
}
}
}
}
private static boolean clac2(int a, int b, int c, int d) {
if((a + b) == (c + d) || (a + c) == (b + d) || (a + d) == (b + c)) {
return true;
}
return false;
}
}
9. 代码设计(满分18分)
整数的分划问题。
如,对于正整数n=6,可以分划为:
6
5+1
4+2, 4+1+1
3+3, 3+2+1, 3+1+1+1
2+2+2, 2+2+1+1, 2+1+1+1+1
1+1+1+1+1+1+1
现在的问题是,对于给定的正整数n,编写算法打印所有划分。
用户从键盘输入 n (范围1~10)
程序输出该整数的所有划分。
public class Demo09 {
public Demo09() {
TreeNode root = new TreeNode(3);
buildTree(root, 6, 6);
printTreePath(root);
}
public static void main(String[] args) {
// Scanner in = new Scanner(System.in);
// int n = in.nextInt();
// calc(n);
// System.out.println(calcCount(6, 6));
Demo09 demo = new Demo09();
}
static void calc(int n) {
for (int i = n - 1; i >= 1; i--) {
}
}
private static int calcCount(int n, int max) {
if (n == 1 || max == 1) {
return 1;
} else if (n < max) {
return calcCount(n, n);
} else if (n == max) {
return 1 + calcCount(n, max - 1);
} else {
return calcCount(n, max - 1) + calcCount(n - max, max);
}
}
void printTreePath(TreeNode root) {
if (root.getCount() == 0) {
System.out.println(root.getFullPath());
} else {
for (TreeNode child : root.getNodes()) {
printTreePath(child);
}
}
}
void buildTree(TreeNode root, int n, int max) {
if (n == 1) {
root.addNode(new TreeNode(1));
} else if (max == 1) {
TreeNode lastNode = root;
for (int i = 0; i < n; i++) {
lastNode.addNode(new TreeNode(1));
lastNode = lastNode.getChildNode();
}
} else if (n < max) {
buildTree(root, n, n);
} else if (n == max) {
// 包含max
root.addNode(new TreeNode(n));
// 不包含max
buildTree(root, n, max - 1);
} else { // n > max
// 含max
TreeNode node = new TreeNode(max);
root.addNode(node);
buildTree(node, n - max, max);
// 不含max
buildTree(root, n, max - 1);
}
}
class TreeNode {
private int data; // 结点数据
private List<TreeNode> nodes; // 孩子们
private TreeNode parentNode;
public TreeNode() {
nodes = new ArrayList<TreeNode>();
}
public TreeNode(int data) {
this.data = data;
nodes = new ArrayList<TreeNode>();
}
public String getFullPath() {
if (parentNode == null) {
return "";// String.valueOf(this.getData());
} else {
return ((parentNode.parentNode == null) ? (parentNode.getFullPath()) : (parentNode.getFullPath() + "+")) + this.getData();
}
}
// 获取父结点
public TreeNode getParentNode() {
return parentNode;
}
// 设置父结点
// public void setParentNode(TreeNode root) {
// this.parentNode = root;
// root.addNode(this);
// }
// 获取孩子数
public int getCount() {
return nodes.size();
}
// 获取第一个子结点
public TreeNode getChildNode() {
if (nodes.size() == 0) {
return null;
} else {
return nodes.get(nodes.size() - 1);
}
}
// 获取所有子结点
public List<TreeNode> getNodes() {
return nodes;
}
// 获取数据
public int getData() {
return data;
}
// 设置数据
public void setData(int data) {
this.data = data;
}
// 添加子结点
public void addNode(TreeNode node) {
nodes.add(node);
node.parentNode = this;
}
}
}
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分钟内运行完毕。
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++;
}
}
// 判断两个int型数组是否相等
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); // 对指定的 int 型数组按数字升序进行排序。
Arrays.sort(a2);
return Arrays.equals(a1, a2);
}
// 将BigInteger转化为int数组
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;
}
}