ACM Programming Book title U (Beautiful Number)

Source: Internet
Author: User

Description

Mike is very lucky, as he has both beautiful numbers, 3 and 5. But he's so greedy, that he wants infinite beautiful numbers. So he declares this any positive number which be dividable by 3 or 5 is beautiful number. Given you an integer n (1 <= n <= 100000), could do you have to tell Mike the Nth beautiful number?

Input

The input consists of one or more test cases. For each test case, the there is a single line containing an integer N.

Output

The For each test is in the input, and the result on a line by itself is output.

Sample Input

1
2
3
4

Sample Output

3
5
6
9

The number that can be divisible by 3 or 5 is a beautiful number, the value of the nth beautiful number.

My train of thought was simple, and began to fear timeouts, but did not.

Figure out all the beautiful numbers. There is an array in which the direct subscript minus one is the end of the output.

The code is as follows:

#include <bits/stdc++.h>
using namespace std;
int main ()
{
        int n=0,i=3,num;
        Vector<int> v;
        while (true)
        {
                if (n>100000) break
                        ;
                if (i%3==0| | i%5==0)
                {
                        v.push_back (i);
                        n++;
                }
                i++;
        }
        while (cin>>num)
                cout<<v[num-1]<<endl;
}

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.