杭电1002 a+b problem2

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 294530    Accepted Submission(s): 56704


Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 

  说说我遇到的问题吧。
  1:首先是读题的问题。不知道exceed 1000是什么意思,就定义了一个long long,想着这个数很大了呗,这个题和1000有什么区别啊,结果交上去不出意料的的了一个WA 大哭.
  2:知道了意思后,就想着怎么解决这个问题呢。数组是第一个想到的,可是数组的输入是个问题,因为不知道循环的次数,不知道怎么输。可是数组又必须用,memset函数必须上啊。最后就想了一个办法,先用gets用字符串录进去,字符串有strlen函数啊,这样次数问题就解决了。再把数组的数一个一个录进去就好了。
  3:数组的存储问题。正着还是反着呢?代码部分会看到答案,大家肯定知道怎么选。
#include<stdio.h>
#include<string.h>
int main()
{
 char d[1000],e[1000],al;
 int a[1000],b[1000],c[1000],i=0,j,k=1,max1,max2,max;
 memset(a,0,sizeof(a));
 memset(b,0,sizeof(b));
 memset(c,0,sizeof(c));
    gets(d);
    max1=strlen(d)-1;
    for(i=0;i<=max1/2;)
    {
     al=d[max1];
  d[max1]=d[i];
  d[i]=al;
  i++;
  max1--;;
    }
 gets(e);
 max2=strlen(e)-1;
    for(i=0;i<=max2/2;)
    {
     al=e[max2];
  e[max2]=e[i];
  e[i]=al;
  i++;
  max2--;;
    }
 for(i=0;i<strlen(d);i++)
 {
  a[i]=d[i]-48;
 }
 for(i=0;i<strlen(e);i++)
 {
  b[i]=e[i]-48;
 }
 max=(strlen(d)>strlen(e)?strlen(d):strlen(e));
 c[0]=(a[0]+b[0])%10;
 while(k<=max)
 {
  c[k]=(a[k]+b[k])%10+(a[k-1]+b[k-1])/10;
  k++;
 }
 if(c[max]!=0)
 printf("%d",c[max]);
 k=max-1;
 for(k;k>=0;k--)
 {
  
  printf("%d",c[k]);
 }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值