Solution of 11185 - Ternary

Problem Description
source:https://uva.onlinejudge.org/external/111/11185.html

You will be given a decimal number. You will have to convert it to its ternary (Base 3) equivalent. Input The input file contains at most 100 lines of inputs. Each line contains a non-negative decimal integer N (N < 1000000001). 

Input 

is terminated by a line containing a negative value. This line should not be processed. 

Output 

For each line of input produce one line of output. This line contains the ternary equivalent of decimal value N. 

Sample Input 

10 
100 
1000 
-1 

Sample Output 

101 
10201 
1101001


Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
void Ternary(long long int value);
int main()
{
    long long int value;
    while(scanf("%lld",&value)==1)
    {
        if(value<0)
            break;
        Ternary(value);
        printf("\n");
    }
    return 0;
}

void Ternary(long long int value)
{
    int mod;
    mod=value%3;
    value/=3;
    if(value!=0)
    {
        Ternary(value);
        printf("%d",mod);
    }

    else
        printf("%d",mod);
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience