有点乱
#include <math.h>
int
main( void )
{
int log_3( int n );
int sum_n( int n );
void prin( int n );
int n;
scanf( "%d", &n );
while( n > 121)
{
scanf( "%d", &n );
}
printf("%d = ", n );
prin( n );
getch();
}
void
prin( int n )
{
int i, sum, ok;
i = log_3( n );
sum = sum_n( i );
if( n > sum )
{
printf( "%d", (int)pow( 3, i+1 ) );
if( n % 3 == 1)
{
printf(" + 1");
ok = pow( 3, i+1 ) - ( n - 1 );
while( ok > 0 )
{
printf(" - %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 2)
{
printf(" - 1 ");
ok = pow( 3, i+1 ) - ( n + 1 );
while( ok > 0 )
{
printf(" - %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 0 )
{
ok = pow( 3, i+1 ) - n;
while( ok > 0 )
{
printf(" - %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
}
else
{
printf( "%d", (int)pow( 3, i ) );
if( n % 3 == 1)
{
printf(" + 1");
ok = ( n - 1 )-pow( 3, i );
while( ok > 0 )
{
printf(" + %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 2)
{
printf(" - 1 ");
ok = ( n + 1 ) - pow( 3, i );;
while( ok > 0 )
{
printf(" + %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 0 )
{
ok = n - pow( 3, i );;
while( ok > 0 )
{
printf(" + %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
}
}
int
log_3( int n )
{
int x = 0;
while( n /= 3 )
{
x++;
}
return x;
}
int
sum_n( int n )
{
int sum = 0;
while( n >= 0 )
{
sum += pow( 3, n );
n --;
}
return sum;
}
#include <math.h>
int
main( void )
{
int log_3( int n );
int sum_n( int n );
void prin( int n );
int n;
scanf( "%d", &n );
while( n > 121)
{
scanf( "%d", &n );
}
printf("%d = ", n );
prin( n );
getch();
}
void
prin( int n )
{
int i, sum, ok;
i = log_3( n );
sum = sum_n( i );
if( n > sum )
{
printf( "%d", (int)pow( 3, i+1 ) );
if( n % 3 == 1)
{
printf(" + 1");
ok = pow( 3, i+1 ) - ( n - 1 );
while( ok > 0 )
{
printf(" - %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 2)
{
printf(" - 1 ");
ok = pow( 3, i+1 ) - ( n + 1 );
while( ok > 0 )
{
printf(" - %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 0 )
{
ok = pow( 3, i+1 ) - n;
while( ok > 0 )
{
printf(" - %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
}
else
{
printf( "%d", (int)pow( 3, i ) );
if( n % 3 == 1)
{
printf(" + 1");
ok = ( n - 1 )-pow( 3, i );
while( ok > 0 )
{
printf(" + %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 2)
{
printf(" - 1 ");
ok = ( n + 1 ) - pow( 3, i );;
while( ok > 0 )
{
printf(" + %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
if( n % 3 == 0 )
{
ok = n - pow( 3, i );;
while( ok > 0 )
{
printf(" + %d", (int)pow( 3, log_3( ok ) ) );
ok -= pow( 3, log_3( ok ) );
}
return ;
}
}
}
int
log_3( int n )
{
int x = 0;
while( n /= 3 )
{
x++;
}
return x;
}
int
sum_n( int n )
{
int sum = 0;
while( n >= 0 )
{
sum += pow( 3, n );
n --;
}
return sum;
}