hdu 2031 進制轉換(棧思想的使用)
進制轉換
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 63039 Accepted Submission(s): 34261
Input 輸入數據包含多個測試實例,每個測試實例包含兩個整數N(32位整數)和R(2<=R<=16, R<>10)。
Output 為每個測試實例輸出轉換後的數,每個輸出占一行。如果R大於10,則對應的數字規則參考16進制(比如,10用A表示,等等)。
Sample Output 111 1B -11
Author lcy
Source C語言程序設計練習(五)
Recommend lcy | We have carefully selected several similar problems for you: 2035 2043 2036 2048 2049 ac代碼: (棧思想,先進後出)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
stack <int> q;
int main()
{
int n,d;
while (cin>>n>>d){
while (!q.empty())
q.pop();
if (n<0){
cout<<"-";
n=-n;
}
else if (!n){
cout<<‘0‘<<endl;
continue;
}
while (n){
q.push(n%d);
n/=d;
}
while (!q.empty()){
int x=q.top();
if (x>=10)
printf("%c",‘A‘+x-10); //cout<<‘A‘+x-10<<endl; 這個地方有坑, 這裏 cout 輸出的不是字符,而是整數.
else
cout<<x;
q.pop();
}
cout<<endl;
}
return 0;
}
hdu 2031 進制轉換(棧思想的使用)