ACM长数加法

coomax 2011-03-16 09:32:37
前几天,在这个论坛上发的一个帖子,给我提的意见,可以说毫无用处。我听说,CSDN上高手不少,我也相信,比我强的人很多,可是他们太让我失望了,我今天在检查这个程序又发现一个问题,我才明白,回答我上个帖子的人根本就没认真看,我希望,各位高手,给我点帮助,谢谢了,小弟我真的需要你们的帮助。

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.



Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.



Sample Input
2
1 2
112233445566778899 998877665544332211


Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110


#include<stdio.h>
#include<string.h>
#include<memory.h>
#define LEN 1001
main()
{
char arr1[LEN],arr2[LEN],arr3[LEN];
int i,j,n,l,l1,l2,s;
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%s%s",arr1,arr2);
printf("Case %d\n",j+1);
printf("%s + %s =",arr1,arr2);
l1=strlen(arr1);
l2=strlen(arr2);
memset(arr3,'0',LEN);
l=l1>l2?l1:l2;
s=l1<l2?l1:l2;
for(i=0;i<s;i++)
{
arr3[l-i]+=arr1[l1-i-1]+arr2[l2-i-1]-96;
if(arr3[l-i]>'9')
{
arr3[l-i]-=10;
arr3[l-i-1]+=1;
}
}
if(l1-i>0||l2-i>0)
{
if(l2>l1)
strcpy(arr1,arr2);
for(;i<l;i++)
{
arr3[l-i]+=arr1[l-i-1]-48;
if(arr3[l-i]>'9')
{
arr3[l-i]-=10;
arr3[l-i-1]+=1;
}
}
}
arr3[l+1]='\0';
if(arr3[0]=='0')
printf("%s\n",arr3+1);
else
printf("%s\n",arr3);
printf("\n\n");
}
}
...全文
192 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwypy 2011-03-18
  • 打赏
  • 举报
回复
不难,仔细想想就能够解决的。
coomax 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dreamhunter_lan 的回复:]
把arr3定义为int型数组,还改了其他一点,你自己看看能不能ac吧~~记得以前写大数加法比LZ这种写法简单些

C/C++ code

#include<stdio.h>
#include<string.h>

#define LEN 1001

int main(void)
{
char a1[LEN], a2[LEN];
int a3[LEN];
……
[/Quote]还是不行啊结果还是wrong answer 不过谢谢还是谢谢你
wesweeky 2011-03-17
  • 打赏
  • 举报
回复
数组解决
logiciel 2011-03-17
  • 打赏
  • 举报
回复
"只有在2个CASE之间有换行"改为"只有在2个CASE之间有空行".
logiciel 2011-03-17
  • 打赏
  • 举报
回复
LZ程序的算法没有问题,但没有注意输出格式的细节.以下修改AC:
#include<stdio.h>
#include<string.h>
#include<memory.h>
#define LEN 1001
main()
{
char arr1[LEN],arr2[LEN],arr3[LEN];
int i,j,n,l,l1,l2,s;
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%s%s",arr1,arr2);
if (j>0) printf("\n");//加:只有在2个CASE之间有换行
printf("Case %d:\n",j+1);//漏了冒号:漏了printf("Case %d\n",j+1);
printf("%s + %s = ",arr1,arr2); //=右边漏了空格:printf("%s + %s =",arr1,arr2);
l1=strlen(arr1);
l2=strlen(arr2);
memset(arr3,'0',LEN);
l=l1>l2?l1:l2;
s=l1<l2?l1:l2;
for(i=0;i<s;i++)
{
arr3[l-i]+=arr1[l1-i-1]+arr2[l2-i-1]-96;
if(arr3[l-i]>'9')
{
arr3[l-i]-=10;
arr3[l-i-1]+=1;
}
}
if(l1-i>0||l2-i>0)
{
if(l2>l1)
strcpy(arr1,arr2);
for(;i<l;i++)
{
arr3[l-i]+=arr1[l-i-1]-48;
if(arr3[l-i]>'9')
{
arr3[l-i]-=10;
arr3[l-i-1]+=1;
}
}
}
arr3[l+1]='\0';
if(arr3[0]=='0')
printf("%s\n",arr3+1);
else
printf("%s\n",arr3);
//只有在2个CASE之间有换行:printf("\n\n");
}
}


一曲肝肠断 2011-03-17
  • 打赏
  • 举报
回复
眼神不好,性子急,太长的看不了
quwei197874 2011-03-17
  • 打赏
  • 举报
回复
用字符数组可以解决.
赵4老师 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhao4zhong1 的回复:]
C/C++ code
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
vo……
[/Quote]
KISS(Keep It Simple and Stupid) (^_^)
赵4老师 2011-03-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
scanf("%d",&n);
for (j=0;j<n;j++) {
scanf("%s%s",a1,a2);

L=strlen(a1);
for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';

L=strlen(a2);
for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';

for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];

for (i=0;i<MAXLEN;i++) {
if (v3[i]>=10) {
v3[i+1]+=v3[i]/10;
v3[i]=v3[i]%10;
}
}

printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);

z=0;
for (i=MAXLEN-1;i>=0;i--) {
if (z==0) {
if (v3[i]!=0) {
printf("%d",v3[i]);
z=1;
}
} else {
printf("%d",v3[i]);
}
}
if (z==0) printf("0");

printf("\n");
}
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110
dreamhunter_lan 2011-03-16
  • 打赏
  • 举报
回复
把arr3定义为int型数组,还改了其他一点,你自己看看能不能ac吧~~记得以前写大数加法比LZ这种写法简单些

#include<stdio.h>
#include<string.h>

#define LEN 1001

int main(void)
{
char a1[LEN], a2[LEN];
int a3[LEN];
int i, j, n, l, l1, l2, s;
scanf("%d", &n);
for(j=0; j<n; j++)
{
scanf("%s %s", a1, a2);

l1 = strlen(a1);
l2 = strlen(a2);
memset(a3, 0, sizeof(a3));

l = l1>l2 ? l1 : l2;
s = l1<l2 ? l1 : l2;

for(i=0; i<s;i++)
{
a3[l-i] += a1[l1-i-1] + a2[l2-i-1]-96;
if(a3[l-i] > 9)
{
a3[l-i] -= 10;
a3[l-i-1] += 1;
}
}
// printf("%s\n", a3);
if(l1-i>0 || l2-i>0)
{
if(l2>l1)
strcpy(a1, a2);
for(; i<l; i++)
{
a3[l-i] += (a1[l-i-1] - 48);
if(a3[l-i]>9)
{
a3[l-i] -= 10;
a3[l-i-1] += 1;
}
}
}
printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);
for(i=0; a3[i]==0; i++)
;
for(; i<=l; i++)
printf("%d", a3[i]);
printf("\n");
}
return 0;
}

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