Description
定义一种集合,如果x属于这个集合,则2*x+1和3*x+1也属于这个集合,已知1属于这个集合,问这个集合从小到大第n个元素是多少(1<=n<=10000000),
Input
多组用例,每组一个整数n,以文件尾结束输入
Output
对于每组用例,输出该集合中第n个元素
Sample Input
100
254
Sample Output
418
1461
Solution
这个集合是通过集合里的每一个数×2,×3+1来扩展的,要想从小到大,从num[1]开始扩展时,由于×2,x3+1大小不同,所以num[]的下一个元素是这两个数的最小值。
Code
#include<stdio.h>
#define min(x,y) (x)<(y)?(x):(y)
long f[10000001];
int main()
{
int n,i,x2,x3;
x2=x3=1;
f[x2]=f[x3]=1;
for(i=2;i<10000001;i++)
{
f[i]=min(f[x2]*2+1,f[x3]*3+1);
if(f[i]==f[x2]*2+1)
x2++;
if(f[i]==f[x3]*3+1)
x3++;
}
while(scanf("%d",&n)!=EOF)
printf("%ld\n",f[n]);
return 0;
}