careercup-Mathematics and Probability 7.7

Source: Internet
Author: User

7.7 Some of the factors are only 3, 5, 7, please design an algorithm to find out the number of K.

Solution:

First of all, we can find out the first few numbers that meet the conditions, so as to look for solutions to problems.

A simple idea is that for the number already listed, we multiply by 3,5,7 to get a set of numbers and then find the smallest and not yet listed number, adding to this list. Then repeat the above steps: Multiply 3,5,7 to find the smallest and not yet listed number ... The time complexity of this method is O (N2).

There is one problem with this idea, which is to repeat the calculation. For example, for the table above, I want to calculate the next number, then I use 3,5,7 to multiply each number in the table, and then find the smallest and unused number. But like the 3*3,3*5,3*7,5*5,5*7 wait is already calculated and already used, according to the above algorithm will continue to repeat the calculation. So we have no way to avoid repetition? That is to save the calculated numbers and keep them in order. In order to avoid the occurrence of 3 times 5 and then 5 to 3, we can maintain these numbers with 3 queues to prevent duplication in the number we maintain. The 1th queue is responsible for multiplying by 3, the 2nd queue is responsible for multiplying by 5, and the 3rd queue is responsible for multiplying by 7. The algorithm is described as follows:

 1 . Initialize the result res=1 and queue q3,q5,q7  2 . Insert 1*3 , 5 , 1  *7  3 . Find the smallest x in the team header element of three queues, update the result res=x  4   q3,q5,q7 into *x, 5  *x, x Q5, remove X from Q5 and insert 5  *x,7  *x Q7, Then remove X from Q7 and insert 7  *x  5 . Repeat step 3-5  until the number of K satisfies the condition is found 

Note that when X appears in Q5, we do not insert the 3*x into the Q3 because the number has been inserted in the Q5.

C + + Implementation code:

#include <iostream>#include<queue>using namespacestd;intGet_num (intk) {    if(k<=0)        return 0; intRes,cnt=1; Queue<int>Q3,q5,q7; Q3.push (3); Q5.push (5); Q7.push (7);  while(cnt<k) {res=min (Q3.front (), Min (Q5.front (), Q7.front ())); if(res==Q3.front ())            {Q3.pop (); Q3.push (3*res); Q5.push (5*res); Q7.push (7*res); }        Else if(res==Q5.front ())            {Q5.pop (); Q5.push (5*res); Q7.push (7*res); }        Else if(res==Q7.front ())            {Q7.pop (); Q7.push (7*res); } CNT++; }    returnRes;}intMain () {cout<<get_num (Ten) <<Endl;}

careercup-Mathematics and Probability 7.7

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.