zoukankan      html  css  js  c++  java
  • 10106 Product

     

     

     Product 

    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
    
     






    高精度乘法,注意进位的时候,遍历K次,就要从第K位开始加起
    由于只进行了len次循环,最后一位可能还可以进位,所以要加一个判断,使得最后一位不会丢失

    #include <cstdio>
    #include <cstring>
    char x[500];
    char y[500];
    int s1[500],s2[500];
    int s[1000];
    int main()
    {
        int lenx,leny;
        while (scanf("%s%s",x,y)!=EOF)
        {
            memset(s1,0,sizeof(s1));
            memset(s2,0,sizeof(s2));
            memset(s,0,sizeof(s));
            lenx=strlen(x);
            leny=strlen(y);
            for (int i=lenx-1,j=0; i>=0; i--,j++)
            {
                s1[j]=(int)x[i]-'0';
            }
            for (int ii=leny-1,jj=0; ii>=0; ii--,jj++)
            {
                s2[jj]=(int)y[ii]-'0';
            }
            int q,k;
            for (k=0; k<lenx; k++)
            {
                int c=0,temp;
                for (q=0; q<leny; q++)
                {
                    temp=s[k+q]+s1[k]*s2[q]+c;
                    s[k+q]=temp%10;
                    c=temp/10;
                }
                if (c>0) s[q+k]=c;
            }
            int p;
            for (p=999; p&&!s[p]; p--);
            p++;
            while (p--)
            {
             printf("%d",s[p]);
            }
            putchar('\n');
        }
        return 0;
    }





  • 相关阅读:
    python_异常处理
    python_类与对象
    函数
    字符串(查找,替换,分割)
    容器类型的数据
    条件语句
    关于WinSock编程的多线程控制
    利用Delphi编写Socket通信程序
    SQL Server数据库开发的二十一条军规
    SQL Server中的日期格式化
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2868757.html
Copyright © 2011-2022 走看看