A+B problem

miaoqiucheng 2017-06-28 05:24:20
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
freopen("/home/miao/Documents/program/c++/HOJ/in.txt", "r", stdin);
char str1[1010], str2[1010];
int t, str_len1, str_len2, str_max, k, num = 1;
int a[1010] = {0}, b[1010] = {0}, c[1010] = {0};
std::cin>>t;
while(t--)
{

std::cin>>str1;
str_len1 = strlen(str1);
for (int i=0; i<str_len1; i++)
a[i] = str1[str_len1 - 1 - i] - '0'; //because str1 is string, so we should convert it to integer, sub the '0', ASCII code.

std::cin>>str2;
str_len2 = strlen(str2);
for (int i=0; i<str_len2; i++)
b[i] = str2[str_len2 - 1 - i] - '0';

if (str_len1 > str_len2)
str_max = str_len1;
else
str_max = str_len2;

k = 0;
for (int i=0; i<str_max; i++)
{
c[i] = (a[i] + b[i] + k) % 10;
k = (a[i] + b[i] + k) / 10;//进位
}


if (k != 0)
c[str_max] = 1;
else
c[str_max] = 0;
std::cout<<"Case "<<num<<":"<<endl;
num++;
std::cout<<str1<<" + "<<str2<<" = ";
if (c[str_max] == 1)
std::cout<<"1";
for (int i = str_max-1; i >= 0; i--)
std::cout<<c[i];

std::cout<<endl;
if (t >= 1)
std::cout<<endl;
}

return 0;
}




input:
7
100 200000
1 2
10000 20
112233445566778899 998877665544332211
0 0
10 30
1000 200000


output:
Case 1:
100 + 200000 = 200100

Case 2:
1 + 2 = 3

Case 3:
10000 + 20 = 10020

Case 4:
112233445566778899 + 998877665544332211 = 1111111111111111110

Case 5:
0 + 0 = 0

Case 6:
10 + 30 = 40

Case 7:
1000 + 200000 = 971000


请问是哪里出了问题,谢谢.

...全文
185 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wallesyoyo 2017-06-30
  • 打赏
  • 举报
回复
你每次在往 a b 数组里面加数据的时候,上次存的数据未清空,所以导致结果异常,在你的while循环里面,最开头部分加上下面这部分代码就ok了:

memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
赵4老师 2017-06-30
  • 打赏
  • 举报
回复
仅供参考:
#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
FancyMouse 2017-06-29
  • 打赏
  • 举报
回复
显然是脏数据啊 2 11111 11111 99 1
拂去烟尘 2017-06-29
  • 打赏
  • 举报
回复
能不能详细说一下,因为这个错误困扰我很长时间了,花了大量时间结果啥结果都没得到,我感觉自己的思路是没有问题的呀.
赵4老师 2017-06-29
  • 打赏
  • 举报
回复
输入输出格式
战在春秋 2017-06-28
  • 打赏
  • 举报
回复
Sorry,看了代码,是尝试用字符数组处理整型计算,应该和位数无关。
战在春秋 2017-06-28
  • 打赏
  • 举报
回复
代码逻辑没细看,但至少需要注意int类型可以表示的数据是有范围的。 可以参考: https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

64,654

社区成员

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

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