题解 | #进制转换#

进制转换

https://www.nowcoder.com/practice/0337e32b1e5543a19fa380e36d9343d7

#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
using namespace std;
int main(){
	string str;
	while (cin >> str){
		vector<int>ans;
		int k=0;
		if (str == "0"){
			printf("0\n");
			continue;
		}
		while (!str.empty()){
			for (int i = 0; i < str.size(); i++){
				int t = k;
				k = (str[i] - '0' + k * 10) % 2;
				str[i] = (str[i] - '0' + t * 10) / 2 + '0';
			}
			if (str[0] == '0'){
				str.erase(0,1);
			}
			ans.push_back(k);
			k = 0;
		}
		for (int i = ans.size() - 1; i >= 0; i--){
			printf("%d", ans[i]);
		}
		printf("\n");
	}
}

全部评论

相关推荐

就用这个吧:支持多益再加一个空气使用费
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务