Tarsus summary

Read, read, write protect yourself mess, packaged with two functions.

addition:

vector<int> add(vector<int>&A,vector<int>&B)//加法 
{
    vector<int> X;
    int t=0;
    for(int i=0;i<A.size()|| i<B.size();i++)
    {
        if(i<A.size()) t+=A[i];
        if(i<B.size()) t+=B[i];
        X.push_back(t % 10);
        t /= 10;
    }
    if(t!=0) X.push_back(1);
    return X;
}

Subtraction:

Because of the possibility of positive and negative, but also to determine what size

bool big(vector<int>&A,vector<int>&B){
	if(A.size()!=B.size())	return A.size()>B.size();
	for(int i=A.size()-1;i>=0;i--){
		if(A[i]>B[i]){
			return true;
			break;
		}else if(A[i]<B[i]){
			return false;
			break;
		}
	}
	return true;
}

Large decreases:

vector<int> sub(vector<int>&A,vector<int>&B)//减法 
{
    vector<int> X;
    int t=0;
    for(int i=0;i<A.size();i++)
    {
        t=A[i]-t;
        if(i<B.size()) t-=B[i];
        X.push_back((t+10)%10);
        if(t<0) t=1;//如果t<0,退1加10. 
        else t=0;
    }
    while(X.size()>1 && X.back()==0) X.pop_back();
    return X;
}
Published 34 original articles · won praise 6 · views 1329

Guess you like

Origin blog.csdn.net/qq_44669377/article/details/105214361