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;
}
}
复制代码

posted on   getgoing  阅读(450)  评论(0编辑  收藏  举报

编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
< 2012年2月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 1 2 3
4 5 6 7 8 9 10

导航

统计

点击右上角即可分享
微信分享提示