Problem:UVA 713 - Adding Reversed Numbers
#include<bits/stdc++.h>
using namespace std;
#include<string.h>
int main()
{
char ar[250],br[250];
long long cr[250];
long long n,i,l1,l2,j,ll1,ll2,sum,c,carry,k;
cin>>n;
for(i=1; i<=n; i++)
{
cin>>ar>>br;
ll1=strlen(ar);
ll2=strlen(br);
if(ll1>ll2)
{
l1=ll1;
l2=ll2;
}
else
{
l1=ll2;
l2=ll1;
}
carry=0;
c=0;
for(j=0; j<l2; j++)
{
sum=(ar[j]-'0')+(br[j]-'0')+carry;
cr[c]=sum%10;
carry=sum/10;
//cout<<"a1=="<<cr[c]<<endl;
c++;
}
if(ll1>ll2)
{
for(j=l2; j<l1; j++)
{
sum=(ar[j]-'0')+carry;
cr[c]=sum%10;
carry=sum/10;
//cout<<"a2=="<<cr[c]<<endl;
c++;
}
}
else
{
for(j=l2; j<l1; j++)
{
sum=(br[j]-'0')+carry;
cr[c]=sum%10;
carry=sum/10;
// cout<<"a3=="<<cr[c]<<endl;
c++;
}
}
while(carry)
{
cr[c]=carry%10;
carry/=10;
//cout<<"a4=="<<cr[c]<<" "<<carry<<endl;
c++;
}
for(k=0; k<c; k++)
{
if(cr[k]!=0)
break;
}
for(j=k; j<c; j++)
{
cout<<cr[j];
}
cout<<endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#include<string.h>
int main()
{
char ar[250],br[250];
long long cr[250];
long long n,i,l1,l2,j,ll1,ll2,sum,c,carry,k;
cin>>n;
for(i=1; i<=n; i++)
{
cin>>ar>>br;
ll1=strlen(ar);
ll2=strlen(br);
if(ll1>ll2)
{
l1=ll1;
l2=ll2;
}
else
{
l1=ll2;
l2=ll1;
}
carry=0;
c=0;
for(j=0; j<l2; j++)
{
sum=(ar[j]-'0')+(br[j]-'0')+carry;
cr[c]=sum%10;
carry=sum/10;
//cout<<"a1=="<<cr[c]<<endl;
c++;
}
if(ll1>ll2)
{
for(j=l2; j<l1; j++)
{
sum=(ar[j]-'0')+carry;
cr[c]=sum%10;
carry=sum/10;
//cout<<"a2=="<<cr[c]<<endl;
c++;
}
}
else
{
for(j=l2; j<l1; j++)
{
sum=(br[j]-'0')+carry;
cr[c]=sum%10;
carry=sum/10;
// cout<<"a3=="<<cr[c]<<endl;
c++;
}
}
while(carry)
{
cr[c]=carry%10;
carry/=10;
//cout<<"a4=="<<cr[c]<<" "<<carry<<endl;
c++;
}
for(k=0; k<c; k++)
{
if(cr[k]!=0)
break;
}
for(j=k; j<c; j++)
{
cout<<cr[j];
}
cout<<endl;
}
return 0;
}
No comments:
Post a Comment