Kai’blog

技术博客

【模拟】高精度练习之减法

原题传送门

思路


简单的高精度减法,无需解释~~~

Code


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
using namespace std;

string s1,s2;
int A[501],B[501],C[501],i; 

int main()
{
    cin>>s1>>s2;
    if(s1.length()<s2.length()||s1.length()==s2.length()&&s1<s2) swap(s1,s2),cout<<'-';
	for(i=1;i<=s1.length();i++)
    	A[s1.length()-i+1]=s1[i-1]-'0';
    for(i=1;i<=s2.length();i++)
    	B[s2.length()-i+1]=s2[i-1]-'0';
    int len=max(s1.length(),s2.length());
	for(i=1;i<=len;i++)
	{
		C[i+1]=(A[i]-B[i]+C[i])>=0?0:-1;
		C[i]=(A[i]-B[i]+C[i]+(C[i+1]==0?0:10))%10;
	}
	for(i=len;i>=1;i--,len--)
		if(C[i]!=0)break;
	for(i=len;i>=1;i--)
		cout<<C[i]; 
    return 0;
}

posted @ 2019-08-03 20:17  Kai02  阅读(111)  评论(0编辑  收藏  举报
Copyright © 2019-2020 拱大垲. All rights reserved.