牟小生 2019-04-16 23:31 采纳率: 100%
浏览 227
已采纳

hdu 1002 (高精度加法运算)一直出现Runtime Error (ACCESS_VIOLATION),请问怎样改正?

题目如下:

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

在vs上运行没有问题,但是oj上一直结果为Runtime Error (ACCESS_VIOLATION), _请问为什么?

#include<iostream>
using namespace std;
char a[1010] = { 0 };
char b[1010] = { 0 };
bool flag = true;
int main()
{
    int T;
    while(cin >> T)
    { 
        if (T < 1 || T > 20)
            break;
        for (int j = 1; j <= T; j++)
        {
            if (T == 0)
                break;
            //below

            cin >> a;
            cin >> b;
            int m = 0, n = 0;//count
            for (int i = 0; i < 1000; i++)
            {
                if (a[i] != 0)
                    m++;
                else
                    break;
            }
            for (int i = 0; i < 1000; i++)
            {
                if (b[i] != 0)
                    n++;
                else
                    break;
            }
            char*c = new char[m + 1];//c[m]
            char*d = new char[n + 1];//d[n]
            int t = 0;
            for (int i = m - 1; i >= 0; i--)
            {

                c[t] = a[i];
                t++;
            }
            c[m] = 0;
            t = 0;
            for (int i = n - 1; i >= 0; i--)
            {

                d[t] = b[i];
                t++;
            }
            d[n] = 0;//two integers ready
            char*c1 = new char[m + 1];
            char*d1 = new char[n + 1];
            t = 0;
            for (int i = m - 1; i >= 0; i--)
            {
                c1[t] = c[i];
                t++;
            }
            t = 0;
            for (int i = n - 1; i >= 0; i--)
            {
                d1[t] = d[i];
                t++;
            }
            c1[m] = 0;
            d1[n] = 0;
            cout << "Case " << j << ":" << endl;
            cout << c1 << " + " << d1 << " = ";
            int k;
            if (m > n)
                k = m;
            else
                k = n;
            char*e = new char[k + 1];//extra number
            char*f = new char[k + 1];//final number
            for (int i = 0; i < k; i++)
            {
                char z = 0;
                if (c[i] > 0 && d[i] > 0)
                    z = (c[i] - '0') + (d[i] - '0');
                else if (c[i] <= 0)
                    z = d[i] - '0';
                else
                    z = c[i] - '0';
                f[i] = z;
                if (z > 9)
                {
                    e[i + 1] = '1';
                    f[i] = z - 10;
                }
                if (e[i] == '1')
                    f[i]++;
                if (f[i] > 9)
                {
                    e[i + 1] = '1';
                    f[i] = f[i] - 10;
                }
                f[i] += '0';
            }
            if (e[k] == '1')
            {
                f[k] = '1';
                for (int i = k; i >= 0; i--)
                {
                    while (f[i] == '0')
                    {
                        i--;
                    }
                    if (i < 0)
                        i++;
                    cout << f[i];
                }
            }
            else
            {
                for (int i = k - 1; i >= 0; i--)
                {
                    while (f[i] == '0')
                    {
                        i--;
                    }
                    if (i < 0)
                        i++;
                    cout << f[i];
                }
            }
            cout << endl;
            if (j != T)
                cout << endl;
            delete[]c;
            delete[]d;
            delete[]c1;
            delete[]d1;
            delete[]e;
            delete[]f;
            //above
        }
    }

    return 0;
}
  • 写回答

2条回答 默认 最新

  • tktp_ht 2019-04-17 13:35
    关注

    尝试将

                char*c = new char[m + 1];//c[m]
                char*d = new char[n + 1];//d[n]
    

    改成

                char*c = new char[1010];
                char*d = new char[1010];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码