编程语言
首页 > 编程语言> > 第九届蓝桥杯全球决赛大学生本科JavaB组

第九届蓝桥杯全球决赛大学生本科JavaB组

作者:互联网

第九届蓝桥杯全球决赛大学生本科JavaB组

文章目录

T1:三角形面积

在这里插入图片描述

// 海伦公式
public class Main {
 
	public static void main(String[] args) {
		double a = Math.sqrt((6.4 - 2.3) * (6.4 - 2.3) + (3.1 - 2.5) * (3.1 - 2.5));
		double b = Math.sqrt((5.1 - 2.3) * (5.1 - 2.3) + (7.2 - 2.5) * (7.2 - 2.5));
		double c = Math.sqrt((6.4 - 5.1) * (6.4 - 5.1) + (7.2 - 3.1) * (7.2 - 3.1));
		double q = (a + b + c) / 2.0;
		double area = Math.sqrt(q * (q - a) * (q - b) * (q - c));
		System.out.println(area);
	}
 
}

T2 最大面积

在这里插入图片描述

// 答案:839542176
public class Main {
	static int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	static int ans = 0;
 
	public static void main(String[] args) {
		f(a.length - 1);
		System.out.println(ans);
	}
 
	public static void f(int start) {
		if (start >= 0 && start <= 7) {
			check(start);
		}
 
		for (int i = start; i >= 0; i--) {
			{
				int temp = a[i];
				a[i] = a[start];
				a[start] = temp;
			}
			f(start - 1);
			{
				int temp = a[i];
				a[i] = a[start];
				a[start] = temp;
			}
		}
	}
 
	public static boolean check(int start) {
		String s1 = "";
		for (int i = 0; i <= start; i++) {
			s1 += a[i];
		}
		String s2 = "";
		for (int i = start + 1; i < a.length; i++) {
			s2 += a[i];
		}
		int num1 = Integer.parseInt(s1);
		int num2 = Integer.parseInt(s2);
		int sum = num1 * num2;
		if (sum < 830000000 || sum > 987654321)
			return false;
		if (!isOk(sum))
			return false;
		if (sum > ans)
			ans = sum;
		return true;
	}
 
	public static boolean isOk(int num) {
		String s = "" + num;
		for (int i = 1; i <= 9; i++) {
			if (s.indexOf(i + "") == -1)
				return false;
		}
		return true;
	}
}

T3 全排列

在这里插入图片描述
在这里插入图片描述

import java.util.*;
public class A
{
	static void permu(char[] data, int cur){
		if(cur==data.length-1){
			System.out.println(new String(data));
			return;
		}
		
		for(int i=cur; i<data.length; i++){
			char tmp = data[i]; 
			for(int j=i-1; j>=cur; j--) data[j+1] = data[j];
			data[cur] = tmp;			
 
			permu(data, cur+1);			
 
			tmp = data[cur]; 
			
	for(int j = cur; j < i; j++) data[j] = data[j + 1];
			data[i] = tmp;			
		}
	}
	
	static void permu(String x){
		permu(x.toCharArray(),0);
	}
	
	public static void main(String[] args){
		permu("1234");
	}
}

T4 整理玩具

在这里插入图片描述

package Test9;

import java.util.Scanner;

public class Test4 {

    public static void main(String[] args) {
        //三个点求区的 面积
        Scanner s= new Scanner(System.in);
        int M  = s.nextInt();
        int N =s.nextInt();
        int [][] arr = new int[M][N];
        int [][] n = new int[10][4];
        System.out.println(n[0][0]);
        s.nextLine();
        for (int i = 0; i < M; i++) {

           String str= s.nextLine();
            for (int j = 0; j < N; j++) {
                arr[i][j] = Integer.parseInt(str.substring(j,j+1));
                if (i<n[arr[i][j]][0]){
                    n[arr[i][j]][0]=i;
                }  if (j<n[arr[i][j]][1]){
                    n[arr[i][j]][1]=j;
                }  if (i>n[arr[i][j]][2]){
                    n[arr[i][j]][2]=i;
                }  if (j>n[arr[i][j]][3]){
                    n[arr[i][j]][3]=j;
                }
            }
        }

        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }

        OUT:for (int i = 0; i < 10; i++) {
            if (n[i][0]==n[i][2]&&n[i][1]==n[i][3]){
                continue;
            }else {
                for (int k = n[i][0]; i <=  n[i][2]; i++) {
                    for (int j = n[i][1]; j <= n[i][3]; j++) {
                        if (arr[k][j] != i){
                            System.out.println("不符合");
                            break OUT;
                        }
                    }
                }
            }

        }
        System.out.println("符合");

    }
    //整理玩具

}

T5版本分支

在这里插入图片描述

package Test9;

import java.util.Scanner;

public class Test5 {

    //写一个并查集

    static int parent[] = new int[100000];

    static{
        for (int i = 0; i < parent.length; i++) {
            parent[i]=i;
        }
    }

    static void union(int x, int y){
        parent[find(y)]=x;
    }

    static int find(int x){
        if (parent[x] == x){
            return x;
        }else {
            return find(parent[x]);
        }
    }
    static boolean find(int x,int y){

        do {
            if (parent[y]==x) return true;

            y = parent[y];
        }while (parent[y] != y);
        return false;

    }

    public static void main(String[] args) {
        Scanner s= new Scanner(System.in);
        int M = s.nextInt();
        int Q = s.nextInt();
        System.out.println(M+Q);
        for (int i = 0; i < M-1; i++) {
            union(s.nextInt(),s.nextInt());
        }
        for (int i = 0; i < Q; i++) {
            if (find(s.nextInt(),s.nextInt())){
                System.out.println("YES");
            }else {
                System.out.println("NO");
            }

        }
    }

    

}

标签:第九届,JavaB,int,System,蓝桥,start,static,data,public
来源: https://blog.csdn.net/weixin_46350527/article/details/115642903