Collection definition C/C + +

Source: Internet
Author: User

Problem description
The definition of set S is as follows: (1) 1 within S; (2) If x is within the set S, then 2x+1 and 3x+1 are also within s; (3) Only the element satisfying the condition (1) (2) is within S. To arrange the elements in S in ascending order, output the nth element in S.
Input
There are several sets of test data. Each set of test data is a row, one positive integer n (1 <= n <= 100000) per line.
Output
For each set of test data, output the nth element of s in a separate line.
Enter a sample column
123456100254
Output sample
13479104181461
The idea is elaborated: This question data compares big 100,000, if uses the circulation to do, certainly time out the tle not to discuss. So it must be a bit tricky, for example, with a Boolean array tag, or false if a number satisfies the rule to be marked true in the collection.
In this way, the generation and sequencing of the collection elements are resolved.
Note: Here is a small hole, if the cycle is defined as n=3000000, the direct use of the loop will definitely re, because the subscript out of bounds, this because I also had a gun I did not say the specific reason, hehe O (∩_∩) o~
Well, nonsense not much to say, the good code came!
1#include <stdio.h>2 #defineN 30000013 BOOLk[3*n]={0};4 intaim[n]={0};5 voidf ()6 {7k[1]=true;8     inti,j;9      for(i=1; i<n;i++)Ten         if(K[i]) One     { Ak[2*i+1]=true; -k[3*i+1]=true; -     } thej=0; -      for(i=0; i<n;i++) -     { -         if(K[i]) +aim[j++]=i; -     } +  A } at intMain () - { -     intN; - f (); -      while(~SCANF ("%d",&N)) -     { inprintf"%d\n", aim[n-1]); -     } to     return 0; +}
If you have any questions, please leave a message O (∩_∩) o~


Collection definition C/C + +

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.