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 ...
 
随机推荐
- asp.net C# int 类型在32/64位环境下取值范围无变化
			
最近在学习中突然想到,我在64位环境下,int取值范围是否有变化?为了检测这个结果,我做了以下这个测试:1.环境:win7旗舰版64位+vs2010 sp1(版本号:10.0.40219.1SP1Re ...
 - QT和MFC的差别
			
QT和MFC的差别 在使用MFC之前就已经使用Qt这个事实可能影响了我的客观性. (MFC效率较高,但大量的Windows API和消息机制使得其较难理解,不易用:QT封装较好,易用且跨平台,但效率较 ...
 - Android Studio updating indices 一直刷新和闪烁
			
Android Studio 更新到了 3.1.3 版本,在导入了工程以后,一直出现了 updating indices 刷新的情况,造成闪烁,在切换到其他视图以后,Android Studio 会一 ...
 - [docker]docker自带的overlay网络实战
			
overlay网络实战 n3启动consul docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -b ...
 - 关于es6中的yield
			
<!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...
 - Xbox One手柄 + Xbox Wireless Adapter PC无线适配器驱动安装、配对全流程
			
以下步骤在Windows 7系统中操作.XBox One手柄+无线适配器并非仅只能在Windows 10中使用. 一点感想:微软的XBoxOne手柄实在是好东西,但产品使用说明与文档实在太垃圾,翻遍官 ...
 - 扩展layui中的自带字体图标
			
项目中,虽然layui的字体图标库中已经有了1000多个图标了,但是也有时候不能满足我们自定义图标的需求,所以需要进行自定义,具体步骤如下: 1.在iconfont上找到自己喜欢的图标,也可以上传ui ...
 - 【原】使用Tkinter绘制GUI并结合Matplotlib实现交互式绘图
			
在数据分析的过程中,往往需要对所建立的模型进行可视化,并调整其中的某些参数. 通常情况下,在Python中可以通过Matplotlib来进行绘制图像.然而该绘制过程是静态的,也就是每次调整完参数需要重 ...
 - linux中的信号机制
			
概述 Linux信号机制是在应用软件层次上对中断机制的一种模拟,信号提供了一种处理异步事件的方法,例如,终端用户输入中断键(ctrl+c),则会通过信号机制停止一个程序[1]. 这其实就是向那个程序( ...
 - Linux系统备份与恢复
			
序言:前面一篇文章简单地介绍了Linux系统备份与恢复的相关概念,这里接着上一篇介绍两个常用的备份与恢复命令. 1 常见的备份命令 在介绍下面的备份恢复命令之前先简单的说明一下: 如果我们只是要实现 ...