1298. 數制轉換

來源:互聯網
上載者:User

1298. 數制轉換

Description

有一種數制的基數是3,權值可以取-1,0,1,並分別用符號-,0,1表示,如這種數制的101表示十進位數的10,即1*(3^2)+0*(3^1)+1*(3^0)=10,又如這種數制的-0 表示十進位數的-3,即-1*(3^1)+0*(3^0)=-3。編程要求把給定的有符號整數轉換為新數制的數,該數的前面不能有多餘的0,如10的新數製表示是101,則不要輸出成0101。

Input

檔案有一行或多行,每行有一個整數N (-2,147,483,647≤N≤2,147,483,647),整數內不會有其他分隔字元。

Output

對輸入檔案的每一行輸出一行,該行是輸入行的整數的新數製表示,不能有多餘空行,每行之前不能有前置空格。

Sample Input

10
-3
Sample Output

101
-0
Problem Source

ZSUACM Team Member

注意各種情況咯。

#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;}            

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.