文章

34

粉丝

9

获赞

5

访问

8.8k

头像
进制转换 题解:模板+二进制+字符串的处理
P1178 北京大学上机题
发布于2024年7月6日 21:34
阅读数 272

#include <iostream>
#include <string.h>
#include <algorithm>
#include <string>

using namespace std;

string convert(string s,int m,int b){
	string ans = "";
	for(int i = 0;i < s.size();){
		int k = 0;
		for(int j = i;j < s.size();j ++){
			int t = (k * m + s[j] - '0') % b;
			s[j] = (k * m + s[j] - '0') / b + '0';
			k = t;
		}
		ans += (k + '0');
		while(s[i] == '0')i ++;
	}
	return ans;
}


int main(){
	string s,ans;
	
	while(cin >> s){
		ans = convert(s,10,2);
		
		reverse(ans.begin(),ans.end());
		
		cout << ans << endl;
	}
	
	
	
	
	
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发