10 进制 VS 2 进制

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
 
typedef long long LL;
const int N=1e5+10,mod=1e6;
const int MAX = 1e7 + 10 ;



char ch1[10010] , ch2[100101] ;

int num[1000] ;

void ten_to_two(){
	int len = strlen(ch1) ;
	int shang_sum = 1 ;
	int flag = 0 ;
	int a , b ;
	while(shang_sum){
		b = 0 ;
		shang_sum = 0 ;
		ch2[flag ++ ] = (ch1[len-1] - '0') % 2 + '0' ;
		for(int i = 0 ; i < len; i++){
			if(b == 0){
				a = (ch1[i] - '0') ;
				ch1[i] = a/2 + '0' ;
			}else {
				a = ch1[i] - '0' + 10 ;
				ch1[i] = a/2 + '0' ;
			}
			if(a % 2 == 0) {
				b = 0 ;
			}else {
				b = 1 ;
				
				
			}
			shang_sum += a / 2 ;
		}
	}
}

int two_to_ten(){
	int len2  = 1 ;
	num[0] = 0 ;
	for(int i = 0 ; i < strlen(ch2) ; i++){
		for(int j = 0 ; j < len2 ; j ++){
			if(j == 0){
				num[j] = num[j]*2 + ch2[i] - '0' ;
			}else {
				num[j] = num[j]*2 ;
			}
		}
		for(int j = 0 ; j < len2 ; j++){
			if(j == len2 - 1 && num[len2-1] >= 10){
				num[j] = num[j] %10 ;
				num[++j] = 1;
				len2++ ;
			}else  if(num[j] >= 10){
				num[j] = num[j]%10 ;
				num[j+1] ++ ;
			}
		}
	}
	return len2 ;
}

int main(){
	while(cin >> ch1){
		memset(ch2,0,sizeof(ch2)) ;
		ten_to_two() ;
		int len2 = two_to_ten() ;
		for(int i = len2 - 1; i >=0 ; i--){
			cout << num[i] ;
		}
		cout <<endl;
	}
}

看了这位大佬的分析:

https://blog.csdn.net/j754379117/article/details/40631899

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值