美文网首页
2980大整数乘法

2980大整数乘法

作者: SUNRISE_05fd | 来源:发表于2018-11-10 15:33 被阅读0次
    #include<stdio.h>
    #include<string.h>
    #define MAX_LEN 200
    unsigned an1[MAX_LEN + 10];// 存放乘数1int
    unsigned an2[MAX_LEN + 10];//存放乘数2int
    unsigned aResult[MAX_LEN*20 + 10];
    char szLine1[MAX_LEN + 10];
    char szLine2[MAX_LEN + 10];
    int main()
    {
        gets_s(szLine1);
        gets_s(szLine2);
        int i, j;
        int nLen1 = strlen(szLine1);
        memset(an1, 0, sizeof(an1));//Initialize
        memset(an2, 0, sizeof(an2));
        memset(aResult, 0, sizeof(aResult));
        j = 0;
        for (i = nLen1 - 1; i >= 0; i--)
            an1[j++] = szLine1[i] - '0';//化为数
        int nLen2 = strlen(szLine2);
        j = 0;
        for (i = nLen2 - 1; i >= 0; i--)
            an2[j++] = szLine2[i] - '0';//化为数
    
        for (i = 0; i < nLen2; i++)
        {
            for (j = 0; j < nLen1; j++)
                aResult[i + j] += an2[i] * an1[j];
        }
        for (i = 0; i < MAX_LEN * 2; i++)
        {
            if (aResult[i] >= 10)
            {
                aResult[i + 1] += aResult[i] / 10;
                aResult[i] %= 10;
            }
        }
        bool bStartOutput = false;
        for (i = MAX_LEN * 2; i >= 0; i--)
            if (bStartOutput)
                printf("%d", aResult[i]);
            else if (aResult[i]) {
                printf("%d", aResult[i]);
                bStartOutput = true;
            }
        if (!bStartOutput)
            printf("0");
        return 0;
    }
    

    一次就ac了, 开心

    相关文章

      网友评论

          本文标题:2980大整数乘法

          本文链接:https://www.haomeiwen.com/subject/kvizxqtx.html