加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Product(大数乘法)

发布时间:2020-12-14 03:53:44 所属栏目:大数据 来源:网络整理
导读:The Problem The problem is to multiply two integers X,Y. ( 0=X,Y10 250 ) The Input The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer. The Output For each input pair of lines the output line should

The Problem

The problem is to multiply two integers X,Y. (0<=X,Y<10250)

The Input

The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output

For each input pair of lines the output line should consist one integer the product.

Sample Input

12
12
2
222222222222222222222222

Sample Output

144
444444444444444444444444

Input

Output

Sample Input

Sample Output

Hint

Please enable JavaScript to view the &lt;a href="http://disqus.com/?ref_noscript"&gt;comments powered by Disqus.&lt;/a&gt;

这是一个可以考虑时间复杂度的问题,学长说的算法说是降低时间,可能因为我的数组过长而且在算法细节上用的长循环过多导致我的代码用时不短。

#include <stdio.h>
#include <string.h>

int ji[10][1000];
int n1[1000];
int n2[1000];
int sum[1000];
char a[1000];
void chushihua (void)
{
    memset(ji,sizeof(int) * 10 * 1000);
    memset(n1,sizeof(int) * 1000);
    memset(n2,sizeof(int) * 1000);
    memset(sum,sizeof(int) * 1000);
}

int fuz (char * a,int *n)
{
    int i = 999;
    int len = 0;
    char *p = a;

    while (*p != '')
    {
        len++;
        p++;
    }
    p--;

    while (p >= a)
    {
        n[i] = (int)(*p - '0');
        i--;
        p--;
    }
    return len;
}

void va(int *n)
{
    int i = 999;
    while (i >= 0)
    {
        if (n[i] >= 10)
        {
            n[i - 1] += (int)(n[i] / 10);
            n[i] = n[i] % 10;
        }
        i--;
    }
}

void caiy (int *n1,int n1_len)
{
    int n = 0,i;
    int len;

    while (n < 10)
    {
        i = 999;
        len = n1_len;
        while (len--)
        {
            ji[n][i] += n1[i] * n;
            va(ji[n]);
            i--;
        }
        n++;
    }
}

void pri(int *n)
{
    int tf = 0,i = 0;
    int ptf = 0;

    while (i < 1000)
    {
        if ((!tf) && n[i] != 0)
            tf = 1;

        if (tf)
        {
            ptf = 1;
            printf ("%d",n[i]);
        }

        i++;
    }
    if (!ptf)
        printf ("0");

    printf ("n");
}

void yiwei(int *num,int n)
{
    int *p1 = num;
    int *p2 = NULL;

    while (n--)
    {
        p1 = num;
        while (*p1 != 0)
            p1++;

        p2 = p1;
        p1--;
        while (p2 <= &num[999])
        {
            *p1 = *p2;
            p1++;
            p2++;
        }
        p2--;
        *p2 = 0;
    }

}

void Qsum(int n2_len)
{
    int *p = &n2[999];
    int tmp[1000];
    int n = 0,i;

    while (n < n2_len)
    {
        switch (*p)
        {
        case 0:
            memcpy(tmp,ji[0],sizeof(int) * 1000);
            break;
        case 1:
            memcpy(tmp,ji[1],sizeof(int) * 1000);
            break;
        case 2:
            memcpy(tmp,ji[2],sizeof(int) * 1000);
            break;
        case 3:
            memcpy(tmp,ji[3],sizeof(int) * 1000);
            break;
        case 4:
            memcpy(tmp,ji[4],sizeof(int) * 1000);
            break;
        case 5:
            memcpy(tmp,ji[5],sizeof(int) * 1000);
            break;
        case 6:
            memcpy(tmp,ji[6],sizeof(int) * 1000);
            break;
        case 7:
            memcpy(tmp,ji[7],sizeof(int) * 1000);
            break;
        case 8:
            memcpy(tmp,ji[8],sizeof(int) * 1000);
            break;
        case 9:
            memcpy(tmp,ji[9],sizeof(int) * 1000);
            break;
        }

        yiwei(tmp,n);
        i = 999;
        while (i >= 0)
        {
            sum[i] += tmp[i];
            i--;
        }
        va(sum);
        n++;
        p--;
    }
}

int main()
{
    int n1_len = 0,n2_len = 0;
    //freopen ("1.txt","r",stdin);
    while (gets(a) != NULL)
    {
        n1_len = fuz(a,n1);
        gets (a);
        n2_len = fuz(a,n2);
        caiy(n1,n1_len);
        Qsum(n2_len);
        pri(sum);
        chushihua();
    }
    return 0;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读