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
唉, 编程能力还是太弱了, 看见这个题目时没考虑周全, 比如 n<=0 一开始没考虑 (样例匆匆瞥了一眼).
- #include<iostream>
- #include<iomanip>
- //#include<bits/stdc++.h>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<sstream>
- #define PI 3.14159265358979
- #define LL long long
- #define eps 0.00000001
- #define LL long long
- using namespace std;
- int main()
- {
- LL n,m;int ans[1001];
- while(cin>>n>>m)
- {
- bool r=true;int Y=n;
- if(n<0) {r=0;n=-n;}
- if(n==0) {cout<<0<<endl;continue;}
- int k=1;
- while(n)
- {
- ans[k++]=n%m;
- n/=m;
- }
- if(r==0) cout<<'-';
- for(int i=k-1;i>=1;--i)
- {
- if(m>=10&&ans[i]>=10) cout<<char(55+ans[i]);
- else cout<<ans[i];
- }
- cout<<endl;
- }
- }
- View Code
进制转换
来源: http://www.bubuko.com/infodetail-3043690.html