csu1002 A+B(III)

题目描述:

There are multiple test cases. Each test case contains only one line. Each line consists of a pair of integers a and b1=< 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\n", 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;
}
}

转载于:https://www.cnblogs.com/JMDWQ/archive/2012/02/16/2354495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值