【数据结构】十进制转化为二进制

描述

进制之间的转换是很常见的操作

输入

一个正整数

输出

对应的二进制

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值