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;
}



相关文章
|
7月前
Pseudoprime numbers(POJ-3641 快速幂)
Pseudoprime numbers(POJ-3641 快速幂)
UPC Haywire(模拟退火 || 随机数法)
UPC Haywire(模拟退火 || 随机数法)
103 0
UPC Haywire(模拟退火 || 随机数法)
POJ-3641,Pseudoprime numbers(快速幂)
POJ-3641,Pseudoprime numbers(快速幂)
POJ 1844 Sum
POJ 1844 Sum
106 0
|
Go
POJ 1503 Integer Inquiry 简单大数相加
POJ 1503 Integer Inquiry 简单大数相加
83 0
【PTA】7-6 求最大公约数 (40point(s))
【PTA】7-6 求最大公约数 (40point(s))
212 0
|
安全
D-POJ-3126 Prime Path
Description The ministers of the cabinet were quite upset by the message from the Chief of...
1128 0
POJ1331 Multiply(strtol函数练习)
题目链接:http://poj.org/problem?id=1331 主要介绍strtol函数: long int strtol(const char *nptr,char **endptr,int base); strtol函数会将参数nptr字符串根据参数base来转换成长整型数。
786 0