View Code of Problem 3689

#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "algorithm"
#include <queue>

using namespace std;


 void Transform(char s[],int a[])
{
int i,n;
n=strlen(s);
a[0]=n;
for(i=1;i<=n;i++)
a[i]=s[n-i]-'0';
}
int Big(int a[],int b[])
{
int i;
if(a[0]-b[0])
return a[0]-b[0];
for(i=a[0];i>0;i--)
if(a[i]-b[i])
return a[i]-b[i];
return 0;
}
void Subtract(int a[],int b[],int ans[])
{
int i,k=0;
ans[0]=a[0];
for(i=1;i<=ans[0];i++)
{
ans[i]=a[i]-b[i]-k;
if(ans[i]<0)
{
ans[i]+=10;
k=1;
}
else
k=0;
}
while(!ans[ans[0]])
ans[0]--;
}
void print(int a[])
{
int i;
for(i=a[0];i>0;i--)
printf("%d",a[i]);
putchar('\n');
}
int main()
{
int i,t=1,T,a[105],b[105],ans[105];
char s1[105],s2[105];
//freopen("t","r",stdin);
scanf("%d",&T);

while(T--)
{
    scanf("%s",s1);
    scanf("%s",s2);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(ans,0,sizeof(ans));
Transform(s1,a);
Transform(s2,b);
printf("Case #%d:\n",t++);
if(Big(a,b)==0)
{
printf("0\n");
continue;
}
if(Big(a,b)>0)
Subtract(a,b,ans);
else
{
printf("-");
Subtract(b,a,ans);
}
print(ans);
}


return 0;
}

Double click to view unformatted code.


Back to problem 3689