2980大整数乘法

2018-11-10  本文已影响0人  SUNRISE_05fd
#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了, 开心

上一篇 下一篇

猜你喜欢

热点阅读