poj1906

Three powers
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4094   Accepted: 1833

Description

Consider the set of all non-negative integer powers of 3. 
S = { 1, 3, 9, 27, 81, ... }

Consider the sequence of all subsets of S ordered by the value of the sum of their elements. The question is simple: find the set at the n-th position in the sequence and print it in increasing order of its elements.

Input

Each line of input contains a number n, which is a positive integer with no more than 19 digits. The last line of input contains 0 and it should not be processed.

Output

For each line of input, output a single line displaying the n-th set as described above, in the format used in the sample output. 

Sample Input

1

7

14

783

1125900981634049

0

Sample Output

{ }

{ 3, 9 }

{ 1, 9, 27 }

{ 3, 9, 27, 6561, 19683 }

{ 59049, 3486784401, 205891132094649, 717897987691852588770249 }

Source

 
#include<iostream>

#include<cmath>

#include<string.h>



using namespace std;



char result[65][50];

//我们不难判断出,data数组里的每个值等于3的index次方(index为下标),

//结合集合的个数为2的x次方个(x为元素个数),那么,每当在集合中增加一个不同的值,

//则子集合翻倍,即个数*2;因此如果我们找到2^x<=n<=2^(x+1),那么data[x]必然要输出。

//如(2^3<= n(14)<=2^4,则data[3](即是27)必要输出。Findpow函数就是找出这个x(即是index)



double Findpow(double n)

{

    double i = 0, t = 1;

    while(t < n)

    {

        t *= 2;

        i++;

    }

    return i;

}



int main()

{

    int  index, j;

    double n;

    char data[65][50]={"\0","1","3","9","27","81","243","729","2187","6561","19683","59049","177147","531441",

                        "1594323","4782969","14348907","43046721","129140163","387420489","1162261467","3486784401",

                        "10460353203","31381059609","94143178827","282429536481","847288609443","2541865828329","7625597484987",

                        "22876792454961","68630377364883","205891132094649","617673396283947","1853020188851841",

                        "5559060566555523","16677181699666569","50031545098999707","150094635296999121","450283905890997363",

                        "1350851717672992089","4052555153018976267","12157665459056928801","36472996377170786403",

                        "109418989131512359209","328256967394537077627","984770902183611232881","2954312706550833698643",

                        "8862938119652501095929","26588814358957503287787","79766443076872509863361","239299329230617529590083",

                        "717897987691852588770249","2153693963075557766310747","6461081889226673298932241",

                        "19383245667680019896796723","58149737003040059690390169","174449211009120179071170507",

                        "523347633027360537213511521","1570042899082081611640534563","4710128697246244834921603689",

                        "14130386091738734504764811067","42391158275216203514294433201","127173474825648610542883299603",

                        "381520424476945831628649898809","1144561273430837494885949696427"};

     while(cin>>n, n)

    {

        memset(result, '\0',sizeof(result));

        j = 0;



        while(n > 0)

        {

            index = Findpow(n);    //用Findpow找到输入的n在数组里的下标

            if(index != 0)          //小心index==0时,data[0]=“0”;

                strcpy(result[j++], data[index]);

            n -=  pow(double(2), index-1);    //这里错了一次,pow有多个版本,必须显式调用double版本。。。

        }



        if(j <= 0)

           cout<<"{ }"<<endl;

        else

        {

           cout<<"{ ";

           while(j > 1)

               cout<<fixed<<result[--j]<<", ";

           cout<<fixed<<result[--j];

           cout<<" }"<<endl;

        }

    }



    return 0;

}

 

你可能感兴趣的:(poj)