描述
进制之间的转换是很常见的操作
输入
一个正整数
输出
对应的二进制
#include<iostream>
using namespace std;
int main()
{
int n,i,j=0;
int a[1000];
cin>>n;
i=n;
while(i)
{
a[j]=i%2;
i/=2;
j++;
}
for(i=j-1;i>=0;i--)
cout<<a[i];
cout<<endl;
}
#include<iostream>
using namespace std;
const int Size = 50;
class SeqStack
{
private:
int data[Size]; //数据存储数组
int top;
public:
SeqStack (){top=-1;}
~SeqStack (){}
void Push(int x); //入栈操作
void Pop(); //出栈操作
};
void SeqStack ::Push(int x)
{
top=-1;
int y;
int m=2;
if(top==Size-1) throw "上溢";
while(x!=0)
{
y=x%m;
data[++top]=y;
x=x/m;
};
}
void SeqStack ::Pop()
{
if (top == -1) throw "下溢";
while(top!=-1)
{
int x=data[top--];
cout<<x;
}
}
int main()
{
int i=1;
int number;
SeqStack a;
cin>>number;
a.Push(number);
a.Pop();
cout<<endl;
}