本文主要是介绍3989. I guess the gift is a calculator! toj,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写的好搓 先放着
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
string jianfa(string a,string b,char f)
{if(a.compare(b)==0){ string c="0";return c;}string c="",ans="",t;int flag=0,k=0;//flag退位的标志int flag2;//结果正负的标志if(f=='+')flag2=0;elseflag2=1;if(a.length()<b.length()||(a.length()==b.length()&& a.compare(b)<0)){t=a;a=b;b=t;flag2^=1;}int i=a.length()-1,j=b.length()-1;while(i>=0&&j>=0){if(a[i]+flag>b[j]){c+=a[i]+flag-b[j]+'0';flag=0;}else if (a[i]+flag==b[j]){c+='0';flag=0;}else{c+=(a[i]-'0')+flag+10-(b[j]-'0')+'0';flag=-1;}i--;j--;k++;}int len=k-1;while(c[len]=='0'&&len>0)len--;//检索高位for(j=0;j<=len;j++)ans+=c[j];if(flag2){ ans+='-';}//结果正负判断char tt;for(i=0,j=ans.length()-1;i<j;i++,j--)//倒置{tt=ans[i];ans[i]=ans[j];ans[j]=tt;}return ans;
}int main()
{string a,b;while(cin>>a>>b&&(a[0]!='0'||b[0]!='0')){string s1(a.begin()+1,a.end());string s2(b.begin()+1,b.end());if(a[0]==b[0]){cout<<jianfa(s1,s2,a[0])<<endl;}else{int num1[150],num2[150];int ans[150];memset(ans,0,sizeof(ans));memset(num1,0,sizeof(num1));memset(num2,0,sizeof(num2));int l1=s1.size(),l2=s2.size();int i,j;for(i=l1-1,j=0;i>=0;i--,j++)num1[j]=s1[i]-'0';for(i=l2-1,j=0;i>=0;i--,j++)num2[j]=s2[i]-'0';int max=l1>l2?l1:l2;for(i=0;i<max;i++){ans[i]+=num1[i]+num2[i];if(ans[i]>9){ans[i+1]+=ans[i]/10;ans[i]=ans[i]%10;}}if(b[0]=='+')cout<<"-";int flag=1;for(i=max+1;i>=0;i--)if(flag&&ans[i]==0)continue;else{flag=0;cout<<ans[i];}cout<<endl;} }return 0;
}
这篇关于3989. I guess the gift is a calculator! toj的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!