View Code
View Problem
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 300
void reverse(char *fnum,char *snum)
{
int len,i;
len=strlen(fnum);
for(i=0;i<len;i++)
snum[i]=fnum[len-i-1];
snum[len]='\0';
}
void add(char *first,char *sec,char *result)
{
char F[MAX],S[MAX],R[MAX];
int flen,slen,now,extra,sum;
flen=strlen(first);
slen=strlen(sec);
reverse(first,F);
reverse(sec,S);
for(now=0,extra=0;(now<flen&&now<slen);now++)
{
sum=(F[now]-'0')+(S[now]-'0')+extra;
R[now]=sum%10+'0';
extra=sum/10;
}
for(;now<flen;now++)
{
sum=F[now]+extra-'0';
R[now]=sum%10+'0';
extra=sum/10;
}
for(;now<slen;now++)
{
sum=S[now]+extra-'0';
R[now]=sum%10+'0';
extra=sum/10;
}
if(extra!=0)
R[now++]=extra+'0';
R[now]='\0';
if(strlen(R)==0)
strcpy(R,"0");
reverse(R,result);
}
int main()
{
char f[MAX],s[MAX],r[MAX],f1[MAX],s1[MAX];
int i,len,t,flag;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",&f1,&s1);
reverse(f1,f);
reverse(s1,s);
add(f,s,r);
len=strlen(r);
flag=0;
for(i=len-1;i>=0;i--)
{
if((r[i]==48)&&(flag==0));
else
{printf("%c",r[i]);
flag=1;
}
}
printf("\n");
}
return 0;
}
View Problem
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 300
void reverse(char *fnum,char *snum)
{
int len,i;
len=strlen(fnum);
for(i=0;i<len;i++)
snum[i]=fnum[len-i-1];
snum[len]='\0';
}
void add(char *first,char *sec,char *result)
{
char F[MAX],S[MAX],R[MAX];
int flen,slen,now,extra,sum;
flen=strlen(first);
slen=strlen(sec);
reverse(first,F);
reverse(sec,S);
for(now=0,extra=0;(now<flen&&now<slen);now++)
{
sum=(F[now]-'0')+(S[now]-'0')+extra;
R[now]=sum%10+'0';
extra=sum/10;
}
for(;now<flen;now++)
{
sum=F[now]+extra-'0';
R[now]=sum%10+'0';
extra=sum/10;
}
for(;now<slen;now++)
{
sum=S[now]+extra-'0';
R[now]=sum%10+'0';
extra=sum/10;
}
if(extra!=0)
R[now++]=extra+'0';
R[now]='\0';
if(strlen(R)==0)
strcpy(R,"0");
reverse(R,result);
}
int main()
{
char f[MAX],s[MAX],r[MAX],f1[MAX],s1[MAX];
int i,len,t,flag;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",&f1,&s1);
reverse(f1,f);
reverse(s1,s);
add(f,s,r);
len=strlen(r);
flag=0;
for(i=len-1;i>=0;i--)
{
if((r[i]==48)&&(flag==0));
else
{printf("%c",r[i]);
flag=1;
}
}
printf("\n");
}
return 0;
}
No comments:
Post a Comment