// 十进制转二进制.cpp : Defines the entry point for the console application.
//
//题目说明:将输入的十进制数,转换成二进制数,并打印出来
#include "stdafx.h"
#include<Stack>
#include<iostream>
#include<string>
using namespace std;
void DaelWith(int n);
int _tmain(int argc, _TCHAR* argv[])
{
int Num=0;
while(cin>>Num)
{
DaelWith(Num);
}
return 0;
}
void DaelWith(int n)
{
stack<int> Count;
while(n!=0)
{
int a=n%2;
if(a==0) Count.push(0);
else Count.push(1); //用栈的方式来存储,方便打印
n=n/2;
}
int Length=Count.size();
for(int i=0;i<Length;i++)
{
cout<<Count.top();
Count.pop();
}
cout<<endl;
}
十进制打印成二进制
最新推荐文章于 2022-04-05 21:55:22 发布