编程填空:二进制输出

版权声明:欢迎转载,但转载时请注明原文地址 https://blog.csdn.net/weixin_42110638/article/details/83031068

编程填空:二进制输出

总时间限制: 

1000ms

内存限制: 

65536kB

// 在此处补充你的代码

描述

给出一个int表示范围内的正整数x,输出其二进制表示。一共要输出31位,不足处要补0。

#include <iostream>
#include <string>
using namespace std;
string dec2bin(int x){
}
int main(){
	int n;
	cin >> n;
	while(n--) {
		int x;
		cin >> x;
		cout << dec2bin(x) << endl;
	}
	return 0;
}

输入

第一行是整数n(n<15),表示有n个正整数要处理
第二行是n个正整数

输出

对每个给出的正整数,输出其二进制表示。不足31位则用0补齐到31位

样例输入

3
1 2 3

样例输出

0000000000000000000000000000001
0000000000000000000000000000010
0000000000000000000000000000011
string dec2bin(int x)
{
    string s="";
    string s1;
    while(x)
    {
        if(x%2)
            s1="1";
        else
            s1="0";
        x/=2;
        s=s1+s;
    }
    for(int i=s.size(); i<31; i++)
        s="0"+s;
    return s;
}

进制转换专题

猜你喜欢

转载自blog.csdn.net/weixin_42110638/article/details/83031068