The beauty of programming 2.8 find qualified integers, the beauty of 2.8

Source: Internet
Author: User

The beauty of programming 2.8 find qualified integers, the beauty of 2.8

Given an integer N, we need to look for another integer M so that the result given by N * M contains only 1 and 0 digits in the decimal representation. First of all, we can see this question. The first thought is to make M = 1 and increase the value of M accordingly until N * M gets the desired effect. However, if N is large, therefore, we need to find a better solution.

The solution mentioned in the book is a bit complicated. The method I introduced here is also very simple, that is, to think about the problem. We use numbers that only contain 1 and 0 to Remove N, if the remainder is 0, M is found, and M is equal to the number except N.

Using the above ideas to solve the problem is not difficult to write code, we can define a queue, and then put the appropriate number into the queue from large to small, divide N in order to view the remainder.

The function declaration is as follows:

/* 2.8 find the qualified integer */void DutFindNumMakeMultipleOnlyHasOneOrZero (int );

The source code is as follows:

/* Search for a number so that it is multiplied by the given number. Only 1 and 0 exist in the result. * // * It provides a better solution. However, the method is really complicated, so I won't explain it */void DutFindNumMakeMultipleOnlyHasOneOrZero (int n) {if (n <= 0) return; queue <long> q; q. push (1); while (1) {long t = q. front (); q. pop (); if (t % n = 0) {cout <t <"=" <n <"*" <t/n <endl; break;}/* The idea here is to remove a given number from the decimal number containing only 1 and 0. If the remainder is 0, it is stopped */q. push (10 * t); q. push (10 * t + 1 );}}




One Algorithm

Is 1-N a negative number? Are you indicating an error or I don't understand it, bro?
 
Faster, please.

To produce 2800-x bottles of B type drinks, the raw materials A and B consumed by the beverage must be controlled within grams. Therefore, there is an inequality group:
20X + 30 (100-X) ≤ 2800,
40X + 20 (100-X) ≤ 2800
We can get 20 ≤ X ≤ 40. There are 21 integers X that meet the conditions, so there are 21 production solutions.
Y = 2.6X + 2.8 (100-X) = 280-0.2X. The larger the value of X, the lower the total cost. Therefore, if X is set to 40, the cost is 272.

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.