1262 “A+B”

來源:互聯網
上載者:User
 
描述

求a+b的和。

輸入

第一行為整數n(n<100),表示下面有n組資料。每組資料一行,分別為兩個用空格隔開的數a 和 b,a、b為不超過一百位的整數。

輸出

對於每組資料,輸出一行,即a+b的和。

範例輸入
31 1260 26011 22
範例輸出
252033
解題思路:高精度加法,必須轉換為字串形式來做。滿十進一位,從前往後或者從後往前都可以。
 
 
#include <stdio.h>#include <string.h>int main(){    void add(char a[],char b[],char result[]);    void plus(char a[],char b[],char result[]);    char a[1000]={0},b[1000]={0},temp1[1000]={0},temp2[1000]={0};    char result[1000]={0},zancun[1000]={0};    int m,i,n;int up,number;scanf("%d",&number);    for(up=1;up<=number;up++){scanf("%s%s",&a,&b);        if(a[0]=='-'&&b[0]=='-')           {m=0;n=0;           for(i=1;a[i]!='\0';i++)               temp1[m++]=a[i];               temp1[m]='\0';               for(i=1;b[i]!='\0';i++)                 temp2[n++]=b[i];                 temp2[n]='\0';                 add(temp1,temp2,result);                 printf("-%s\n",result);                 goto abc;           }           if(a[0]!='-'&&b[0]!='-')           {                 add(a,b,result);                   printf("%s\n",result);                   goto abc;           }         if(a[0]=='-'&&b[0]!='-')           {   m=0;              n=0;              for(i=1;a[i]!='\0';i++)                 temp2[m++]=a[i];                 temp2[m]='\0';              for(i=0;b[i]!='\0';i++)                 temp1[n++]=b[i];                 temp1[n]='\0';                 if(m<n)                 {plus(temp1,temp2,result); printf("%s\n",result);                 goto abc;                 }                 if(m>n)                 {   strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                     plus(temp1,temp2,result);                      printf("-%s\n",result);                      goto abc;                 }                 if(m==n)                 {  if(strcmp(temp1,temp2)==0)                     {printf("0\n");                     goto abc;                     }                     if(strcmp(temp1,temp2)<0)                       {strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                           plus(temp1,temp2,result);                        printf("-%s\n",result);                        goto abc;                       }                       if(strcmp(temp1,temp2)>0)                        { plus(temp1,temp2,result);                          printf("%s\n",result);                          goto abc;                        }                 }           }           if(a[0]!='-'&&b[0]=='-')           { m=0;              n=0;              for(i=0;a[i]!='\0';i++)                 temp1[m++]=a[i];                 temp1[m]='\0';              for(i=1;b[i]!='\0';i++)                 temp2[n++]=b[i];                 temp2[n]='\0';                  if(m>n)                 {                     plus(temp1,temp2,result);                 printf("%s\n",result);                 goto abc;                 }                 if(m<n)                 {  strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                     plus(temp1,temp2,result);                      printf("-%s\n",result);                      goto abc;                 }                 if(m==n)                 {  if(strcmp(temp1,temp2)==0)                    { printf("0\n");                     goto abc;                    }                     if(strcmp(temp1,temp2)<0)                       {strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                           plus(temp1,temp2,result);                        printf("-%s\n",result);                        goto abc;                       }                       if(strcmp(temp1,temp2)>0)                        { plus(temp1,temp2,result);                          printf("%s\n",result);                          goto abc;                        }                 }           }  abc:   memset(a,0,sizeof(a));     memset(b,0,sizeof(b));     memset(result,0,sizeof(result));      memset(temp1,0,sizeof(temp1));     memset(temp2,0,sizeof(temp2));     memset(zancun,0,sizeof(zancun));    }    return 0;}void add(char a[],char b[],char result[]){    int la,lb,lresult;    int s,c,t;    int i;    char temp;    la=strlen(a)-1;    lb=strlen(b)-1;    c=0;    t=0;    while(la >= 0 || lb >= 0){        if(la < 0) s=b[lb--]-48;        else if(lb < 0) s=a[la--]-48;        else s=a[la--]-48+b[lb--]-48;        result[t++]=(s+c)%10+48;        c=(s+c)/10;    }    if(c != 0) {result[t]=c+48;lresult=t;}    else lresult=t-1;    for(i=0;i <= lresult/2;i++){        temp=result[i];        result[i]=result[lresult-i];        result[lresult-i]=temp;    }}void plus(char temp1[],char temp2[],char result[]){ int la,lb,lresult;     int s,c,t=0;     int i,j;     char temp[1000]={0};     char temp3[1000]={0},temp4[1000];     int length;    memset(temp4,'0',sizeof(temp4));     la=strlen(temp1);/*???é?¢?????°?¤§????????????é?¢?????°??*/     lb=strlen(temp2);     c=0;s=0;     for(i=la-1;i>=0;i--)       temp3[c++]=temp1[i];     for(i=lb-1;i>=0;i--)        temp4[s++]=temp2[i];     temp[la]='0';    for(i=0;i<la;i++)      {          if(temp3[i]<temp4[i])   {temp[i]=temp3[i]+10-temp4[i]+'0';            temp3[i+1]--;           }         if(temp3[i]==temp4[i])            temp[i]='0';          if(temp3[i]>temp4[i])            temp[i]=temp3[i]-temp4[i]+'0';      }      for(j=i;j>0;j--)      {  if(temp[j]=='0')     continue;  else     break;      }      for(i=j;i>=0;i--)        result[t++]=temp[i];        result[t]='\0';}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.