A+B长字符串
来源:互联网 发布:抽奖软件代码 编辑:程序博客网 时间:2024/05/02 00:46
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
21 2112233445566778899 998877665544332211
Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110#include <stdio.h>#include <string.h>#define N 1001char A[N],B[N],sum[N];int main(){int T,i,j,k,x,carry; scanf("%d",&T);for(i=0;i<T;i++){if(i)printf("\n");scanf("%s%s",&A,&B);j=strlen(A)-1;k=strlen(B)-1;for(x=0,carry=0;(j+1)&&(k+1)!=0;j--,k--,x++){if((A[j]-'0')+(B[k]-'0')+carry<10){sum[x]=(A[j]-'0')+(B[k]-'0')+carry;carry=0;}else{sum[x]=(A[j]-'0')+(B[k]-'0')+carry-10;carry=1;}}if(j+1){for(;j>=0;j--,x++){if(A[j]-'0'+carry<10){sum[x]=(A[j]-'0')+carry;carry=0;}else{sum[x]=0;carry=1;}}}else if(k+1){for(;k>=0;k--,x++){if(B[k]-'0'+carry<10){sum[x]=(B[k]-'0')+carry;carry=0;}else{sum[x]=0;carry=1;}} } if(carry) sum[x]=1; else x--; printf("Case %d:\n",i+1); printf("%s + %s = ",A,B); while(x>-1) printf("%d",sum[x--]); printf("\n");}return 0; }
阅读全文
0 0
- A+B长字符串
- 短字符串a中的字母是否都包含在长字符串b中
- A + B 字符串处理
- A-B 字符串删除
- 字符串A删除字符串B
- A+B(字符串a+b问题)
- 给一个很长的字符串str 还有一个字符集比如{a,b,c} 找出str 里包含{a,b,c}的最短子串。要求O(n)
- HDOJ1228(字符串A+B)
- 【编程之法】如何最快的判断出短字符串b中的所有字符是否都在长字符串a中?编写函数bool StringContain(string&b,string&a)实现此功能。
- 【编程之法】如何最快的判断出短字符串b中的所有字符是否都在长字符串a中?编写函数bool StringContain(string&b,string&a)实现此功能。
- 将字符串a复制到字符串b
- 将字符串a复制为字符串b
- A+B长整数的加法(算法实现)
- A+B长整数的加法(算法实现)
- A+B长整数的加法(算法实现)
- 九度OJ 1016:火星A+B 长整数加法
- hdu 1753 大明A+B 长小数相加
- 获取a+b字符串输入 执行a+b运算
- 解决中文输入 Qt 针对5.8也做了说明 及4.x版本
- numpy模块的linspace函数实例
- 邮件中嵌入html中要注意的样式
- jquery中的html()方法和append()使用注意点
- CSS之水平垂直居中
- A+B长字符串
- [HNOI2008]越狱 快速幂取模
- word 插入对象Microsoft公式3.0失败
- 梯度下降法基本原理
- Git学习
- spring+webscoket注解方式
- Sass安装(windows版)
- 51Nod-1011-最大公约数GCD
- 全文搜索引擎 ElasticSearch 入门教程