[strange cup. Blue bridge on cloud - algorithm training camp] week 1

1. Running training

Problem description
Xiao Ming wants to do a running training. At the beginning, Xiao Ming is full of physical strength, and the physical strength value is 10000.

If Xiao Ming runs, he will lose 600 physical strength per minute.
If Xiao Ming has a rest, he will increase his physical strength by 300 per minute.
The loss and increase of physical strength vary evenly.

Xiao Ming plans to run for one minute, rest for one minute, run for another minute, rest for another minute... This cycle.
If Xiaoming's physical strength reaches 0 at a certain time, he will stop exercising. How long will Xiaoming stop exercising.
In order to make the answer an integer, please output the answer in seconds. Only the number, not the unit, is filled in the answer.

Idea analysis: set a flag isRun to indicate whether to run this time. If running physical strength is - 600, if resting, physical strength is + 300 Put these two judgments in the while(true) loop. If you find that the physical strength value is less than 600, but isRun= true this time, you will jump out of the loop and calculate the total time.

package com.test;

public class RunningTest {
	public static void main(String[] args) {
		int energy=10000;
		boolean isRun=true;
		
		int sec=0;
		int min=0;
		
		while(true) {
			if(energy<600&&isRun) break;
			if(isRun) {
				energy-=600;
				isRun=false;
				min++;
			}else {
				energy+=300;
				isRun=true;
				min++;
			}
		}
		
		sec=min*60+energy/10;
		
		System.out.println(sec);
		 	
	}
}

2. Factorial divisor

Problem description
Define factorial n= one × two × three × ··· × n.
Excuse me 100! (factorial of 100) how many divisors are there.

Train of thought analysis: mathematical formula - > any positive integer X can be expressed as the product of several prime numbers, that is, X = p1 α 1 ∗ p2 α 2 …… ∗ pk α k

Number of divisors = (a1 + 1)(a2 + 1)... (ak + 1)

package com.test;

import java.util.ArrayList;
import java.util.List;

public class Factorial {
	private static List<Integer> asList;

	public static void main(String[] args) {
		List<Integer> primeList=new ArrayList<Integer>();
		boolean flag=true;
		for(int i=2;i<=100;i++) {
			flag=true;
			for(int j=2;j<=i-1;j++) {
				if(i%j==0) {
					flag=false;
					break;
				}
			}
			if(flag) {
				primeList.add(i);
			}
		}
		for(int i:primeList) {
			System.out.print(i+",");
		}
		System.out.println();
		Long result=1L;
		int [] arr=new int[] {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
		int size = arr.length;
		for(int i=0;i<size;i++) {
			int mc=0;int n=100;
			while(n!=0) {
				mc+=(n/=arr[i]);		
			}
			result*=(mc+1);
		}
		
		System.out.println(result);
		
	}
}

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
Answer: 39001250856960000

3. Stack order

Planet X pays special attention to order. All roads are one-way streets. A fleet of 16 beetles started successively according to the number, caught in other traffic flows and moved forward slowly. There is a dead end on the roadside, which can only allow one car to pass. It is a temporary checkpoint, as shown in the figure
Planet X is so rigid that every passing car is required to enter the checkpoint, or it may be released without inspection, or it may be carefully inspected. If the order of vehicles entering and leaving the checkpoint can be staggered arbitrarily. So, how many possible orders will the team take when it gets on the road again? For convenience, it is assumed that the checkpoint can accommodate any number of cars. Obviously, if the fleet has only one vehicle, there may be one order; 2 possible sequences of 2 vehicles; There are 5 possible sequences for 3 vehicles.

There are 16 cars now, pro! You need to calculate the number of possible orders

Train of thought analysis:
Define recursive function recur(int m,int n);

m represents the number of vehicles on the left and n represents the number of vehicles at the checkpoint.
Recursive idea - > when m=0, no matter how much n is, the result is 1. Then, the recursive relationship is divided into two cases: when n=0, that is, the number of vehicles at the checkpoint is 0, recur(m,n)=recur(m-1,1); When there is a car at the checkpoint, that is n=0, then recur(m,n)= recur(m-1, n+1)+recur(m, n-1)

package com.test;

public class Planet {
	public static void main(String[] args) {
		Long resultLong=recur(16, 0);
		System.out.println(resultLong);
		
	}
	//m represents the number of vehicles on the left and n represents the number of vehicles in the re inspection station
	public static Long recur(int m,int n) {
		if(m==0) return 1L;
		if(n==0) return recur(m-1, 1);
		if(n>0) return  recur(m-1, n+1)+recur(m, n-1);
		
		return 0L;
	}
}

Answer: 35357670

4. Goldbach decomposition


Train of thought analysis:
First traverse all even numbers of 10000-4, then find the smallest prime number from all prime numbers starting from 3, and then find the largest prime number

package com.test;

public class Decompose {
	public static void main(String[] args) {
		int n=10000;
		int m=0;
		for(int i=n;i>3;i-=2) {
			for(int j=1;j<(i+1)/2;j+=2) {
				if(isPrime(j)&&isPrime(i-j)) {
					m=Math.max(m, j);
					break;
				}
			}
		}
		System.out.println(m);
	}
	
