A + B Problem II

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

Total Submission(s): 308484    Accepted Submission(s): 59635

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
 
-----------------------------------------------------------------------------------------------------------------------------------------------
高精度加法计算,数值位数太大,用字符处理,按位计算,为了方便末尾对齐,采取先取反再相加在取反;
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
void un(char *a,int k) //取反
{
for(int i=0;i<k/2;i++)
swap(a[i],a[k-1-i]);
}
int main()
{
char a[1005],b[1005],c[1005],A[1005],B[1005];
int k1,k2,k;
int o;
while(~scanf(" %d",&o))
{
int t=0;
int q=o;
while(o--)
{
memset(c,'0',sizeof(c));
scanf(" %s %s",A,B);
strcpy(a,A);
strcpy(b,B);
k1=strlen(a);
k2=strlen(b);
un(a,k1);
un(b,k2);
if(k1>k2)//补零以对齐;
{
k=k1;
for(int i=k2;i<k;i++)
b[i]='0';
}
else
{
k=k2;
for(int i=k1;i<k;i++)
a[i]='0'; }
for(int i=0;i<k;i++)//相加和进位
{
c[i]=c[i]+(a[i]-'0')+(b[i]-'0');
if(c[i]>'9')
{
c[i]-=10;
c[i+1]+=1;
}
}
printf("Case %d:\n%s + %s = ",++t,A,B);
if(c[k]>'0') //最高位进位单独考虑
printf("%c",c[k]);
for(int i=k-1;i>=0;i--)
{
printf("%c",c[i]);
}
printf("\n");
if(t!=q)
printf("\n");
} }
return 0;
}

hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏的更多相关文章

  1. A simple problem 分类: 哈希 HDU 2015-08-06 08:06 1人阅读 评论(0) 收藏

    A simple problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...

  2. short-path problem (Floyd) 分类: ACM TYPE 2014-09-01 23:58 100人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  3. short-path problem (Dijkstra) 分类: ACM TYPE 2014-09-01 23:51 111人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  4. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  5. 多校赛3- Solve this interesting problem 分类: 比赛 2015-07-29 21:01 8人阅读 评论(0) 收藏

    H - Solve this interesting problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I ...

  6. Train Problem I 分类: HDU 2015-06-26 11:27 10人阅读 评论(0) 收藏

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  8. HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏

    Sum Problem Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. HDU 2101 A + B Problem Too 分类: ACM 2015-06-16 23:57 18人阅读 评论(0) 收藏

    A + B Problem Too Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. C++17尝鲜:在 if 和 switch 语句中进行初始化

    初始化语句 在C++17中,类似于 for 语句,在 if 和 switch 语句的判断条件之前也能加上初始化语句,语法形式如下: if (初始化语句; 条件) 语句 else 语句 switch ( ...

  2. Vue.js路由详解

    有时候,我们在用vue的时候会有这样的需求,比如一个管理系统,点了左边的菜单栏,右边跳转到一个新的页面中,而且刷新的时候还会停留在原来打开的页面. 又或者,一个页面中几个不同的画面来回点击切换,这两种 ...

  3. Git操作的一些注意

    这是在在学习Git时遇到的一些需要注意的地方,都是一些小细节的地方,可能会有错误的地方,希望大家可以指出谢谢   1.git使用,安装后,首先要打开git bash   2.必须登录后才可以操作git ...

  4. cdoj793-A Linear Algebra Problem

    http://acm.uestc.edu.cn/#/problem/show/793 A Linear Algebra Problem Time Limit: 3000/1000MS (Java/Ot ...

  5. 122. Best Time to Buy and Sell Stock II (Array;Greedy)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. C#.net随机数函数

    (1)Random rnd = new Random(); int rndNum = rnd.Next();           //int 取值范围内的随机数 int rndNum = rnd.Ne ...

  7. ssh登录忽略known_hosts列表

    编辑 ~/.ssh/config 如果不存在,可以先创建,并且要注意其读写权限 mkdir -p ~/.ssh touch ~/.ssh/config ~/.ssh/config 文件内容如下 Log ...

  8. linux-git服务搭建

    第一步,安装git: 源码安装参考:http://www.cnblogs.com/syuf/p/9151115.html 第二步,创建一个git用户,用来运行git服务: $ sudo adduser ...

  9. IPEndPoint

    .NET框架用IPEndPoint 对象来表示一个特定的IP地址和端口的组合,应用该对象的场景多是在讲socket绑定到本地地址或者将socket绑定到非本地地址.

  10. mvc 验证封装到某个特征类里[特性的使用]