北京邮电大学机试 二进制转换

基本思想:

进制转换;

关键点:

坑壁牛客网,第一行数据不读入;

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

string charge(int x) {
	string s="";
	if (x == 0) {
		return "0";
	}
	while (x != 0) {
		s += x % 2+'0';
		x -= x % 2;
		x /= 2;
	}
	reverse(s.begin(), s.end());
	return s;
}

int main() {
	int n;
	int a;
	while (cin>>n){
		for (int i = 0; i < n; i++) {
			cin >> a;
			printf("%s\n",charge(a).c_str());
		}
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12405967.html