Hard to Believe, but True!(大数加法的模板和atoi()的应用)

来源:互联网 发布:北大青鸟java多少钱 编辑:程序博客网 时间:2024/04/30 19:21

Hard to Believe, but True!


The fight goes on, whether to store numbers starting with their most significant digit or their least significant digit. Sometimes this is also called the "Endian War". The battleground dates far back into the early days of computer science. Joe Stoy, in his (by the way excellent) book "Denotational Semantics", tells following story:
"The decision which way round the digits run is, of course, mathematically trivial. Indeed, one early British computer had numbers running from right to left (because the spot on an oscilloscope tube runs from left to right, but in serial logic the least significant digits are dealt with first). Turing used to mystify audiences at public lectures when, quite by accident, he would slip into this mode even for decimal arithmetic, and write things like 73+42=16. The next version of the machine was made more conventional simply by crossing the x-deflection wires: this, however, worried the engineers, whose waveforms were all backwards. That problem was in turn solved by providing a little window so that the engineers (who tended to be behind the computer anyway) could view the oscilloscope screen from the back. 
[C. Strachey - private communication.]"
You will play the role of the audience and judge on the truth value of Turing's equations.

Input Specification

The input contains several test cases. Each specifies on a single line a Turing equation. A Turing equation has the form "a+b=c", where a, b, c are numbers made up of the digits 0,...,9. Each number will consist of at most 7digits. This includes possible leading or trailing zeros. The equation "0+0=0" will finish the input and has to be processed, too. The equations will not contain any spaces.

Output Specification

For each test case generate a line containing the word "True" or the word "False", if the equation is true or false, respectively, in Turing's interpretation, i.e. the numbers being read backwards.

Sample Input

73+42=165+8=1310+20=300001000+000200=000301234+5=12391+0=07000+8000=510+0=0

Sample Output

TrueFalseTrueTrueFalseFalseTrueTrue

解析:这题题意理解很重要,就是两个数倒序相加,看是否和等号后面数的倒序是否相同。这题直接暴力也可过。我用的大数相加模拟的,但是wa了N次还是没过。不过可以当一个大数加法的模板来用。网上还有的是用atoi()函数把字符数组转换成整数并忽略前面的0;在头文件#include<stdlib>里定义。

代码一:模拟大数加法的,冗余度大。

