24h購物| | PChome| 登入
2012-01-12 19:42:41| 人氣784| 回應0 | 上一篇 | 下一篇

[UVA] 11185 - Ternary

推薦 0 收藏 0 轉貼0 訂閱站台

Problem K
Ternary
Input: Standard Input

Output: Standard Output

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                               Output for Sample Input

10
100
1000
-1
101
10201
1101001
 





#include<stdio.h>
void Base3(int n) {
    if(n == 0)    return ;
    Base3(n/3);
    printf("%d", n%3);
}
int main() {
    int n;
    while(scanf("%d", &n) == 1 && n >= 0) {
        if(n == 0)    {puts("0");continue;}
        Base3(n);
        puts("");
    }
    return 0;
}

台長: Morris
人氣(784) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11172 - Relational Operator
此分類上一篇:[UVA] 10340 - All in All

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文