1298. Digital Conversion

Source: Internet
Author: User

1298. Digital Conversion

Description

There is a number system with a base number of 3. The weight value can be-101, 1, and expressed respectively with the symbol-, 0, 1. For example, of this number system represents 10 of the decimal number, that is, 1*(3 ^ 2) + 0*(3 ^ 1) + 1*(3 ^ 0) = 10, another example is that-0 in this number system indicates-3 of the decimal number, that is,-1 * (3 ^ 1) + 0*(3 ^ 0) =-3. Programming requires that a given signed integer be converted to a new number. There cannot be any excess 0 before this number. For example, if the value of 10 is 101, do not output it to 0101.

Input

The file has one or more rows, and each row has an integer.N(-2,147,483,647 ≤ n ≤ 2,147,483,647), there is no other Separator in the integer.

Output

Output a line for each row of the input file. This line is a new numeric representation of the integer of the input line. No extra blank lines are allowed. Leading spaces are not allowed before each line.

Sample Input

10
-3
Sample output

101
-0
Problem Source

Zsuacm team member

Pay attention to various situations.

#include <iostream>#include <cstring>using namespace std;int main(){    int n;    while(cin>>n)    {        if(n==0)        {            cout<<0<<endl;            continue;        }        char result[50];        for(int i=0;i<50;i++)            result[i]='A';        int shang,yu,i=0;        while(n!=0)        {            yu=n%3;            shang=n/3;            if(yu==2)            {                shang++;                result[i]='-';            }            else if(yu==-1)            {                result[i]='-';            }            else if(yu==1)            {                result[i]=1+'0';            }            else if(yu==-2)            {                shang--;                result[i]=1+'0';            }            else result[i]='0';            n=shang;            i++;        }        int index=49;        while(result[index]=='A')            index--;        for(int i=index;i>=0;i--)            cout<<result[i];        cout<<endl;    }    return 0;}            

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.