UstcOJ 1036 Long Sum

Description

A and B are both integers, which have at most 300 decimal digits. Please calculate their sum.

Input

Each line of the input contains two decimal integers A and B separated with a space character. (0<=A,B<10^300)

Output

For each line of input, print the value of A+B in decimal form on a line by itself.

Sample Input

1 1
1234567890 10000000010

Sample Output

2
11234567900

分析

两个大整数相加,最多300位,先按字符串读入,之后挨个字符转化为数字相加,并进位。最后如果哪个数有多余的数,再加在一块。

#include"stdio.h"
int main()
{
   char a[301],b[301];
   while(scanf("%s %s",a,b)!=EOF)
   {
      //printf("%s %s\n",a,b);
      int alength=0,blength=0;
      while(a[alength]!='\0')alength++;
      while(b[blength]!='\0')blength++;
      //printf("%d %d\n",alength,blength);

      int c[302];
      for(int i=0;i<302;i++)
      c[i]=0;
      int i=alength-1,j=blength-1,clength=0;
      while(i>=0&&j>=0)
      {
         int temp=a[i]-'0'+b[j]-'0'+c[clength];
         if(temp>=10) {c[clength]=temp%10;c[clength+1]=temp/10;}
         else c[clength]=temp;
         i--;
         j--;
         clength++;
      }
      while(i>=0)
      {
         int temp=a[i]-'0'+c[clength];
         if(temp>=10) {c[clength]=temp%10;c[clength+1]=temp/10;}
         else c[clength]=temp;
         i--;
         clength++;
      }
      while(j>=0)
      {
         int temp=b[j]-'0'+c[clength];
         if(temp>=10) {c[clength]=temp%10;c[clength+1]=temp/10;}
         else c[clength]=temp;
         j--;
         clength++;
      }
      if(c[clength]!=0)clength++;
      for(int k=clength-1;k>=0;k--)
      printf("%d",c[k]);
      printf("\n");
   }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • 变幻的人生和不变的人性 驴得水是今年看的第一部好片子,好到我到处给人推荐,好到到处去查影评,好到我为此写一篇文章。...
    碎云阅读 2,962评论 0 0
  • 出门在外久了与人打招呼常常多问一句你是哪里人,回答若是东北山东都会倍感亲切。有人问过我,你一口东北味装什么山东人啊...
    小董二阅读 3,505评论 1 2

友情链接更多精彩内容