UVa 11185 Ternary

Code:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<vector>
using namespace std;
string three(int n)
{
    int k=0;
    string ans="";
    while(n>0)
    {
        k=n%3;
        if(k==1) ans+="1";
        else if(k==2) ans+="2";
        else ans+="0";
        n/=3;
    }
    return ans;
}
int main()
{
    int n;
    while(cin>>n && n>=0)
    {
        string ans=three(n);
        if(n==0) {cout<<'0'<<endl;continue;}
        reverse(ans.begin(),ans.end());
        cout<<ans<<endl;
    }
    return 0;

}

留言

這個網誌中的熱門文章

Things a Little Bird Told Me: Confessions of the Creative Mind

UVa 12970 Alcoholic Pilots

UVa 483 Word Scramble