uva10106(大数)

来源:互联网 发布:淘宝买家申请售后换货 编辑:程序博客网 时间:2024/06/07 10:05

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1047

10106 - Product

Time limit: 3.000 seconds

Product

TheProblem

The problem is to multiply two integers X, Y.(0<=X,Y<10250)

TheInput

The input will consist of a set of pairs of lines. Each line inpair contains one multiplyer.

TheOutput

For each input pair of lines the output line should consist oneinteger the product.

SampleInput

12122222222222222222222222222

SampleOutput

144444444444444444444444444
 题意:就是高精度乘法的计算...
#include<stdio.h>
#include<string.h>
#define maxn 2000
int arr1[maxn],arr2[maxn];
char str1[maxn],str2[maxn];//str1[]输入的第一个数,str2[]输入的第二个数
int result[maxn];//要输出的结果
int main()
{
 memset(str1,0,sizeof(str1));//清零
 memset(str2,0,sizeof(str2));//清零
 while(scanf("%s %s",str1,str2)!=EOF)
 {
  memset(result,0,sizeof(result));
  memset(arr1,0,sizeof(arr1));
  memset(arr2,0,sizeof(arr2));
  int k=0,i,j;
  for(i=strlen(str1)-1;i>=0;i--)//arr1[0]保存str1[]个位数......
   arr1[k++]=str1[i]-'0';
  k=0;
  for(i=strlen(str2)-1;i>=0;i--)
   arr2[k++]=str2[i]-'0';//arr2[0]保存str2[]个位数
  for(i=0;i<strlen(str1);i++)
  {
   for(j=0;j<strlen(str2);j++)
    result[i+j]+=arr1[i]*arr2[j];//这个挺好理解的,自己用两个数试试体会下就知道了
  }
  for(i=0;i<maxn;i++)
  {
   if(result[i]>9)//result[i]>9就表示要进位
   {
    result[i+1]+=result[i]/10;
    result[i]%=10;//把进位的保存在后一位
   }
  }
  bool flag=false;
  for(i=maxn;i>=0;i--)
  {
   if(result[i])//只要有一个数字不是0的就可以输出结果这是为了避免输出像000000的这样的数(这样的数直接输出0)
   {
    flag=true;
    break;
   }
  }
  if(flag)
  {
   for(k=i;k>=0;k--)
    printf("%d",result[k]);
  }
  else
   printf("0");
  printf("\n");
  memset(str1,0,sizeof(str1));
  memset(str2,0,sizeof(str2));
 }
 return 0;
}
 
原创粉丝点击