第十一届蓝桥杯校内模拟赛java组试题

第十一届蓝桥杯校内模拟赛java组试题

(题目是从别处找来的,也许会不对应,请以实际题目要求为准)

  1. 问题描述
      两个二进制数11110011101和1111101001的和是多少?请用二进制表示。
    答案提交
      这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个二进制数,在提交答案时只填写这个二进制数,填写多余的内容将无法得分。

最后结果应为:101110000110

/**
 *	binary二进制的;sum 求和;remainder余数
 *输入两个二进制数,求和
 */
public class BinarySum {
    //十进制转二进制
    public static String decimalismTransformBinary(int decnum){
        String binanum = "";
        while(decnum/2!=0){
            int remainder = decnum%2;//remainder余数
            binanum+=remainder;
            decnum = decnum/2;
        }
        binanum+=1;//此处加1是因为当最后一步decnum/2==0跳出循环后,余数应该为1
        String result = "";
        for(int i = binanum.length()-1;i>=0;i--){
            result+=binanum.charAt(i);
        }//倒置binanum的内容
        return result;
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str1 = input.nextLine();
        int num1 = Integer.parseInt(str1,2);//将字符串str转化为十进制整型,2代表str中的数字是二进制数字
        String str2 = input.nextLine();
        int num2 = Integer.parseInt(str2,2);
        int sum = num1 + num2;
//		接下来将十进制在转为二进制
//      java中提供了类似方法Integer.toBinaryString
        String s = decimalismTransformBinary(sum);
        String res = Integer.toBinaryString(sum);
        if(s.equals(res))
            System.out.println(res);
    }
}
  1. 问题描述
      不超过19000的正整数中,与19000互质的数的个数是多少?
    答案提交
      这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

/**
 * 质数又称素数。指整数在一个大于1的自然数中,除了1和此整数自身外,
 * 没法被其他自然数整除的数。换句话说,只有两个正因数(1和自己)
 * 的自然数即为素数。比1大但不是素数的数称为合数。1和0既非素数也非合数。
 * 素数在数论中有着很重要的作用。
 * ------------------------------------------------------------------------------
 * 互质是公约数只有1的两个整数,叫做互质整数。
 * 公约数只有1的两个自然数,叫做互质自然数,后者是前者的特殊情形。
 */
import java.util.Scanner;

public class PrimeNumber {
	public static int euler(int n){
		int res = n;
		for(int i= 2 ;i*i<=n;i++){
			if(n%i==0){
	            res=res/i*(i-1);
	            while(n%i==0){
	                n/=i;
	            }
	        }
		}
		if(n>1){
	        res=res/n*(n-1);
	    }
	    return res;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		    Scanner input  = new Scanner(System.in);
		    int n = input.nextInt();
		    int res = euler(n);
		    System.out.println(res);
	}
}
  1. 问题描述
      给定一个单词,请计算这个单词中有多少个元音字母,多少个辅音字母。
      元音字母包括 a, e, i, o, u,共五个,其他均为辅音字母。
    输入格式
      输入一行,包含一个单词,单词中只包含小写英文字母。
    输出格式
      输出两行,第一行包含一个整数,表示元音字母的数量。
      第二行包含一个整数,表示辅音字母的数量。
    样例输入
    lanqiao
    样例输出
    4
    3
    评测用例规模与约定
      对于所有评测用例,单词中的字母个数不超过100。

public class Demo3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1 = 0;
        String str = input.nextLine();
        for(int i = 0; i< str.length(); i++){
            char temp = str.charAt(i);
            if (temp=='a' || temp=='e' || temp=='i' || temp=='o' ||temp=='u'){
                num1++;
            }
        }
        System.out.println(num1);
        System.out.println(str.length()-num1);
    }
}
  1. 问题描述
      在数列 a_1, a_2, …, a_n中,如果 a_i 和 a_j 满足 i < j 且 a_i > a_j,则称为一个逆序数对。
      给定一个数列,请问数列中总共有多少个逆序数对。
    输入格式
      输入的第一行包含一个整数 n。
      第二行包含 n 个整数 a_1, a_2, …, a_n,相邻的整数间用空格分隔,表示给定的数列。
    输出格式
      输出一行包含一个整数,表示答案。
    样例输入
    6
    3 1 5 2 3 5
    样例输出
    4
    评测用例规模与约定
      对于 50% 的评测用例,1 <= n <= 100,0 <= 数列中的数 <= 1000。
      对于所有评测用例,1 <= n <= 1000,0 <= 数列中的数 <= 10000。

