[Daily Blue Bridge] 9. The real question of the provincial Java group in the first three years of "the number that cannot be bought"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Unable to buy

Xiao Ming opened a candy store. He was ingenious: he packed the fruit candy into two packages of 4 and 7 each. The candy cannot be unpacked. When children come to buy candy, he uses these two packages to combine. Of course, the number of some candies cannot be combined. For example, if you want to buy 10 candies, you can test it with a computer. In this type of packaging, the maximum number that cannot be bought is 17, and any number greater than 17 can be used with 4 and 7 combination,

The requirement of this question is to find the largest number that cannot be combined when the quantities of two packages are known.

enter:

Two positive integers, indicating the number of sugar in each package (not more than 1000)

Request output:

A positive integer, indicating the maximum amount of sugar that cannot be bought,

No need to consider unsolvable situations

E.g:

User input:

4  7

The program should output:

17

 

Another example:

User input:

3  5

The program should output:

7

 

Resource agreement:
peak memory consumption <64M

CPU consumption <1000ms

Please output in strict accordance with the requirements, and do not print superfluous content similar to: "Please enter...".

All the codes are placed in the same source file. After the test passes, copy and submit the source code.

Note: the main function needs to return 0

Note: Only use ANSI C/ANSI C++ standards, do not call special functions that depend on the compilation environment or operating system

Note: All dependent functions must be clearly in the source file, #include<xxx> can not be set through the project to ignore common header files

When submitting, pay attention to selecting the desired compiler type

Problem-solving ideas:

Part of the mathematical knowledge is used in the solution process of this problem, and it can also be understood as the calculation of solving the linear equation in two unknowns. In this problem, we can assume that the number of two kinds of candies in the package is a and b, a candy Buy package x, package b candies and package y, a total of C candies, then the combination that meets the requirements should meet the following formula:

a * x + b * y = C

Here we have two solutions, for two positive integers without a common divisor : the
first: use the formula directly: the largest number that cannot be combined = a*bab

The second method: use enumeration to list all the numbers that can be formed, but there is an upper limit, that is, the number that can be formed should be less than a*b, then according to the numbers that can be formed, find the largest number that cannot be formed , The specific implementation can be seen in the program.

Answer source code:

method one:

package 一三年省赛真题;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Year2013_t9 {

//	求ax+by=C
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		System.out.println(answer_one(a, b));
	}
	
	/**
	 * 解法一 最大不能组合的数就是 C = a*b-a-b
	 * */
	public static int answer_one(int a,int b) {
		return a*b-a-b;	//利用公式直接求得不能组成的最大的数字
	}

}

Method Two:

package 一三年省赛真题;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Year2013_t9 {

//	求ax+by=C
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		System.out.println(answer_two(a, b));
	}
	
	/**
	 * 解法二
	 * */
	public static int answer_two(int a,int b) {
		int max = a*b;	//取得一个上限
		Set<Integer> ss = new HashSet<Integer>();	//存放能组成的数字
		for (int x = 0; a*x <= max ; x++) {		//从第一种糖果0包开始枚举
			for (int y = 0; b*y <= max; y++) {
				ss.add(a*x+b*y);	//将能组成的数字放到哈希表中,可以避免数值重复
			}
		}
		
		// 从max-1开始向下遍历,找出第一个不在哈希表中的数字
		for (int i = max-1; i > 0; i--) {
			if (!ss.contains(i)) {	//如果该数字不在哈希表中,则返回结果
				return i;
			}
		}
		return 0;
	}

}

Sample output:

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the 2020 Blog Star, please help me to vote for it!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/112851208