[High Accuracy] B band Planet

[High Accuracy] B band Planet

topic

Topic background

Ary title, but also a calculator ~ ~

Title Description

Saying that one day, a small Z spaceship, fly to a beautiful planet. For historical reasons, technology is not very well developed on this beautiful planet, the planet widespread use of B (2 <= B <= 36) binary counter. People on the planet with the delicious food served small Z, in return, want to send a small Z B can complete binary adder calculator to them. Z is now little hope you can help him, prepared to achieve binary adder B program.

Input Format

3 lines Line 1: a decimal integer, indicates hexadecimal B. Lines 2-3: each line a B decimal positive integer. Every number belongs to {0,1,2,3,4,5,6,7,8,9, A, B ......}, each digit length <= 2000.

Output Format

A binary number B, and represents the two input numbers.

Sample input and output

Input # 1 replication
. 4
123
321
the output copy # 1
1110
Description / prompt
hexadecimal calculator

analysis

This problem is also highly accurate title.
With the first read data string, (because there are data numbers and letters), then converted into the corresponding number stored in the array (here, [with] a, b [])
and the addition done with high accuracy.
Several points ** Note: ** It is also noted that high-precision adder daily
1 if n is greater than the highest bit decimal representation can then carry forward
2 to prevent the most significant bit is 0
3 output, is noted from the back and also greater than 10 letters into corresponding

Specifically look at the code. . . . . .

Code

#include<iostream>

using namespace std;

const int maxn = 20000;
int n;
string x,y;
int a[maxn],b[maxn],c[maxn];

int main(){
	cin>>n;
	cin>>x>>y;
	//读数据,存放 
	int lena = x.size();
	int lenb = y.size();
	for(int i=0;i<lena;i++){
		if(x[i] >='0' &&x[i] <='9')		a[lena -i] = x[i] -'0';
		else if(x[i] >='A' && n>=10) a[lena-i] = x[i] -'A'+10; 
	}
	for(int i=0;i<lenb;i++){
		if(y[i] >='0' &&y[i] <='9')		b[lenb -i] = y[i] -'0';
		else if(y[i] >='A' && n>=10) b[lenb-i] = y[i] -'A'+10; 
	}
//ps:下面换成这样写也可以 
//int lenc=0;
//	int cf=0;
//	while(lenc <=lena || lenc <=lenb){
//		lenc++;
//		c[lenc] = a[lenc] +b[lenc]+cf;
//		cf =c[lenc]/n;
//		c[lenc] %= n;
//	}
//	while(c[lenc]==0 && lenc >1){
//		lenc--;
//	}
//	for(int i=lenc;i>=1;i--){
//		if(c[i]>=10){
//			cout<<(char)(c[i] +'A' -10);
//		}else{
//			cout<<c[i];
//		}
//	} 	

//取最长长度,做高精加法 
	int maxlen = max(lena,lenb); 
	for(int i=1;i<=maxlen;i++){
		c[i] += a[i] + b[i];
		c[i+1] = c[i]/n;
		c[i] %= n;
	}
	maxlen++;
	//如果最高位大于n进制,表示可以向前再进位 
	while(c[maxlen] >=n){
		c[maxlen+1] = c[maxlen]/n;
		c[maxlen] %= n;
		maxlen++; 
	}
	//以防 最高位是 0的情况 
	while(c[maxlen]==0 && maxlen >1){
		maxlen--;
	}
	//输出,注意是从后往前,然后大于10还要转换成对应字母 
	for(int i=maxlen;i>=1;i--){
		if(c[i]>=10){
			cout<<(char)(c[i] +'A' -10);
		}else{
			cout<<c[i];
		}
	} 
	
	return 0;
}
Published 75 original articles · won praise 1 · views 3632

Guess you like

Origin blog.csdn.net/A793488316/article/details/104832689