这一题和http://acm.fjnu.edu.cn/show?problem_id=2013 是非常相似的 我的思路来源于霍纳法则和整数求二进制表示的方法 #include <iostream> using namespace std; typedef long long llg; int main(void) { llg pow3[64]; pow3[0] = 1; for(int i = 1; i < 64; ++i) pow3[i] = pow3[i-1] * 3; int ansArr[64], ansIdx; llg n; int m; while(scanf("%d", &m) != EOF) { for(int k = 0; k < m; ++k) { scanf("%lld", &n); ansIdx = 0; while(n != 0 && ansIdx < 64) { int r = n % 3; n /= 3; if(r == 0) ansArr[ansIdx++] = 0; else if(r == 1) ansArr[ansIdx++] = 1; else { ++n; ansArr[ansIdx++] = -1; } } if(n != 0) { printf("no solution!/n"); continue; } bool have = false; for(int i = 0; i < ansIdx; ++i) { if(ansArr[i] < 0) { if(have) printf(","); have = true; printf("%lld", pow3[i]); } } if(!have) printf("empty"); printf(" "); have = false; for(int i = 0; i < ansIdx; ++i) { if(ansArr[i] > 0) { if(have) printf(","); have = true; printf("%lld", pow3[i]); } } if(!have) printf("empty"); printf("/n"); } } }