	public static boolean isPrime(int num) {
		 if (num == 1) return false;
		    for (int i = 2; i*i < num+1; i++)
		        if (num % i == 0) return false;
		    return true;
			
	}
		

}
answer: 173

5. Book arrangement

subject
10 books numbered 1 ~ 10 shall be placed on the bookshelf, and the books with adjacent numbers shall not be placed in adjacent positions.

Please calculate how many different permutations there are.
Note that what needs to be submitted is an integer. Do not fill in any redundant content.

Problem solving ideas:
Use depth first search traversal to filter out those that do not meet the conditions

package com.test;

import java.util.LinkedList;

public class DFS01 {
	public static void main(String[] args) {
		int[] options = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		LinkedList<Integer> list = new LinkedList<Integer>();
		test t1 = new test();
		t1.dfs(list, options);
		System.out.println(t1.count);
	}

}

class test {
	int count;

	public void dfs(LinkedList<Integer> path, int[] options) {
		int n = options.length;

		if (path.size() == n) {
			count++;
			return;
		}

		for (int i = 0; i < n; i++) {
			if (path.contains(options[i]))
				continue;
			int[] wrongOpts = null;
			if (path.size() != 0) {
				wrongOpts = wrongOpt(path.getLast());
				boolean flag = true;
				for (int j = 0; j < wrongOpts.length; j++) {
					flag = true;
					if (options[i]==wrongOpts[j]) {
						flag = false;
						break;
					}
				}
				if (flag) {
					path.add(options[i]);
					dfs(path, options);
					path.removeLast();
				}
			} else {
				path.add(options[i]);
				dfs(path, options);
				path.removeLast();
			}
		}

	}

	public int[] wrongOpt(int i) {
		if (i == 1) {
			return new int[] { 2 };
		} else if (i == 2) {
			return new int[] { 1, 3 };
		} else if (i == 3) {
			return new int[] { 2, 4 };
		} else if (i == 4) {
			return new int[] { 3, 5 };
		} else if (i == 5) {
			return new int[] { 4, 6 };
		} else if (i == 6) {
			return new int[] { 5, 7 };
		} else if (i == 7) {
			return new int[] { 6, 8 };
		} else if (i == 8) {
			return new int[] { 7, 9 };
		} else if (i == 9) {
			return new int[] { 8, 10 };
		} else if (i == 10) {
			return new int[] { 9 };
		} else {
			return null;
		}

	}
}
answer:479306

6. Monkeys divide bananas

package com.test;

public class Banana {
	public static void main(String[] args) {
		int n=6;
		
		while(true) {
			if(n%5==1) {
				//The number of bananas when the second monkey woke up
				int a=(n-1)/5*4;
				if(a%5==2) {
					//The number of bananas when the third monkey woke up
					int b=(a-2)/5*4;
					if(b%5==3) {
						//The number of bananas when the fourth monkey woke up
						int c=(b-3)/5*4;
						if(c%5==4) {
							int d=(c-4)/5*4;
							//The number of bananas when the fifth monkey woke up
							if(d%5==0&&d!=0) {
								System.out.println(n);
								break;
							}
						}
					}
				}
			}
			n++;
				
		}
	}
	
	
}
answer:3141

7. Slightly smaller score

Back to primary school----
True fraction: fraction whose numerator is less than denominator
Reduced fraction: the coprime of numerator and denominator, that is, the maximum common divisor is 1

The entrance verification method of Planet x math city is:
A true score is displayed on the screen. You need to quickly find a reduced score smaller than it. The larger the score, the better.
At the same time, limit the denominator of your score to no more than 100.

The following code solves this problem violently. Please analyze it carefully and fill in the missing code in the underlined part.

package com.test;

public class FenShu {
	public static void main(String[] args) {
		int a = 7;
		int b = 13;

		int m, n;
		int max_a = 0;
		int max_b = 1;

		for (n = 100; n > 1; n--) {
			for (m = n - 1; m >= 1; m--) {
				if (m * b < a * n && isPrimeEachOther(m, n)) {
					if (max_a * n < max_b * m) { // Fill in the blanks
						max_a = m;
						max_b = n;
						break;
					}
				}
			}
		}
		System.out.printf("%d/%d",max_a,max_b);

	}

	public static boolean isPrimeEachOther(int m, int n) {
		boolean f1=true; boolean f2=true;
		for(int i=2;i<m;i++) {
			if(m%2==0) {
				f1=false;
				break;
			}
		}
		for(int i=2;i<n;i++) {
			if(n%2==0) {
				f2=false;
				break;
			}
		}
		return f1&&f2;
		
	}
}

answer: 51/95

8.excel address

Problem description

The address representation of Excel cells is very interesting. It uses letters to represent column numbers.
For example,
A stands for column 1,
B represents column 2,
Z represents column 26,
AA means column 27,
AB stands for column 28,
BA stands for column 53,
  ...
Of course, the maximum column number of Excel is limited, so it is not difficult to convert.
If we want to generalize this representation, can we convert large numbers into long letter sequences?
This topic is to output the corresponding Excel address representation of the input number.

sample input
26
sample output
Z

sample input
2054
sample output
BZZ

import java.util.Scanner;
import java.util.Stack;
public class Excel address {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Stack<Integer> stack = new Stack();
        /*Core part Start*/
        while(n!=0){
            if(n%26==0) n-=26;
            stack.push(n%26==0?26:n%26);
            n/=26;
        }
        /*Core part End*/
       while (!stack.isEmpty())
            System.out.print((char)('A'+stack.pop().intValue()-1));
       sc.close();
    }
}

Keywords: Java Algorithm

Added by Ironmann00 on Tue, 11 Jan 2022 16:20:07 +0200