Problem Type : Number Theory
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
char ch[100000],des[100000],add[10000];
int i,j,k,len1,len2,carry,sum,n,m;
scanf("%s",ch);
len1 = strlen(ch);
reverse(ch,(ch+len1));
while(true)
{
scanf("%s",des);
if(strcmp(des,"0")==0)
break;
len2 = strlen(des);
len1 = strlen(ch);
reverse (des,des+len2);
// printf("%d",len1)
if(len1<len2)
{
for(i=len1; i<len2; i++)
{
ch[i]='0';
}
ch[len2]='\0';
len1=len2;
}
else if(len2<len1)
{
for(i=len2; i<len1; i++)
{
des[i]='0';
}
des[len1]='\0';
len2=len1;
}
// printf("%s\n",ch);
//printf("%s\n",des);
carry = 0;
sum=0;
for(i=0; i<len1; i++)
{
n= ch[i]-48;
m= des[i]-48;
sum = n+m+carry;
if(sum>9)
{
sum = sum%10;
carry = 1;
}
else
{
carry=0;
}
add[i]= sum+48;
}
if(carry==1)
{
add[len1]='1';
len1++;
}
add[len1]='\0';
memset(des,'\0',sizeof(des));
memset(ch,'\0',sizeof(ch));
strcpy(ch,add);
memset(add,'\0',sizeof(add));
}
len1 = strlen(ch);
reverse (ch,ch+len1);
printf("%s\n",ch);
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
char ch[100000],des[100000],add[10000];
int i,j,k,len1,len2,carry,sum,n,m;
scanf("%s",ch);
len1 = strlen(ch);
reverse(ch,(ch+len1));
while(true)
{
scanf("%s",des);
if(strcmp(des,"0")==0)
break;
len2 = strlen(des);
len1 = strlen(ch);
reverse (des,des+len2);
// printf("%d",len1)
if(len1<len2)
{
for(i=len1; i<len2; i++)
{
ch[i]='0';
}
ch[len2]='\0';
len1=len2;
}
else if(len2<len1)
{
for(i=len2; i<len1; i++)
{
des[i]='0';
}
des[len1]='\0';
len2=len1;
}
// printf("%s\n",ch);
//printf("%s\n",des);
carry = 0;
sum=0;
for(i=0; i<len1; i++)
{
n= ch[i]-48;
m= des[i]-48;
sum = n+m+carry;
if(sum>9)
{
sum = sum%10;
carry = 1;
}
else
{
carry=0;
}
add[i]= sum+48;
}
if(carry==1)
{
add[len1]='1';
len1++;
}
add[len1]='\0';
memset(des,'\0',sizeof(des));
memset(ch,'\0',sizeof(ch));
strcpy(ch,add);
memset(add,'\0',sizeof(add));
}
len1 = strlen(ch);
reverse (ch,ch+len1);
printf("%s\n",ch);
}
No comments:
Post a Comment