zoukankan      html  css  js  c++  java
  • △UVA10106

     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

     1 #include<cstdio>
     2 #include<string.h>
     3 
     4 #define maxn 2005
     5 
     6 int main()
     7 {
     8     char a[maxn],b[maxn];
     9     int ans[maxn];
    10     int i,j,k;
    11     int la,lb;
    12     while(scanf("%s%s",a,b) != EOF)
    13     {
    14         la=strlen(a);
    15         lb=strlen(b);
    16         memset(ans,0,sizeof(ans));
    17         for(i=la-1;i>=0;i--)
    18         {
    19             for(j=lb-1,k=la-1-i;j>=0;j--)//大数相乘模拟手算,关键在于错位,即k=la-1-i
    20             {
    21                 ans[k++] += (a[i]-'0')*(b[j]-'0');//ans[0]存的是个位,ans[1]存的是十位,ans[2]存的是百位……
    22             }
    23         }
    24         for(i=0;i<maxn-1;i++)
    25         {
    26             if(ans[i]>9)//进位
    27             {
    28                 ans[i+1]+=ans[i]/10;//因为是乘法,不再像加法一样是进1还是不进位了
    29                 ans[i]%=10;//只留一位
    30             }
    31         }
    32         for(i=maxn-1;i>=0;i--)
    33         {
    34             if(ans[i])//忽略前导0
    35                 break;
    36         }
    37         if(i>=0)
    38         {
    39             for(;i>=0;i--)
    40             {
    41                 printf("%d",ans[i]);
    42             }
    43         }
    44         else
    45             printf("0");
    46         printf("
    ");
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    定时器
    javascript之循环保存数值
    Web 前端之HTML和CSS
    [讲解]容斥原理
    [vijos1048]送给圣诞夜的贺卡<DFS剪枝>
    [vijos1145]小胖吃巧克力<概率dp>
    [noip2012]国王游戏<贪心+高精度>
    [codevs3118]高精度除法<高精度>
    [noip2016]组合数问题<dp+杨辉三角>
    [codevs2370]小机房的树<LCA>
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3687357.html
Copyright © 2011-2022 走看看