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 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\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;
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理