Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

三进制..

Posted by Ly86 at 2010-09-14 14:13:46 on Problem 1702
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include <deque>
#include <utility>
#include <list>
#include <sstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <algorithm>
#include <iomanip>
using namespace std;
typedef unsigned __int64 u64;
template<class T>inline string toStr(const T& v){ostringstream os;os<<v;return os.str();}
#define FOR(i,s,e) for (int (i) = (s); (i) < (e); i++)
#define DEBUG(a)     printf("%s = %s\n", #a, toStr(a).c_str())
int po(int a, int b)
{
    if (b == 1) return a;
    if (b == 0) return 1;
    int k = po(a,b/2);
    if (b % 2 == 0) return k * k ;
    else
    return k * k * a;

}
int changExquisite(int a[],u64 n, int m)
{
    int i = 0;
    while (n > 0)
    {
        a[i++] = n % m;
        n = n / m;
    }
    return i;
}
int main ()
{
    u64 n;
    int a[1000];
    int b[1000];
    int c[1000];
    int  t;
    cin >> t;
    while ( t--)
    {
        scanf("%I64d",&n);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int bit_n = changExquisite(a,n,3);
        int sum = 0;
        FOR(i,0,bit_n)
        {
        if (a[i] >= 3)
            {
                a[i+1]++;
                a[i] = a[i] % 3;
            }
        if (a[i] != 0)
        {

            if (a[i] != 1)
            {
                b[i] = 3 - a[i];
                a[i] = 0;
                a[i+1]++;
            }
        }
        }
        sum = 0;
        FOR(i,0,bit_n+1)
            if (0 != b[i]) sum++;
        if (sum == 0)
        printf("empty ");
        else
        FOR(i,0,bit_n)
        if (0 != b[i])
        {
            printf("%d",b[i] * po(3,i));
            sum--;
            if (0 != sum)
            printf(",");
            else
            printf(" ");
        }
        sum = 0;
        FOR(i,0,bit_n+1)
            if (0 != a[i]) sum++;

        FOR(i,0,bit_n+1)
        if (0 != a[i])
        {
            printf("%d", po(3,i) );
            sum--;
            if (0 != sum)
            printf(",");
        }
        printf("\n");



    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator