Problem A: 使用棧實現進制轉換
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 62 Solved: 37
[Submit][Status][Web Board]
Description
使用棧將一個很長(>30)的十進制數轉換爲二進制數
Input
若干個很長的十進制數
每行一個
Output
轉換爲二進制,每行輸出一個
Sample Input
123456789012345678901234567890
753951684269875454652589568545854758545824
Sample Output
1100011101110100100001111111101101100001101110011111000001110111001001110001111110000101011010010
10001010011110101010001101001000100100100000101001011010001010101001000100111101011001110001000111110010001000101101111110110110100110100000
HINT
#include<cstdio>
#include<iostream>
#include<cstring>
const int maxn=1000;
using namespace std;
int t[maxn],A[maxn];
char str1[maxn],str2[maxn];
int n,m;
void solve(char *str1,char *str2,int n,int m)
{
int i,len,k;
len=strlen(str1);
for(i=len;i>=0;i--)
{
t[len-1-i]=str1[i]-(str1[i]<58?48:str1[i]<97?55:61);
}
for(k=0;len;)
{
for(i=len;i>=1;i--)
{
t[i-1]+=t[i]%m*n;
t[i]/=m;
}
A[k++]=t[0]%m;
t[0]/=m;
while(len>0&&!t[len-1])
{
len--;
}
}
str2[k]=NULL;
for(i=0;i<k;i++)
{
str2[k-1-i]=A[i]+(A[i]<10?48:A[i]<36?55:61);
}
}
int main()
{
while(cin>>str1)
{
solve(str1,str2,10,2);
cout<<str2<<endl;
}
}