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;
}
Forever+Young 发布了275 篇原创文章 · 获赞 283 · 访问量 2万+ 私信 关注
标签:int,s2,s1,a1,flag,a2,POJ,Bull,2389 来源: https://blog.csdn.net/weixin_43823808/article/details/104080196