csu1002 A+B(III)

5510 단어 su
제목 설명:
There are multiple test cases. Each test case contains only one line. Each line consists of a pair of integers a and b(1=< a,b <=1016), separated by a space. Input is followed by a single line with a = 0, b = 0, which should not be processed.
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
RE: 세그먼트가 크지 않음
어떤 코드는 114B (나의 초1000B) 밖에 없는 것을 보고 이해하지 못했다.
// csuoj 1002 : A+B(III)
# include <stdio.h>
# include <string.h>
# define MAXN 20 // RE: # define MAXN 17
char a[MAXN], b[MAXN], ans[MAXN];
void add(char *a, char *b, char *ans); //ans=a+b
void strnrev(char *a);
int main()
{
while(2==scanf("%s%s", a,b))
{
if(1==strlen(a) && 1==strlen(b) && a[0]=='0' && b[0]=='0') return ;
strnrev(a);
strnrev(b);
add(a, b, ans);
strnrev(ans);
printf("%s
", ans);
}
return 0;
}
void add(char *a, char *b, char *ans)
{
char *p, *q;
int i, j, tmp, c;
if(strlen(a)>strlen(b)) {p=a;q=b;}
else {p=b;q=a;}
i = c = 0;
while (q[i] != '\0')
{
tmp = (q[i]-'0')+(p[i]-'0')+c;
ans[i] = tmp%10+'0';
c = tmp/10;
++i;
}
while (p[i] != '\0')
{
tmp = (p[i]-'0')+c;
ans[i] = tmp%10+'0';
c = tmp/10;
++i;
}
if(c==1) ans[i++] = c+'0';
ans[i] = '\0';
}
void strnrev(char *a)
{
int i, len;
char tmp;
len = strlen(a);
for (i = 0;i < len>>1; ++i)
{
tmp = a[i];
a[i] = a[len-i-1];
a[len-i-1] = tmp;
}
}

좋은 웹페이지 즐겨찾기