2011JAVA本科蓝桥杯模拟(1)

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 = ____________________________;

            System.out.println(a.remove(d));

        }

    }

}

 --------------------------------

package java2011Yangti;

import java.util.Random;
import java.util.Vector;

public class M1 {
	/**
	 * author hanhexin
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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('F' - 'A' - 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<s.length(); i++)

{

     char c = s.charAt(i);

     if(c<'0' || c > '2') throw newRuntimeException("Format error");

     n = ______________________;

}

System.out.println(n);


-----------------------------------

package java2011Yangti;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class M2 {
	/**
	 * author hanhexin
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s ="0";
		try {
			s = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		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 = n + (c-'0')*(int)Math.pow(3, s.length() - i -1);
		}
		System.out.println(n);
		
		System.out.println(Integer.toString(n, 3));

	}
}


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=____________________

        }

        if(max<array[index]) max= array[index];

   

        return max;

    }

}

 ----------------------------------------------------

package java2011Yangti;

public class M3 {
	/**
	 * author hanhexin
	 */
	public static void main(String[] args) {
		int array[] = { 0, 34, 67, 90, 21, -9, 98, 1000, -78 };
		System.out.println(findMax(array, 0));
	}

	public static 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() * _____________;

        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优惠券");

        }

    }

 --------------------------------------------------------------------------

package java2011Yangti;

public class M4 {
	/**
	 * author hanhexin
	 */
	public static void main(String[] args) {
		int i = (int) (Math.random() * 60);
		System.out.println(i);
		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

publicstatic 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++;

                ________;

            }

            else

            {

                n_1++;

                _________;

            }

           

            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);

}

-----------------------------------------------------

package java2011Yangti;

import java.util.Scanner;

public class M5 {

	/**
	 * author hanhexin
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println(getMaxContinuity(sc.nextLine()));
	}

	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;
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值