Problem D: 使用栈实现进制转换
Time Limit: 1 Sec
Memory Limit: 128 MB
Submit: 12
Solved: 9
[
Submit][
Status][
Web Board]
Problem D: 使用栈实现进制转换
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 12 Solved: 9
[ Submit][ Status][ Web Board]
Description
使用栈将一个很长(>30)的十进制数转换为二进制数
Input
若干个很长的十进制数
每行一个
Output
转换为二进制,每行输出一个
Sample Input
123456789012345678901234567890
753951684269875454652589568545854758545824
Sample Output
1100011101110100100001111111101101100001101110011111000001110111001001110001111110000101011010010
10001010011110101010001101001000100100100000101001011010001010101001000100111101011001110001000111110010001000101101111110110110100110100000
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char s[10010];
int a[10010];
int b[10010];
int main()
{
int stl,i,start,flag,flag1,num,n;
while(cin>>s)
{
num=start=0;
stl=strlen(s);
for(i=0;i<stl;i++)
{
a[i]=s[i]-'0';
// cout<<a[i];
}
// cout<<endl;
while(stl-start!=1)
{
// cout<<"start="<<start<<"and a[start]="<<a[start]<<endl;
flag=a[start]%2;
a[start]/=2;
for(i=start+1;i<stl-1;i++)
{
flag1=flag*10+a[i];
flag=flag1%2;
a[i]=flag1/2;
}
flag1=flag*10+a[stl-1];
a[stl-1]=flag1/2;
b[num++]=flag1%2;
if(a[start]==0)
{
start++;
}
/* for(i=start;i<stl;i++)
{
cout<<a[i];
}
cout<<endl;*/
}
n=a[start];
while(n>0)
{
b[num++]=n%2;
n/=2;
}
for(i=num-1;i>=0;i--)
{
cout<<b[i];
}
cout<<endl;
}
}