为什么0的样例不对,还是100
查看原帖
为什么0的样例不对,还是100
1023161
White_Night_楼主2023/12/1 13:31

代码:

#include <bits/stdc++.h>
using namespace std;
const int N=110;
string ret[N];
string get(long long x){
	if(x == 0){
		return 0; 
	}
	string s="";
	while(x>0){
		int yu=x%2;
		s = char(yu+'0') + s;
		x /= 2;
	}
	return s;
}
string bu(string s){
	int len = s.size();
	if(len % 7 == 0){
		return s;
	}
	else{
		for(int i=0;i<7-(len%7);i++){
			s = '0' + s;
		}
	}
	return s;
}
string zhuan(string s){
	long long r = 0;
	int len = s.size();
	for(int i=len-1;i>=0;i--){
		int x = s[i] - '0';
		r = r + pow(2,len-i-1) * x;
	}
	string tmp="";
	while(r > 0){
		int yu = r % 16;
		if(yu < 10){
			tmp = char(yu+'0') + tmp;
		}
		else{
			tmp = char(yu-10+'A') + tmp;
		}
		r/=16;
	}
	if(tmp == ""){
		return "00";
	}
	if(tmp.size() < 2){
		tmp = '0' + tmp;
	}
	return tmp;
}
int main(){
	long long n;
	cin >> n;
	string s = get(n);
	s = bu(s);
	int len = s.size();
	int cnt=0;
	for(int i=0;i<len;i+=7){
		string tmp = s.substr(i,7);
		if(i == 0){
			tmp = '0' + tmp;
		}
		else{
			tmp = '1' + tmp;
		}
		ret[cnt] = zhuan(tmp);
		cnt++;
	}
	for(int i=cnt-1;i>=0;i--){
		cout << ret[i] << " ";
	}
	return 0;
}

0的输出:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
2023/12/1 13:31
加载中...