集合

作者在 2011-01-09 20:46:01 发布以下内容
  已知一个集合,包含一,若有包含x,则2*x+1以及3*x+1也被包含在里面,编写一个程序按客户要求输出从小到大排列的所要元素!
#include<iostream>
using namespace std;
int a[10000000];
int main()
{
    int i,two,three;
    a[0]=1;
    two=three=0;
    for(i=1;i<10000000;i++)
    {
        if(a[two]*2+1<a[three]*3+1)
        a[i]=a[two++]*2+1;
        else if(a[two]*2+1==a[three]*3+1)
        {
            three++;
            a[i]=a[two++]*2+1;
        }
        else
        a[i]=a[three++]*3+1;
    }
    while(scanf("%d",&i)!=EOF)
    printf("%d\n",a[i-1]);
    return 0;
}
程序 | 阅读 849 次
文章评论,共0条
游客请输入验证码
浏览29767次