public class Demo4 {
    Scanner input = new Scanner(System.in);
    public int[] getinput(int n){
        int temp[]  = new int[n];
        for(int i = 0;i<n;i++) {
            temp[i] = input.nextInt();
        }
        return temp;
    }
    public int getResult(int [] temp){
        int res = 0;
        for(int i = 0;i<temp.length-1;i++){
            for (int j = i+1;j<temp.length;j++) {
                if (temp[i] > temp[j]) {
                    res++;
                }
            }
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        Demo4 demo4 = new Demo4();
        int[] temp = demo4.getinput(n);
        int res = demo4.getResult(temp);
        System.out.println(res);
    }
}
  1. 问题描述
      一个平面向量表示从一个坐标点到另一个坐标点的变化量,一般用两个数 (x, y) 来表示。
      两个向量相加是指分别将对应的两个数相加,例如 (x_1, y_1) 与 (x_2, y_2) 相加后得 (x_1+x_2, y_1+y_2)。
      如果两个向量相加后,得到的向量两个值相等,我们称这两个向量为和谐向量对。例如 (3, 5) 和 (4, 2) 是和谐向量对。
      给定 n 个向量,问能找到多少个和谐向量对?
    输入格式
      输入的第一行包含一个整数 n,表示向量的个数。
      接下来 n 行,每行两个整数 x_i, y_i,表示一个向量。
    输出格式
      输出一行,包含一个整数,表示有多少个和谐向量对。
      请注意,自己和自己不能成为一个和谐向量对。
    样例输入
    5
    9 10
    1 3
    5 5
    5 4
    8 6
    样例输出
    2
    样例输入
    4
    1 1
    2 2
    1 1
    2 2
    样例输出
    6
    样例说明
      每两个向量组成一个和谐向量对。
    评测用例规模与约定
      对于 70% 的评测用例,1 <= n <= 1000;
      对于所有评测用例,1 <= n <= 100000,-1000000 <= x_i, y_i <= 1000000。
      请注意答案可能很大,可能需要使用 long long 来保存。

public class Demo5 {
    Scanner input = new Scanner(System.in);

    public int[][] getinput(int n) {
        int temp[][] = new int[n][2];
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0;j<temp[0].length;j++){
                temp[i][j] = input.nextInt();
            }
        }
        return temp;
    }

    public int getResult(int[][] temp) {
        int res = 0;
        for (int i = 0; i < temp.length - 1; i++) {
            for (int j = i + 1; j < temp.length; j++) {
                if (temp[i][0] + temp[j][0]==temp[i][1]+temp[j][1]) {
                    res++;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        Demo5 demo5 = new Demo5();
        int temp[][] = demo5.getinput(n);
        int value  = demo5.getResult(temp);
        System.out.println(value);
    }
}
  1. 问题描述
      给定一个序列 a_1, a_2, …, a_n。其中 a_1 是最大的数,没有其他数与 a_1 相等。
      对于从第二个数开始的每个数 a_i,请找到位置在 a_i 之前且比 a_i 大的,位置上距离 a_i 最近的数 a_j。称 i-j 为 a_i 的前向距离。
      对于给定的序列,请求出所有数的前向距离之和。
    输入格式
      输入的第一行包含一个整数 n,表示序列的长度。
      第二行包含 n 个正整数,为给定的序列。
    输出格式
      输出一个整数,表示序列中所有数的前向距离之和。
    样例输入
    8
    9 1 3 5 2 7 6 3
    样例输出
    14
    样例说明
      序列中从第二项开始的前向距离依次为:
      1, 2, 3, 1, 5, 1, 1
      和为14。
    数据规模和约定
      对于70%的评测用例,1 <= n <= 1000;
      对于所有评测用例,1 <= n <= 100000,a_1 <= 1000000。
      请注意答案可能很大,可能需要使用 long long 来保存。

public class Demo6 {
    Scanner input = new Scanner(System.in);
    public int[] getinput(int n){
        int temp[]  = new int[n];
        for(int i = 0;i<n;i++) {
            temp[i] = input.nextInt();
        }
        return temp;
    }
    public int getResult(int [] temp){
        int res = 0;
        for(int i = 1;i<temp.length;i++){
            for (int j = i-1;j>=0;j--) {
                if (temp[i] < temp[j]) {
                    res+=i-j;
                    break;
                }
            }
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        Demo6 demo6 = new Demo6();
        int[] temp = demo6.getinput(n);
        int res = demo6.getResult(temp);
        System.out.println(res);
    }
}
  1. 小明和朋友们一起去郊外植树,他们带了一些在自己实验室精心研究出的小树苗。
      小明和朋友们一共有 n 个人,他们经过精心挑选,在一块空地上每个人挑选了一个适合植树的位置,总共 n 个。他们准备把自己带的树苗都植下去。
      然而,他们遇到了一个困难:有的树苗比较大,而有的位置挨太近,导致两棵树植下去后会撞在一起。
      他们将树看成一个圆,圆心在他们找的位置上。如果两棵树对应的圆相交,这两棵树就不适合同时植下(相切不受影响),称为两棵树冲突。
      小明和朋友们决定先合计合计,只将其中的一部分树植下去,保证没有互相冲突的树。他们同时希望这些树所能覆盖的面积和(圆面积和)最大。
    输入格式
      输入的第一行包含一个整数 n ,表示人数,即准备植树的位置数。
      接下来 n 行,每行三个整数 x, y, r,表示一棵树在空地上的横、纵坐标和半径。
    输出格式
      输出一行包含一个整数,表示在不冲突下可以植树的面积和。由于每棵树的面积都是圆周率的整数倍,请输出答案除以圆周率后的值(应当是一个整数)。
    样例输入
    6
    1 1 2
    1 4 2
    1 7 2
    4 1 2
    4 4 2
    4 7 2
    样例输出
    12

发布了68 篇原创文章 · 获赞 57 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43598138/article/details/103341731