杭电ACM1002初级题 求助

sunyuke 2009-02-24 06:24:42
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22627 Accepted Submission(s): 4035


Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.



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
...全文
868 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sk_orc 2009-04-25
  • 打赏
  • 举报
回复
垃圾,递交上去绝对错误,题目都没看清楚!
执假以为真 2009-02-25
  • 打赏
  • 举报
回复

char c[1000];
memset(c, 1000, 0);

改成
char c[1000] = {0};

试试看, 我在g++下也通过了。
执假以为真 2009-02-25
  • 打赏
  • 举报
回复
我用的是DEV-CPP,用VS2008的话不清楚,但应该很好改吧
sunyuke 2009-02-25
  • 打赏
  • 举报
回复
我自己刚写的 大家评点一下



#include <iostream>
using namespace std;
void add(char *large,char *small,const int llenght,const int slenght)
{
char *answer = large;
char *pLarge = &large[llenght];
char *pSmall = &small[slenght];
int count = llenght;
while(pSmall != small)
{
answer[count] = *pLarge + *pSmall;
pLarge--;
pSmall--;
count--;
}
answer[count] =*pLarge + *pSmall;
for(count = llenght;count > 0;count--)
{
if(answer[count] > 9)
{
answer[count] -= 10;
answer[count-1] += 1;
}
}
count = 0;
while(count < llenght)
cout<<(int)answer[count++];
}
int main()
{
int times;
cin>>times;
char numA[1002];
char numB[1002];
for(int i = 1;i <= times;i++)
{
cin>>numA;
cin>>numB;
cout<<"Case "<<i<<':'<<endl;
cout<<numA<<' '<<'+'<<' '<<numB<<' '<<'='<<' ';
int alenght = strlen(numA);
int blenght = strlen(numB);
int count = 0;
for(;count < alenght;count++)
numA[count] -= 48;
count = 0;
for(;count < blenght;count++)
numB[count] -= 48;
if(alenght > blenght)
add(numA,numB,alenght,blenght);
else
add(numB,numA,blenght,alenght);
cout<<endl;
if(i != times)
cout<<endl;
}
return 0;
}
sunyuke 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 nirendao 的回复:]
简单写了一个,你自己稍微改下


C/C++ code
#include <cstdlib>
#include <iostream>

using namespace std;

void Reverse(char array[], int size)
{
int i = 0, j = size-1;
char tmp;
while(i<j)
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
}

void Add(char * a, char * b, char * c)
{
int i = 0;…
[/Quote]

VS2008一调试 满眼的烫烫烫烫烫烫
deerwin1986 2009-02-24
  • 打赏
  • 举报
回复
大数啊 用INT数组快些
或者用CHAR数组占空间小些...
执假以为真 2009-02-24
  • 打赏
  • 举报
回复
简单写了一个,你自己稍微改下


#include <cstdlib>
#include <iostream>

using namespace std;

void Reverse(char array[], int size)
{
int i = 0, j = size-1;
char tmp;
while(i<j)
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
}

void Add(char * a, char * b, char * c)
{
int i = 0;
int tmp, r = 0;
while(a[i]!='\0' && b[i]!='\0')
{
tmp = a[i] - '0' + b[i] - '0' + r;
if(tmp > 9)
{
r=1;
tmp -= 10;
}
else
r=0;

c[i] = tmp + '0';
i++;
}
if(a[i] == '\0' && b[i] == '\0')
{
if(r==1)
c[i] = '1';
}
else if(a[i] == '\0')
{
if(r==0)
{
strcpy(c+i, b+i);
}
else
{
Add("1", b+i, c+i);
}
}
else // b[i] == '\0'
{
if(r==0)
{
strcpy(c+i, a+i);
}
else
{
Add("1", a+i, c+i);
}
}

Reverse(c, strlen(c));
}

int main()
{
char a[] = {'1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9','\0'};
char b[] = {'9','9','8','8','7','7','6','6','5','5','4','4','3','3','2','2','1','1','\0'};

char c[1000];
memset(c, 1000, 0);
Add(a, b, c);
cout<<c<<endl;
cin.get();
}
acdbxzyw 2009-02-24
  • 打赏
  • 举报
回复
用数组
waizqfor 2009-02-24
  • 打赏
  • 举报
回复

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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