POJ-2389,Bull Math(大数乘法)

简介: POJ-2389,Bull Math(大数乘法)

Description:


Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros).


FJ asks that you do this yourself; don't use a special library function for the multiplication.


Input:


* Lines 1..2: Each line contains a single decimal number.


Output:


* Line 1: The exact product of the two input lines


Sample Input:


11111111111111


1111111111


Sample Output:


12345679011110987654321


程序代码:


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int N=1001;
char s1[N],s2[N];
int a1[N],a2[N],b[N*2];
int main()
{
  gets(s1);
  gets(s2);
  int j;
  memset(a1,0,sizeof(a1));
  memset(a2,0,sizeof(a2));
  memset(b,0,sizeof(b));
  int len1=strlen(s1);
  int len2=strlen(s2);
  j=0;
  for(int i=len1-1;i>=0;i--)
    a1[j++]=s1[i]-'0';
  j=0;
  for(int i=len2-1;i>=0;i--)
    a2[j++]=s2[i]-'0';
  for(int i=0;i<len1;i++)
    for(int j=0;j<len2;j++)
      b[i+j]+=a1[i]*a2[j];
  for(int i=0;i<N*2;i++)
  {
    if(b[i]>=10)
    {
      b[i+1]+=b[i]/10;
      b[i]%=10;
    }
  }
  bool flag=false;
  for(int i=N*2-1;i>=0;i--)
  {
    if(flag)
      printf("%d",b[i]);
    else if(b[i])
    {
      printf("%d",b[i]);
      flag=true;
    }
  }
  if(!flag)
    printf("0");
  return 0;
}



相关文章
|
算法
poj 1050 To the Max(最大子矩阵之和)
poj 1050 To the Max(最大子矩阵之和)
36 0
华华教月月做数学 两种方法: (快速幂+快速乘) (__int128+快速幂)
华华教月月做数学 两种方法: (快速幂+快速乘) (__int128+快速幂)
61 0
POJ 2689 Prime Distance (埃氏筛 区间筛)
POJ 2689 Prime Distance (埃氏筛 区间筛)
110 0
HDOJ(HDU) 2504 又见GCD(利用最大公约数反推)
HDOJ(HDU) 2504 又见GCD(利用最大公约数反推)
106 0
|
机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
POJ 1775 (ZOJ 2358) Sum of Factorials
145 0
POJ 1844 Sum
POJ 1844 Sum
104 0
|
Go
POJ 1503 Integer Inquiry 简单大数相加
POJ 1503 Integer Inquiry 简单大数相加
80 0
|
安全
D-POJ-3126 Prime Path
Description The ministers of the cabinet were quite upset by the message from the Chief of...
1126 0
|
人工智能 Java
HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 593...
1274 0