博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
acm竞赛例题
阅读量:5780 次
发布时间:2019-06-18

本文共 2863 字,大约阅读时间需要 9 分钟。

  hot3.png

A + B Problem II

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 244704    Accepted Submission(s): 47183

 

 

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.

 

 

Sample Input

2

1 2

112233445566778899 998877665544332211

 

 

Sample Output

Case 1:

1 + 2 = 3

 

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110

 

 

Author

Ignatius.L

 

 

 

 

解题

#include
#include
int main(){    int n,m,len1,len2,maxlen,minlen;    char a[1010],b[1010],sum[1010];    char *max, *min;    scanf("%d",&n);    for(m=1;m<=n;m++)    {        int i,j,flag=0;        memset(sum,0,sizeof(sum));        scanf("%s %s",a,b);        len1=strlen(a);        len2=strlen(b);        if(len1>=len2)//判断字符串哪个长        {            maxlen=len1;            minlen=len2;            max = a;            min = b;        }        else        {            maxlen=len2;            minlen=len1;            max = b;            min = a;        }        for(i=maxlen-1,j=minlen-1;i>=maxlen-minlen&&j>=0;i--,j--)//做循环把两字符串相加放到sum,用flag记录进位,大于9时候为1,并且sum-10,        {                                                       //只加完到最短的(从后面开始加,方便输出从前面0下表开始输出)            sum[i]+=max[i]+min[j]-'0'-'0';            if(i==0&&sum[0]>9)            {                flag=1;                sum[0]-=10;            }            else if(sum[i]>9)            {                sum[i-1]+=1;//进位                sum[i]-=10;//减10            }        }        for(i=maxlen-minlen-1;i>=0;i--)//再加比最短的长出的部分的,从后面开始加。        {            sum[i]+=max[i]-'0';            if(i==0&&sum[0]>9)            {                flag=1;                sum[i]-=10;            }            else if(sum[i]>9)            {                sum[i-1]++;//进位                sum[i]-=10;//减10            }        }        // printf("       %d\n",flag);        if(flag)                    //输出格式        {            printf("Case %d:\n",m);            printf("%s + %s = 1",a,b);}//a,b字符串不能修改,flag=1,sum[0]=0的时候的进位        else        {            printf("Case %d:\n",m);            printf("%s + %s = ",a,b);        }        for(i=0;i

转载于:https://my.oschina.net/qiyong/blog/393433

你可能感兴趣的文章
用javascript获取地址栏参数
查看>>
一起谈.NET技术,你应该知道的15个Silverlight诀窍
查看>>
商教助手!解析夏普液晶高清宽屏投影机系列
查看>>
云南去年有望实现151万贫困人口净脱贫
查看>>
Java架构师面试题系列整理(大全)
查看>>
延伸产业链 中国产粮大省向“精深”问发展
查看>>
消费贷用户70%月收入低于5000元 80、90后是主要人群
查看>>
2018年内蒙古外贸首次突破1000亿元
查看>>
CTOR有助于BCH石墨烯技术更上一层楼
查看>>
被遗忘的CSS
查看>>
Webpack中的sourcemap以及如何在生产和开发环境中合理的设置sourcemap的类型
查看>>
做完小程序项目、老板给我加了6k薪资~
查看>>
java工程师linux命令,这篇文章就够了
查看>>
关于React生命周期的学习
查看>>
webpack雪碧图生成
查看>>
搭建智能合约开发环境Remix IDE及使用
查看>>
Spring Cloud构建微服务架构—服务消费基础
查看>>
RAC实践采坑指北
查看>>
runtime运行时 isa指针 SEL方法选择器 IMP函数指针 Method方法 runtime消息机制 runtime的使用...
查看>>
LeetCode36.有效的数独 JavaScript
查看>>