Cod sursa(job #394975)

Utilizator test9cTester test9c Data 11 februarie 2010 21:08:16
Problema A+B Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <string.h>
#include <stdio.h>
int v[12],i,j,x,y,s,aux,k;
char a[11],b[11];
int main()
{
	freopen ("adunare.in","r",stdin);
	freopen("adunare.out","w",stdout);
	scanf("%s",&a);
	scanf("%s",&b);
	
	k=strlen(a);
	j=strlen(b);
	
	i=0;
	for (x=k-1,y=j-1;x>=0 && y>=0;x--,y--)//Atentie! x>=0 && y>=0
	{	s=(a[x]-'0')+(b[y]-'0')+aux;
		if (s>10)
		{	v[i]=s%10;
			aux=1;
		}
		if (s<10)
		{	v[i]=s;
			aux=0;
		}
		if (s==10)
		{	v[i]=0;
			aux=1;
		}
	i++;
	}
	for(;x>=0;x--)//este necesar in situatii de tipul a=89988 si b=22
	{
		s=a[x]-'0'+aux;
		if (s>10)
		{	v[i]=s%10;
			aux=1;
		}
		if (s<10)
		{	v[i]=s;
			aux=0;
		}
		if (s==10)
		{	v[i]=0;
			aux=1;
		}i++;
	}
	for(;y>=0;y--)//este necesar in situatie de tipul a=22 si b=89988
	{
		s=b[y]-'0'+aux;
		if (s>10)
		{	v[i]=s%10;
			aux=1;
		}
		if (s<10)
		{	v[i]=s;
			aux=0;
		}
		if (s==10)
		{	v[i]=0;
			aux=1;
		}i++;
	}
	
	if(aux!=0){v[i]=aux;i++;}// este necesar in situatii de tipul a=22 si b=99988 sau a=99988 si b=22
	for(j=i-1;j>=0;j--)
		printf("%d",v[j]);
	return 0;
}