HDU 2031 进制转换
进制转换
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58687 Accepted Submission(s): 31957
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
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<set> #include<map> #include<sstream> #include<queue> #include<stack> #include<cmath> #include<list> #include<vector> #include<string> using namespace std; #define long long ll const double PI = acos(-1.0); const double eps = 1e-6; const int inf = 0x3f3f3f3f; const int N = 500005; int n, m, tot; int a[N]; stack<char> s; int main() { while(cin >> n >> m ) { if(n<0) cout<<"-"; int tmp; while(n) { tmp = n%m; tmp = tmp>0?tmp:-tmp; if(tmp<10) s.push(tmp +'0'); else s.push(tmp - 10 + 'A'); n /= m; } while(!s.empty()) { cout<<s.top(); s.pop(); } cout<<endl; } }
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<set> #include<map> #include<sstream> #include<queue> #include<stack> #include<cmath> #include<list> #include<vector> #include<string> using namespace std; #define long long ll const double PI = acos(-1.0); const double eps = 1e-6; const int inf = 0x3f3f3f3f; const int N = 500005; int n, m, tot; int a[N]; stack<char> s; void print(int a) { if(a<10) cout<<a; else cout<<(char)(a-10+'A'); } int main() { while(cin >> n >> m ) { if(n<0) cout<<"-",n=-n; int tmp, k = 0; while(n) { a[++k] = n % m; n /= m; } for(int i=k; i>=1; i--) { print(a[i]); } cout<<endl; } }