zoukankan      html  css  js  c++  java
  • UVa 10106 Product 【大数相乘】WA

    虽然是错的代码,但是还是想贴出来,最开始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]!=''&&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
    ");
            }
            else if(panduan(str1)==2||panduan(str2)==2)//判断两个相乘的数里面如果出现0---0(若干个),则不处理这组数据
            {
                printf("
    ");
               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("
    ");
    
            }
        }
    }
    

      

  • 相关阅读:
    erlang开发环境(IDE)搭建
    Mono for android,Xamarin点击事件的多种写法
    .NET C#中处理Url中文编码问题
    第一天正式学习,定一个学习目标吧。
    想转行做开发了!
    Java JMS 程序基础 与 ActiveMQ 安装(一)
    JDBC 基础
    Linux 6.4 设置yum 为centOS源
    C++ 简单实现 依赖注入(IOC)
    安卓项目
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4117874.html
Copyright © 2011-2022 走看看