Blue Bridge Cup Java -- algorithm training

Posted by littlejay on Sat, 05 Feb 2022 10:56:46 +0100

catalogue

1. Seal

 2. Take gold coins

 3. Digital game

1. Seal

Problem description

There are n kinds of seals with the same probability of occurrence. Xiao A bought m seals and asked for the probability of collecting n seals.

Input format

Two positive integers n and m in a row

Output format

A real number P represents the answer, with 4 decimal places reserved.

sample input

2 3

sample output

0.7500

Data scale and agreement

1≤n,m≤20

import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        //Enter n and m
        int n = in.nextInt();
        int m = in.nextInt();
        //Define p to represent the probability of each time
        double p = 1.0 / n;
        //Define dp array dp[m][n] Buy m pieces and collect N kinds
        double[][] dp = new double[m + 1][n + 1];
        //i stands for buying i pieces j stands for collecting j kinds
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                //Because i < j, it is impossible to buy i pieces and collect j kinds, so dp=0;
                if (i < j) {
                    dp[i][j] = 0;
                } else if (j == 1) {
                    //If j=1, buy i pieces and put together j kinds, so dp=p to the power of i-1
                    dp[i][j] = Math.pow(p, i - 1);
                } else {
                    //In other cases, i bought j types of i pieces. There are two types of i pieces. The first one is the same as the previous one, and the second one is different from the previous one
                    dp[i][j] = dp[i - 1][j] * (j * p) + dp[i - 1][j - 1] * ((n - j + 1) * p);
                }
            }
        }
        System.out.printf("%.4f", dp[m][n]);
    }
}

 2. Take gold coins

Problem description

There is an N x N square. Each square has some gold coins. You can get the gold coins as long as you stand in the square. You stand in the top left corner of the grid, and you can walk from one grid to its right or lower grid at a time. How can I get the most gold coins.

Input format

On the first line, enter a positive integer n.

The following n lines describe the grid. The number of gold coins is guaranteed to be a positive integer not exceeding 1000.

Output format

The maximum number of gold coins you can take.

sample input

3

1 3 3

2 2 2

3 1 2

sample output

11

Data scale and agreement

n<=1000

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        s.nextLine(); //Remove the effect of carriage return
        int arr[][] = new int[n][n];
        for (int i = 0; i < n; i++) {
            String str [] = s.nextLine().trim().split(" ");//After removing the spaces at the beginning and end of the input String, divide it into a String array under the condition of "".
            for (int j = 0; j < n; j++) {
                arr[i][j]=Integer.parseInt(str[j]);//Change the elements in the character array to numbers
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int left = j-1 < 0 ? 0 : arr[i][j-1];//Judge whether it is the leftmost
                int up = i-1 < 0 ? 0 : arr[i-1][j];//Determine whether it is the top
                arr[i][j] = Math.max(left,up) + arr[i][j];//Take the leftmost and uppermost, and the one with the largest number of gold coins plus the initial number of gold coins in the current grid
            }
        }
        System.out.println(arr[n-1][n-1]); //The for loop starts at 0, so it's n-1
    }
}

 3. Digital game

Problem description

Given a 1-N arrangement a[i], add two adjacent numbers each time to get a new sequence, and then repeat this operation for the new sequence. Obviously, the length of the sequence obtained each time is 1 less than that of the previous sequence, and finally there is only one number left.
For example:
  3 1 2 4
  4 3 6
  7 9
  16
Now, if you know N and the last number sum, request an arrangement with the initial sequence a[i], which is 1 ~ N. If there are multiple answers, the one with the smallest dictionary order will be output. The data is guaranteed to have a solution.

Input format

The second integer, sun, is positive

Output format

An arrangement of 1 ~ N

sample input

4 16

sample output

3 1 2 4

Data scale and agreement

  0<n<=10

import java.util.Scanner;
public class Main{
	static int n;		//Title n
	static int sum;	//sum of topics
	static int b=0;	//It is used to judge whether the answer meets the requirements of the question. If it is found, it does not need to run
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		 n=sc.nextInt();
		 sum=sc.nextInt();
		int[] arr=new int[n];			//Define an array to hold the full sort of 1-n
		int[] vis=new int[n+1];		//1-n is used in the process of total sorting
		dfs(0,arr,vis);					//Recursive search for answers


	}

	private static void dfs(int step, int[] arr, int[] vis) {
		if(b==0){											//If you don't find the answer, keep looking
		if(step==n)
		{
			int[] arr1=new int[n];				//Reserved for output if a matching sequence is found		
			for(int i=0;i<n;i++)
			{
				arr1[i]=arr[i];
			}
			for(int i=0;i<n-1;i++)				//Calculate whether the 1-n ranking meets the sum of the topic according to the requirements of the topic
			{
				for(int j=0;j<n-1;j++)
				{
					arr1[j]=arr1[j]+arr1[j+1];
				}
			}
			if(arr1[0]==sum)				//If it matches, it's over
			{
				for(int i=0;i<n;i++)
				{
					System.out.print(arr[i]+" ");
				} 
				b=1;
			}
				
			return;
			
		}	
		for(int i=1;i<=n;i++)			//This loop is used to find the full sort of 1-n, or you can write one yourself
		{
			if(vis[i]==0)
			{
				arr[step]=i;
				vis[i]=1;	//It means that the value of i in this sequence already exists and cannot appear again. This is a full permutation and a full permutation that cannot be repeated
				dfs(step+1, arr, vis);
				vis[i]=0;	//Backtracking, the above i has been taken. Because of the cycle, the next vis will not be taken
			}
		}
		}
	}
}

 

Topics: Java Eclipse Algorithm