Quantity not available

Source: Internet
Author: User

/* James opened a candy store. He was ingenious: He made water fructose into four one package and seven one package. Candy cannot be sold in a package.
When a child comes to buy sugar, he uses these two packages for combination. Of course, the quantity of some candy cannot be combined, for example, to buy 10 sugar.
You can test it on a computer. In this case, the maximum number of packages that cannot be purchased is 17. Any number greater than 17 can be combined with 4 and 7.
The requirement for this question is to find the maximum number that cannot be combined when the number of two packages is known.
Input:
Two positive integers, indicating the number of sugar in each package (no more than 1000)
Required output:
A positive integer that indicates the maximum number of sugar that cannot be bought


For example:
User input:
4 7
The program should output:
17


For example:
User input:
3 5
The program should output:

7 */

Method 1:


[Cpp]
# Include "stdio. h"
# Include "stdlib. h"
# Include "time. h"
# Deprecision max 100000000
Int main ()
{Int m, n;
Long int start, finish;
Scanf ("% d", & m, & n );
Start = clock ();
Long int * p;
P = (long *) malloc (sizeof (long) * max); // open up space and create a table 0, 1
P [0] = 1; // Initialization
For (long int I = 1; I <max; I ++)
{P [I] = 0; // Initialization
If (I> = m & p [I-m]) p [I] = 1;
Else
If (I> = n & p [I-n]) p [I] = 1;
}
Long int kk = 0;
For (long int I = 0; I <max; I ++)
{If (I> kk &&! P [I])
Kk = I;
}
Printf ("% ld", kk );
Finish = clock ();
Printf ("\ nall time is: % lfs", (finish-start)/1000.0 );
Printf ("\ n ");
System ("pause ");
}

# Include "stdio. h"
# Include "stdlib. h"
# Include "time. h"
# Deprecision max 100000000
Int main ()
{Int m, n;
Long int start, finish;
Scanf ("% d", & m, & n );
Start = clock ();
Long int * p;
P = (long *) malloc (sizeof (long) * max); // open up space and create a table 0, 1
P [0] = 1; // Initialization
For (long int I = 1; I <max; I ++)
{P [I] = 0; // Initialization
If (I> = m & p [I-m]) p [I] = 1;
Else
If (I> = n & p [I-n]) p [I] = 1;
}
Long int kk = 0;
For (long int I = 0; I <max; I ++)
{If (I> kk &&! P [I])
Kk = I;
}
Printf ("% ld", kk );
Finish = clock ();
Printf ("\ nall time is: % lfs", (finish-start)/1000.0 );
Printf ("\ n ");
System ("pause ");
} // A small number of optimizations. If n consecutive p [I] = 1 occurs when the number is larger, then no judgment is needed for all the subsequent numbers. So optimized, n <m

 

 

 
 

 

 


Method 2:

 

 

[Cpp]
# Include "stdio. h"
# Include "stdlib. h"
# Include "time. h"
# Deprecision max 100000000
Int main ()
{Int m, n, temp;
Long int start, finish;
Scanf ("% d", & m, & n );
Start = clock ();
Long int * p;
P = (long *) malloc (sizeof (long) * max); // open up space and create a table 0, 1
P [0] = 1; // Initialization
If (m <n)
{Temp = m; m = n; n = temp;} // ensure n <m
Temp = 0; // Initialization
For (long int I = 1; I <max; I ++)
{P [I] = 0; // Initialization
If (I> = m & p [I-m]) p [I] = 1;
Else
If (I> = n & p [I-n]) p [I] = 1;
If (p [I]) temp ++; else temp = 0;
If (temp = n) {printf ("% ld", I-n); break ;}
}
/* Long int kk = 0;
For (long int I = 0; I <max; I ++)
{If (I> kk &&! P [I])
Kk = I;
}
Printf ("% ld", kk );*/
Finish = clock ();
Printf ("\ nall time is: % lfs", (finish-start)/1000.0 );
Printf ("\ n ");
System ("pause ");
}

# Include "stdio. h"
# Include "stdlib. h"
# Include "time. h"
# Deprecision max 100000000
Int main ()
{Int m, n, temp;
Long int start, finish;
Scanf ("% d", & m, & n );
Start = clock ();
Long int * p;
P = (long *) malloc (sizeof (long) * max); // open up space and create a table 0, 1
P [0] = 1; // Initialization
If (m <n)
{Temp = m; m = n; n = temp;} // ensure n <m
Temp = 0; // Initialization
For (long int I = 1; I <max; I ++)
{P [I] = 0; // Initialization
If (I> = m & p [I-m]) p [I] = 1;
Else
If (I> = n & p [I-n]) p [I] = 1;
If (p [I]) temp ++; else temp = 0;
If (temp = n) {printf ("% ld", I-n); break ;}
}
/* Long int kk = 0;
For (long int I = 0; I <max; I ++)
{If (I> kk &&! P [I])
Kk = I;
}
Printf ("% ld", kk );*/
Finish = clock ();
Printf ("\ nall time is: % lfs", (finish-start)/1000.0 );
Printf ("\ n ");
System ("pause ");
}

 


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.