但跟 se7en_enter 的代码比起来,还是显得笨了些,se7en_enter才用了36行,而我就不行,动不动就是100多行
解题方法还是需要继续深入研究;
一起努力吧;
英语不好还想学编程??逆天之路,不由分说!! 数学太差还想学编程??离经叛道,义无返顾!!
贴上我的代码,.刚看过几个高手写的,我都不愿意贴出来的,如果大家要的话,我可以把几个高手写的发上来.
#include<stdio.h>
#include<math.h>
int main()
{
int n,length;
int data[15];
while(EOF!=(scanf("%d",&n))&&n!=0)
{
length=0;
n--;
while(n>0)//根据数据关系,化成二进制
{
data[length++]=n%2;
n/=2;
}
printf("{ ");
if(length!=0)
{
printf("%ld",(long)pow(3,--length));
}
while(--length>=0)
{
if(data[length]==1)
{
printf(", %ld",(long)pow(3,length));
}
}
printf(" }\n");
}
return 0;
}
请大家继续关注...
以下皆转载,看看别人是怎么写的.
#include <stdio.h>
//Viusal C++ 6.0
#define BIGINT __int64
#define FORMAT "%I64d"
/*
mingw use this macro
#define BIGINT long long
#define FORMAT "%I64d"
linux use this macro
#define BIGINT long long
#define FORMAT "%lld"
*/
BIGINT ThreePower[32];
void HeroNeedThree(BIGINT* subset, BIGINT n)
{
int t, p;
BIGINT m;
m = n-1;
for (t = 0; m; m&=m-1) t++;
p = 0; m = n-1; subset[0] = t;
while (t)
{
if (m & 1) subset[t--] = ThreePower[p];
p++;
m >>= 1;
}
}
int main()
{
int i;
BIGINT n, subset[33];
ThreePower[0] = 1;
for (i = 1; i < 32; i++) ThreePower[i] = ThreePower[i-1]*3;
while (EOF != scanf(FORMAT, &n))
{
HeroNeedThree(subset, n);
printf("{ ");
for (i = 1; i < subset[0]; i++) printf(FORMAT", ", subset[i]);
if (subset[0]) printf(FORMAT" ", subset[subset[0]]);
puts("}");
}
return 0;
}
typedef long long unsigned INT64;
void HeroNeedThree (INT64* subset, unsigned int n)
{
static INT64 three[41];
static bool ini = false;
if (ini == false) {
INT64 a = 1;
for (int i = 0; i < 41; ++i, a*=3) {
three[i] = a;
}
ini == true;
}
int bitn = 31;
unsigned int mask = (1 << 31);
for (; bitn >= 0 && ((mask&n)==0); --bitn, mask >>= 1);
int amount = 0;
for (int i = 1; bitn >= 0; mask>>=1, --bitn) {
if (n > mask) {
subset[i] = three[bitn];
amount++;
n -= mask;
i++;
}
}
subset[0] = amount;
}
方法:这跟二进制有点关系.