----------------解决方案--------------------------------------------------------
同时包含1 3的子集很多,不是只2~n
----------------解决方案--------------------------------------------------------
[CODE]
#include <stdio.h>
#include <math.h>
int main(void)
{
int i, j, n, k;
while(scanf("%d", &n) != EOF)
{
for(i = 0;;i ++)
if(n >= pow(2, i)&&n <= pow(2, i + 1))
break;
j = i;
if(n == 1)
printf("{}\n");
else if(n == 2)
printf("{ 1 }\n");
else if(n == 3)
printf("{ 3 }\n");
else if(n == 4)
printf("{ 3, 1 }\n");
else
{
printf("{ %.0f, ", pow(3, i));
while(n > 0)
{
n -= pow(2, j);
if(n == 1 || n <= 0)
break;
for(k = 0;;k ++)
{
if(n > pow(2, k)&&n <= pow(2, k + 1))
break;
}
printf("%.0f, ", pow(3, k));
j = k;
}
printf("\b\b }\n");
}
}
return 0;
}
----------------解决方案--------------------------------------------------------