[Blue Bridge Cup] Exam Training 2013 C++A Group Question 8 Unavailable Number

Unable to buy

Problem description
Xiao Ming opened a candy store. He is ingenious: Pack the fruit candy into two packs of 4 pieces and 7 pieces. Candies cannot be sold unpacked.

When a child comes to buy sweets, he uses these two kinds of packaging to combine. Of course, the number of candies cannot be combined, such as 10 candies.

You can test it with a computer. In this packaging case, the maximum unavailable quantity is 17. Any number greater than 17 can be combined with 4 and 7.

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

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

Output format
A positive integer, indicating the maximum number of sugars that cannot be bought

Sample input 1
4 7
Sample output 1
17
Sample input 2
5 3
Sample output 2
7

Problem analysis 

Draw a conclusion from the meaning of the question: because there must be a solution, the two numbers entered must be relatively prime

a * x + b * y = C, xy is relatively prime, the upper bound of C where the equation has a solution and all integers is x * y;

Method 1: The gap between 0 and 100 points

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	
	int a, b;
	cin >> a >> b;
	cout << a * b - a - b << endl;

	return 0;
}

Method 2: Enumeration

Enumerate all the combinations of two numbers, store them in the collection, and finally output the number that does not appear in the reverse direction, which is the largest number that cannot be combined!

#include <iostream>
#include <set> 
using namespace std;

int main(int argc, char** argv) {
	
	int a, b;
	cin >> a >> b;
	
	set<int> ss;
	
	for(int x = 0; a*x <= a*b; x++){
		for(int y = 0; a*x + b*y <= a*b; y++){
			ss.insert(ss.end(), a*x+b*y);
		}
	}
	
	for(int i = a*b; i >= 0; i--) {
		if(ss.find(i) == ss.end()){	//i 不在set,即为答案 
			cout << i << endl;
			break;
		}
	}

	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115144784