首页 > 其他 > 详细

UVa 10106 Product 【大数相乘】WA

时间:2014-11-24 07:33:42      阅读:180      评论:0      收藏:0      [点我收藏+]

虽然是错的代码,但是还是想贴出来,最开始WA发现是没有考虑到乘积为0的情况,后来把a*0,0*a,a*0---0(若干个0),0--0(若干个0)*a都考虑进去了;可是还是WA,实在不懂先留在这儿。

 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


#include<stdio.h>
#include<string.h>
#define max 500
int panduan(char a[])
{
  int tag,flag=1;
  int i;
  for(i=0;a[i]!=‘\0‘&&flag;i++)
  {
  	if(a[i]!=‘0‘)
  	{
  		flag=0;
  	}
  }
  if(flag)
  {
      if(i==1)
      return 1;
      else
      return 2;
  }
  else
  return 0;
}
int main()
{
    int i,j;
    int len1,len2,len;
    int a[max],b[max],c[max];
    char str1[max],str2[max];
    while(~scanf("%s %s",&str1,&str2))
    {
        if(panduan(str1)==1||panduan(str2)==1)//判断两个相乘的数里面有没有‘0‘
        {
            printf("0\n");
        }
        else if(panduan(str1)==2||panduan(str2)==2)//判断两个相乘的数里面如果出现0---0(若干个),则不处理这组数据
        {
            printf("\n");
           continue;
        }
        else
        {

            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            memset(c,0,sizeof(c));
            len1=strlen(str1);
            len2=strlen(str2);
            for(i=0;i<len1;i++)
             {
            a[i]=str1[len1-i-1]-‘0‘;
             }
            for(i=0;i<len2;i++)
              {
            b[i]=str2[len2-i-1]-‘0‘;
              }
            for(i=0;i<len2;i++)
              {
            for(j=0;j<len1;j++)
            c[i+j]+=b[i]*a[j];
               }
        len=i+j;
        for(i=0;i<len;i++)
        {
            if(c[i]>=10)
            {
            c[i+1]+=c[i]/10;
            c[i]=c[i]%10;
            }
        }
        for(i=len;(c[i]==0)&&(i>=0);i--);
        for(j=i;j>=0;j--)
           printf("%d",c[j]);
           printf("\n");

        }
    }
}

  

UVa 10106 Product 【大数相乘】WA

原文:http://www.cnblogs.com/wuyuewoniu/p/4117874.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!