#include <iostream> #include <algorithm> #include <string> #include <stack> #include <vector> #include <set> using namespace std; stack<int> han(string a,string b ){ stack<int> temp; int k=0,p=a.length()-1,q=b.length()-1;//p,q是a,b的下标,k为借位 for(;p>=0&&q>=0;p--,q--){ if(a[p]-k>=b[q]){//a>=b int c=a[p]-b[q]-k; temp.push(c); k=0; }else{//a<b int c=a[p]-b[q]-k+10; temp.push(c); k=1; } } for(;p>=0;p--){//a不为0的时候 int c=a[p]-'0'-k; if(c<0){ k=1; c+=10; }else k=0; temp.push(c); } return temp; } int main() { int t; cin>>t; for(int i=1;i<=t;i++){ string a,b; cin>>a; cin>>b; int flag=0;//正负 if(a.length()<b.length()) flag=1; else if(a.length()==b.length()) flag=a>=b?0:1; stack<int> temp; int count=0; cout<<"Case #"<<i<<":"<<endl; if(flag){ cout<<"-"; temp=han(b,a); } else temp=han(a,b); while(!temp.empty()){ int c=temp.top(); if(c!=0){ count=1; cout<<temp.top(); } else if(c==0&&count==1) cout<<temp.top(); temp.pop(); } if(count==0) cout<<0; cout<<endl; } return 0; } |
Double click to view unformatted code.