HDU 2031 进制转换

来源:互联网 发布:打淘宝客服要话费吗 编辑:程序博客网 时间:2024/05/01 10:43

题目地址


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

思路:  进制转换就不多说了,利用了栈,输出就可以。 不必存储每一个数字的字符串。

错误: 忘记了考虑负数的情况wa了一次


#include <iostream>#include <cstdio>using namespace std; char index[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};int main(){int a,r; int que[30]; int top;  bool flag; while  ( ~scanf("%d%d",&a,&r) ) {flag = false;top = 0 ; if ( a<0 ) {flag = true;a = -a;  } if ( a==0 ) {cout<<"0"<<endl; continue; }while ( a ){que[top++] = a%r;a = a/r; }if ( flag ) cout<<"-"; while ( top ) {cout<<index[que[--top]];}cout<<endl; }  return 0;}




0 0
原创粉丝点击