HDU 1002 - A + B Problem II - [高精度]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
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
题意:
给出 $A,B$,求 $A+B$。
题解:
数据范围太大,需要手工模拟加法。
AC代码:
#include<stdio.h>
#include<string.h>
#define max(a,b) ((a>b)?a:b)
int main()
{
char ta[],tb[];
int a[],b[],sum[],n,t,i,lena,lenb,lenmax;
scanf("%d",&n);
for(t=;t<=n;t++)
{
scanf("%s %s",ta,tb);
lena=strlen(ta);
lenb=strlen(tb);
lenmax=max(lena,lenb);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(sum,,sizeof(sum));
for(i=;i<=lena-;i++) a[i]=ta[lena--i]-'';
for(i=;i<=lenb-;i++) b[i]=tb[lenb--i]-'';
for(i=;i<=lenmax-;i++)
{
sum[i]+=(a[i]+b[i]);
if(sum[i]>=)
{
if(i==lenmax-) lenmax++;
sum[i]-=;
sum[i+]++;
}
}
printf("Case %d:\n",t);
printf("%s + %s = ",ta,tb);
for(i=lenmax-;i>=;i--) printf("%d",sum[i]); printf("\n");
if(t<n) printf("\n");
}
}
(默默把当年刚入坑时敲得C语言代码折叠)拿出我们的高精度板子!
#include<bits/stdc++.h>
using namespace std;
const int maxn=; struct BigInt
{
int len,d[maxn]; void clean(){while(len> && !d[len-]) len--;}
void output(){for(int i=len-;i>=;i--) printf("%d",d[i]);}
string str()const
{
string s;
for(int i=;i<len;i++) s+=d[len--i]+'';
return s;
} BigInt(){memset(d,,sizeof(d));len=;}
BigInt(int num){*this=num;}
BigInt(char* num){*this=num;} bool operator<(const BigInt& oth)const
{
if(len!=oth.len) return len<oth.len;
for(int i=len-;i>=;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i];
return false;
}
bool operator>(const BigInt& oth)const{return oth<*this;}
bool operator<=(const BigInt& oth)const{return !(oth<*this);}
bool operator>=(const BigInt& oth)const{return !(*this<oth);}
bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;}
bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);} BigInt operator=(const char* num)
{
memset(d,,sizeof(d));
len=strlen(num);
for(int i=;i<len;i++) d[i]=num[len--i]-'';
clean();
return *this;
}
BigInt operator=(int num)
{
char s[];
sprintf(s,"%d",num);
return *this=s;
}
BigInt operator+(const BigInt& oth)const
{
BigInt c;
c.len=max(len,oth.len);
for(int i=;i<=c.len;i++) c.d[i]=;
for(int i=;i<c.len;i++)
{
c.d[i]+=(i<len?d[i]:)+(i<oth.len?oth.d[i]:);
c.d[i+]+=c.d[i]/;
c.d[i]%=;
}
c.len+=(c.d[c.len]>);
c.clean();
return c;
}
BigInt operator-(const BigInt& oth)const
{
BigInt c=*this;
if(c<oth) printf("Produce negative number!\n");
int i;
for(i=;i<oth.len;i++)
{
c.d[i]-=oth.d[i];
if(c.d[i]<) c.d[i]+=, c.d[i+]--;
}
while(c.d[i]<) c.d[i++]+=, c.d[i]--;
c.clean();
return c;
}
BigInt operator*(const BigInt& oth)const
{
BigInt c;
for(int i=;i<len;i++) for(int j=;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j];
for(int i=;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+]+=c.d[i]/, c.d[i]%=;
c.clean();
return c;
}
BigInt operator/(const BigInt& oth)const
{
BigInt c=*this, r=;
for(int i=;i<len;i++)
{
r=r*+c.d[len--i];
int j;
for(j=;j<;j++) if(r<oth*(j+)) break;
c.d[len--i]=j;
r=r-oth*j;
}
c.clean();
return c;
}
BigInt operator%(const BigInt& oth)
{
BigInt r=;
for(int i=;i<len;i++)
{
r=r*+d[len--i];
int j;
for(j=;j<;j++) if(r<oth*(j+)) break;
r=r-oth*j;
}
return r;
}
BigInt operator+=(const BigInt& oth)
{
*this=*this+oth;
return *this;
}
BigInt operator*=(const BigInt& oth)
{
*this=*this*oth;
return *this;
}
BigInt operator-=(const BigInt& oth)
{
*this=*this-oth;
return *this;
}
BigInt operator/=(const BigInt& oth)
{
*this=*this/oth;
return *this;
}
}a,b; char A[maxn],B[maxn];
int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%s%s",A,B);
a=A, b=B;
printf("Case %d:\n",kase);
a.output();
printf(" + ");
b.output();
printf(" = ");
(a+b).output();
printf("%s",kase<T?"\n\n":"\n");
}
}
HDU 1002 - A + B Problem II - [高精度]的更多相关文章
- HDU 1002 A + B Problem II(高精度加法(C++/Java))
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- hdoj 1002 A + B Problem II 高精度 java
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- HDU 1002 A + B Problem II( 高精度加法水 )
链接:传送门 题意:A + B 高精度,板子题 /************************************************************************* & ...
- hdu 1002.A + B Problem II 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...
- 大数加法~HDU 1002 A + B Problem II
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...
- HDU 1002 A + B Problem II (大数加法)
题目链接 Problem Description I have a very simple problem for you. Given two integers A and B, your job ...
随机推荐
- SSD卡对redis的影响
原文地址:http://antirez.com/news/52 Hello! As promised today I did some SSD testing. The setup: a Linux ...
- android 获得View的高度
在一个activity中有一个textview,设置字数不同,如何能在打开这个activity时就及时获得这个textview在activity的高度,有利于我对textview的高度进行设置. ...
- 关于NSString的@""和nil时的判断方法
1.NSString *str = @"";该语句代表是一个空串,并且不为nil,占有内存空间 2.NSString *str = nil;该语句代表,str不指向任何对象,指针指 ...
- Socket网络编程--聊天程序(5)
上一小节我们讲了使用select来避免使用多进程的资源浪费问题.上次只是实现了从多个客户端发送数据给服务器端,接下来就要实现从服务器端发送数据给各个客户端. 使用select多路转换处理聊天程序2 c ...
- 菜鸟教程之工具使用(六)——让Maven项目直接在eclipse内部的Tomcat中运行
Hello,大家好,好久不见!最近终于安定下来了,可以静下心来写东西了.先写篇简单的,找找感觉.工具系列的本身就比较简单,没什么技术含量.因为说到底,工具只是辅助我们工作的,知道怎么用,然后剩下的就是 ...
- linux每日命令(26):Linux文件属性详解
Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...
- 【转】ubuntu16.04设置python3为默认及一些库的安装
原文:https://www.cnblogs.com/jokie/p/6933546.html Ubuntu默认Python为2.7,所以安装Python包时安装的为py2的包. 利用alternat ...
- 开源网络库ACE、Boost的ASIO、libevent、libev、ZeroMQ
开源C/C++网络库:ACE C++语言 跨平台Boost的ASIO C++语言 跨平台libevent C语言 主要支持linux,新版增加了对windows的IOC ...
- ubuntu14.04 中virtual box 加入 usb
参考两篇文章: 1.http://www.cnblogs.com/ericsun/archive/2013/06/10/3130679.html 2.http://www.360doc.com/con ...
- GDC2017 把“现实的天空”在游戏内再现【Forza Horizon 3】的天空表现
原文链接 http://game.watch.impress.co.jp/docs/news/1047800.html 完全表现出现实世界中各种偶然而不可预料的风景! [Forza Horiz ...