牟小生
2019-04-16 23:42
采纳率: 100%
浏览 286

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

在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;
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 牟小生 2019-04-17 23:58
    已采纳

    好了,问题已经解决了。

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题