#UVa:11185-Ternary

灆洢 2012-09-16 00:28:22

利用迴圈轉換成3進位數字即可。

C++(0.008)

/*******************************************************/
/* UVa 11185 Ternary                                   */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2012/09/16                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int N;
  string output;

  while( scanf( "%d", &N ) != EOF && N >= 0 ){

    output = "";
    do{
      output = (char)((N%3)+(int)'0') + output;
      N /= 3;
    }while( N );

    printf( "%s\n", output.c_str() );
  }
  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料