0
点赞
收藏
分享

微信扫一扫

第十三届蓝桥杯模拟赛JAVA组

目录

1.IP地址

2.公约数

3.特殊的数

 4.编码长度(哈夫曼编码)

5.矩阵字符

 6.买铅笔

 7.直角三角形

 8.分享秘密

9.半递增序列

10.方格图


1.IP地址

2.公约数

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in); 
		int count = 0;
		for(int i = 1;i <= 2021;i++) {
			if(gcd(i,2021) > 1)
				count++;
		}
		System.out.println(count);
	}

	private static int gcd(int a, int b) {
		return b>0 ? gcd(b,a % b) :a;
	}
}

3.特殊的数

public class Main {
	public static void main(String[] args) {
		int count = 0;
		int[] a = new int[2022];
		for(int i = 1;i <= 1011;i++) {
			for(int j = 0;j < i;j++) {
				int sum = i*i -j *j;
				if(sum <= 2021)
					a[sum] = 1;
			}
		}
		for(int i = 1;i <= 2021;i++) {
			if(a[i] == 1)
				count++;
		}
		System.out.println(count);
	}
}

 4.编码长度(哈夫曼编码)

 

5.矩阵字符

 6.买铅笔

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int t = sc.nextInt();
		int r = t % 12;
		int num = t / 12;
		if(r > 0)
			num++;
		int count = p * num;
		System.out.println(count);
	}
}

 7.直角三角形

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		if(a+b>c || a+c>b || b+c>a) {
			if(a*a + b*b == c*c || a*a + c*c == b*b || c*c + b*b == a*a)
				System.out.println("YES");
			else
				System.out.println("NO");
		}else {
			System.out.println("NO");
		}
	}
}

 8.分享秘密

9.半递增序列

//求组合数,只要选出奇数位置的数,奇数位置和偶数位置的数一定就固定下来
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		if(n%2 == 0)
			System.out.println(C(n,n/2));
		else
			System.out.println(C(n,(n+1)/2));
	}

	private static long C(long a, long b) {
		long ans = 1;
		for(long i=1,j=a;i<=b;i++,j--) {
			ans = ans*(j/i)%1000000007;
		}
		return ans;
	}
}
//杨辉三角形
import java.util.*;
public class Main{
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int k = n / 2;
		int [] a = new int[1001];
		a[0] = 1;
		for(int i=1;i<=n;i++) {
			a[0] = 1;
			a[i] = 1;
			for(int j=i-1;j>0;j--) {
				a[j] = (a[j] + a[j-1])%1000000007;
			}
		}
		System.out.println(a[k]);
	}
}

10.方格图

举报

相关推荐

0 条评论