Waterloo Cup The number of previous questions not to buy (Java solution) __java

Source: Internet
Author: User

"Topic description":

Xiao Ming opened a candy shop. He ingenuity: the fruit sugar into 4 packs and 7 packs of two. Sweets cannot be sold by unpacking.

When the child comes to buy the candy, he uses these two kinds of packing to combine. Of course, some sweets can not be combined, such as to buy 10 sugars.

You can use a computer to test that in this case, the maximum number of not buy is 17. Any number greater than 17 can be combined with 4 and 7.

The requirement of the subject is to find the maximum number that cannot be combined when the quantity of two packages is known. Input format

Two positive integers representing the number of sugars in each package (none more than 1000) output format

A positive integer representing the maximum number of sugars that cannot be bought. Sample Input 1 4 7
Sample Output 1 17 example input 2 3 5
Sample output: 2 7
"Solution 1":

Define a large array, the array first initialized to 0, that is, can not buy a number of 0, and then the number can be bought to 1, and finally traversing forward, the output array of the first 0 of the number, that is, the maximum number of "code" can not be bought:

Import Java.util.Scanner;

public class Main {public
	static void Main (string[] args) {
		Scanner sc = new Scanner (system.in);
		int a = Sc.nextint ();
		int b = Sc.nextint ();
		int s[] = new int[1000000];
		for (int i = 0; i < A * b; i++) {for
			(int j = 0; J < A * b; j +) {
				if (A * i + b * J >= 1000000)/over array range Exit the break
					;
				S[a * i + b * j]++;
			}
		int k = 0;
		for (int i = a * b-1 i >= 0; i--)
			if (s[i] = = 0) {
				k = i;
				break;
		System.out.println (k);
	}
}

"Solution 2": Full backpack

"Code":

Import Java.util.Scanner;

public class Main {public
	static void Main (string[] args) {
		Scanner sc = new Scanner (system.in);
		int a[]=new int[2];
		 A[0] = Sc.nextint ();
		 A[1] = Sc.nextint ();
		int s[] = new int[1000000];
	    S[a[0]]=s[a[1]]=1;
		int m=a[0]*a[1];
		int k = 0;
		for (int i=1;i<m;i++) {for
			(int j=0;j<=1;j++)
				if (a[j]<i)
					S[i]+=s[i-a[j]];
			if (s[i]==0)
				k=i;
		}
		System.out.println (k);
	}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.