hdu_problem_2031_進制轉換

/*
*
*Problem Description
*輸入一個十進制數N,將它轉換成R進制數輸出。
*
*
*Input
*輸入數據包含多個測試實例,每個測試實例包含兩個整數N(32位整數)和R(2<=R<=16, R<>10)。
*
*
*Output
*爲每個測試實例輸出轉換後的數,每個輸出佔一行。如果R大於10,則對應的數字規則參考16進制(比如,10用A表示,等等)。
*
*
*Sample Input
*7 2
*23 12
*-4 3
*
*
*Sample Output
*111
*1B
*-11
*
*
*Author
*lcy
*
*
*Source
*C語言程序設計練習(五)
*
*
*Recommend
*lcy
*
*/
#include<iostream>
#include<string>
using namespace std;
void transform(int n, int r) {
 bool mark = 0;
 if (n < 0) {
  n = -n;
  mark = 1;
 }
 string result;
 char x;
 while (n != 0) {
  if (n % r > 9) {
   x = 'A' + n % r - 10;
   result = x + result;
  }
  else {
   x = '0' + n % r;
   result = x + result;
  }
  n /= r;
 }
 if (mark) result = '-' + result;
 cout << result << endl;
}
int main() {
 int n, r;// r是進制
 while (cin >> n >> r) {
  transform(n, r);
 }
 system("pause");
 return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章