#include"stdio.h"
#include"string.h"
int lena,lenb,lenc;
char a[20],b[20],c[20],d[20];
void equAdd()
{
int i,ad=0,a1,b1,c1,k=0;
for(i=0;i<lena;i++)
{
           a1=a[i]-'0';
          b1=b[i]-'0';
          c1=a1+b1+ad;
          if(c1>=10)
          {
        ad=1;
c1=c1%10;
        }
        else
        {
          ad=0;
          }
          d[k++]=c1+'0';
}
if(ad==1)
d[k++]='1';
}
void  add()
{
    int a1,b1,c1,ad=0,min,i,k,j;
         if(lena==lenb)
         {
           equAdd();     //如果两个字符串的长度相等则用 equAdd();函数执行 
           }
      if(lena<lenb)       //如果a的长度小于b,则以a为标准相加。然后把b多余的数字直接给数组d 
      {
      min=lena;
      k=0;
         for(i=0;i<lena;i++)
        {
       
          a1=a[i]-'0';
          b1=b[i]-'0';
          c1=a1+b1+ad;
          if(c1>=10)
          {
        ad=1;
c1=c1%10;
        }
        else
        {
          ad=0;
          }
          d[k++]=c1+'0';
         
          }
          j=lena;
          if(ad==1)
          {
          for(i=j;i<lenb;i++)
          {
          int x=b[i]-'0';
       if(x+ad>=10)
       {
       ad=1;
             x=(x+1)%10;
       }
       else
       {
        ad=0;          
       }
       d[k++]=x+'0';
        }
          if(ad==1)
          d[k++]='1';
          }      
          else
          {
          for(i=j;i<lenb;i++)               
       d[k++]=b[i];
          }
          
               }     
      else
               {
  if(lena>lenb)//如果b的长度小于a,则以b为标准相加。然后把a多余的数字直接给数组d 
               {
                min=lenb;
      k=0;
         for(i=0;i<lenb;i++)
        {
       
          a1=a[i]-'0';
          b1=b[i]-'0';
          c1=a1+b1+ad;
          if(c1>=10)
          {
        ad=1;
c1=c1%10;
        }
        else
        {
         
          ad=0;
          }
          d[k++]=c1+'0';
         
          }
          j=lenb;
          if(ad==1)
          {
          for(i=j;i<lena;i++)
          {
          int x=a[i]-'0';
       if(x+ad>=10)
       {
       ad=1;
             x=(x+1)%10;
       }
       else
       {
        ad=0;          
       }
       d[k++]=x+'0';
        }
          if(ad==1)
          d[k++]='1';
          }     
            else
             {
          for(i=lenb;i<lena;i++)               
       d[k++]=a[i];
              }
               }          
      }  
}
int  judge()           //判断前面的倒序和是否与后面的倒序相等 
{
      int i,j,k;
       lena=strlen(a);
       lenb=strlen(b);
       lenc=strlen(c);
      
      while(a[lena-1]=='0')      //首先将三个数的的前导0去掉 
      {
        lena--;   
      if(lena==0)
      break;
      }
      if(lena==0)
        {
        lena=1;
        a[0]='0';
        }
       while(b[lenb-1]=='0')
      {      
      lenb--;
      if(lenb==0)
      break;
      }
      if(lenb==0)
        {
        lenb=1;
        b[0]='0';
        }
       while(c[lenc-1]=='0')
      {
    lenc--;
      if(lenc==0)
      break;
      }
      if(lenc==0)
        {
        lenc=1;
        c[0]='0';
        }
     add();         //调用相加函数 
      int lend=strlen(d);
      if(lenc!=lend)          //如果字符串的长度不相等则直接返回0 
      return 0;
      for(i=0;i<lenc;i++)     //依次判断是否相等 
        if(c[i]!=d[i])
        return 0;
         
         return 1;
      
}
int main()
{
int i,j,len;
char s[100];
while(scanf("%s",s)!=EOF)
{

memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
len=strlen(s);
j=0;
for(i=0;i<len;i++)                //从字符串的开始依次将加数,被加数以及等号后面的和,放入到a,b,c数组中 
  {
     if(s[i]!='+')
     a[j++]=s[i];
     else
     break;
      }
      j=0;
      for(i=i+1;;i++)
      {
      if(s[i]!='=')
      b[j++]=s[i];
      else
      break;
      }
      j=0;
      for(i=i+1;i<len;i++)
      {
         c[j++]=s[i];
      }
      if(judge())
      printf("True\n");
      else
      printf("False\n");
      
}
return 0;
}


代码2:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char x[1000],a[100],b[100],c[100];
int lena,lenb,lenc,i,h,j,k,f,aa,bb,cc;
while(scanf("%s",&x)!=EOF){
for(i=0;x[i]!='+';i++)
{
a[i]=x[i];
}
a[i]='\0';k=i;h=0;lena=k;
for(i=k+1;x[i]!='=';i++)
{
b[h++]=x[i];
}
b[h]='\0';j=0;lenb=h;h=i;
for(i=h+1;x[i]!='\0';i++)
{
c[j++]=x[i];
}
c[j]='\0';lenc=j;
f=0;
for(i=lena-1;i>=0;i--)
{
x[f++]=a[i];
}
x[f]='\0';
strcpy(a,x);
f=0;
for(i=lenb-1;i>=0;i--)
{
x[f++]=b[i];
 
} x[f]='\0';
strcpy(b,x);
f=0;
for(i=lenc-1;i>=0;i--)
{
x[f++]=c[i];
}
x[f]='\0';
strcpy(c,x);
if(atoi(a)+atoi(b)==atoi(c))printf("True\n");
else printf("False\n");
}}

原创粉丝点击