/*
2020年4月3日19:13:24
十进制转n进制 n >= 2 && n <= 36
输入规模为 2^63
*/
1 #include<iostream> 2 using namespace std; 3 4 char str[36] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; 5 6 int main() 7 { 8 long long num; 9 while(cin >> num) 10 { 11 int x,t; 12 int i = 0; 13 char s[100]; 14 cin >> x; //几进制 15 while(true) 16 { 17 t = num % x; 18 num /= x; 19 if(num == 0) 20 { 21 s[i++] = str[t]; 22 break; 23 } 24 else 25 s[i++] = str[t]; 26 } 27 cout << "i = " << i << endl; 28 for(int j = 0;j < i;j++) 29 cout << s[i - j - 1]; 30 cout << endl; 31 } 32 return 0; 33 }
EG:十进制:10 转换成 二进制:1010
(第一次) 10%2 = 0 , 10/2 = 5
(第二次) 5 % 2 = 1 , 5/2 = 2
(第三次) 2 % 2 = 0 , 2/2 = 1
(第四次) 1 % 2 = 1 , 1/2 = 0
这时候发现规律了吧----短除算法计算的结果是从低位开始的(逆序的)