大数相减

来源:互联网 发布:如何做免费网络推广 编辑:程序博客网 时间:2024/05/16 05:12
#include<stdio.h>#include <iostream>#include <algorithm>#include <string>using namespace std;int main(){string a,b;int n;cin>>n;while(n--){cin>>a>>b;int jh=0;if(a.size()<b.size()){swap(a,b);jh=1;}else if(a.size()==b.size())        if(a<b)        {        swap(a,b);        jh=1;}       int f=0;        int c[1005],k=0;  for(int i=a.size()-1,j=b.size()-1;i>=0||j>=0;j--,i--)  {   char kk;    if(j<0)    kk='0';    else kk=b[j];    if(a[i]-'0'+f<kk-'0')    {            c[k++]=a[i]+f+10-kk;    f=-1;        }    else {    c[k++]=a[i]+f-kk;    f=0;}  }  int flog=0;  if(jh==1)  cout<<'-';  while(k--)  {  if(c[k]>0)   flog=1;   if(flog==0&&c[k]==0)   continue;  cout<<c[k];  }  if(flog==0)  cout<<'0';   cout<<endl; }}

原创粉丝点击