**链接:****传送门 **

题意:A + B 高精度,板子题


 /*************************************************************************
> File Name: hdu1002.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月16日 星期二 20时08分28秒
************************************************************************/ #include<bits/stdc++.h>
using namespace std; #define cls(x) memset(x,0,sizeof(x)) const int maxlen = 10000; // 单个“数字”最大长度
class HP{ // High Precision
public:
int len , s[maxlen];
HP(){ (*this) = 0; }; HP(int inte){ (*this) = inte; }; HP(const char*str){ (*this) = str; };
friend ostream & operator << (ostream & cout , const HP &x); // 定义高精度输出方式
HP operator = (int inte); HP operator = (const char*str); // 定义高精度与高精度的 + - * / % Compare(比较)
HP operator + (const HP &b); HP operator - (const HP &b);
HP operator * (const HP &b); HP operator / (const HP &b);
HP operator % (const HP &b); int Compare(const HP &b);
};
ostream & operator <<(ostream & cout, const HP &x){
for(int i = x.len ; i >= 1 ; i--) cout<< x.s[i]; return cout;
}
HP HP::operator = (int inte){
if( inte == 0 ){ len = 1; s[1] = 0; return (*this); };
for( len = 0 ; inte > 0 ;) { s[++len] = inte % 10 ; inte /= 10; };
return (*this);
}
HP HP::operator = (const char* str){
len = strlen(str);
for(int i = 1 ; i <= len ; i++) s[i] = str[len - i] - '0'; // 需要注意
return (*this);
}
HP HP::operator * (const HP &b){
int i , j; HP c; c.len = len + b.len;
for( i = 1 ; i <= c.len ; i++) c.s[i] = 0; // 清空返回值的数组
for( i = 1 ; i <= len ; i++) for( j = 1 ; j <= b.len ; j++) c.s[i+j-1] += s[i]*b.s[j];
for( i = 1 ; i < c.len ; i++){ c.s[i+1] += c.s[i] / 10 ; c.s[i] %= 10; }
while( c.s[i] ){ c.s[i+1] = c.s[i]/10 ; c.s[i] %= 10; i++; }
while( i>1 && !c.s[i] ) i--; c.len = i;
return c;
}
HP HP::operator + (const HP &b){
int i; HP c; c.s[1] = 0;
for( i = 1 ; i<=len || i<=b.len || c.s[i] ; i++ ){ // 模拟十进制加法运算,写的好呀!写的好!巧妙!
if( i <= len ) c.s[i] += s[i];
if( i <= b.len ) c.s[i] += b.s[i];
c.s[i+1] = c.s[i]/10; c.s[i] %= 10;
}
c.len = i - 1 ; if( c.len == 0 ) c.len = 1;
return c;
}
HP HP::operator - (const HP &b){
int i,j; HP c;
for( i = 1, j = 0; i <= len ; i++){
c.s[i] = s[i] - j; if(i <= b.len) c.s[i] -= b.s[i];
if( c.s[i] < 0 ){ j = 1; c.s[i] += 10; }
else j = 0;
}
c.len = len; while( c.len > 1 && !c.s[c.len] ) c.len--;
return c;
}
int HP::Compare(const HP &y){
if( len > y.len ) return 1;
if( len < y.len ) return -1;
int i = len;
while( (i>1) && (s[i]==y.s[i]) ) i--;
return s[i] - y.s[i]; // 如果 s[i] == y.s[i] 自然返回0,s[i] > y.s[i]返回1.....很巧妙!
}
HP HP::operator / (const HP &b){
int i,j; HP d(0) , c;
for( i = len ; i > 0 ; i--){
if( !(d.len==1 && d.s[1]==0) )
{ for( j = d.len ; j > 0 ; j--) d.s[j+1] = d.s[j]; ++d.len; }
d.s[1] = s[i]; c.s[i] = 0;
while( j = d.Compare(b) >= 0 ){
d = d - b; c.s[i]++; if(j==0) break;
}
}
c.len = len ; while( (c.len>1) && (c.s[c.len]==0) ) c.len--;
return c;
}
HP HP::operator % (const HP &b){
int i,j; HP d(0);
for( i = len ; i > 0 ; i--){
if( !(d.len==1 && d.s[1]==0) )
{ for( j = d.len ; j > 0 ; j--) d.s[j+1] = d.s[j]; ++d.len; }
d.s[1] = s[i];
while( j = d.Compare(b) >= 0 ){
d = d - b; if(j==0) break;
}
}
return d;
}
int main(){
int t,kase = 0;
HP a,b,c;
char s1[maxlen] , s2[maxlen];
scanf("%d",&t);
while(t--){
if( kase ) puts("");
printf("Case %d:\n",++kase);
cls(s1); cls(s2);
scanf("%s %s",s1,s2); a = s1; b = s2;
c = a + b;
cout<< a << " + " << b << " = " << c <<endl;
}
return 0;
}

HDU 1002 A + B Problem II( 高精度加法水 )的更多相关文章

  1. 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) ...

  2. HDU 1002 - A + B Problem II - [高精度]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...

  3. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

  4. hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. 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) ...

  6. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  7. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  8. 大数加法~HDU 1002 A + B Problem II

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...

  9. hdu 1002 A + B Problem II【大数加法】

    题目链接>>>>>> 题目大意:手动模拟大数加法,从而进行两个大数的加法运算 #include <stdio.h> #include <strin ...

随机推荐

  1. Vue学习之路第十三篇:v-for指令

    v-for指令,看名字想必大家也能猜到其作用,没错,就是用来迭代.遍历的. 1.简单数组的遍历 <body> <divi id="app"> <spa ...

  2. 自己对WEBGL坐标系的转换过程的理解【如图】

  3. php程序员需要撑握的知识点

    1. 基本知识点 HTTP协议中几个状态码的含义:1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码   说明 100   (继续) 请求者应当继续提出请求. 服务器返回此代码 ...

  4. Project Euler 42 Coded triangle numbers

    题意:三角形数序列的第n项由公式tn = 1/2n(n+1)给出:因此前十个三角形数是: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, - 将一个单词的每个字母分别转化为其 ...

  5. GIT文件的4种状态

  6. BA-给排水-供水系统自动控制(转载)

    浙江省建筑设计研究院划 杨绍胤 杨庆 摘 要:探讨供水系统变流量和恒压自动控制和设计方法.关键词: 供水系统 自动控制 传统给水系统常在屋顶设置高位水箱.水从地下水箱用水泵打到高位水箱.从高位水箱通过 ...

  7. Ubuntu14.04.1安装搜狗拼音输入法

    之前在Ubuntu16.04下安装过搜狗,在印象中与这次遇到的问题不一样,因此先说明一下这次Ubuntu的版本号: 参考博客http://blog.csdn.net/tao_627/article/d ...

  8. JeeSite(2):导入数据,进入系统

    本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/50954485 未经博主同意不得转载. 博主地址是:http://blog.csd ...

  9. VC 获取控制台窗体的句柄(hWnd)

    在Windows中,句柄是一个系统内部数据结构的引用. 比如当你操作一个窗体.或说是一个Delphi窗体时,系统会给你一个该窗体的句柄,系统会通知你:你正在操作142号窗体.就此你的应用程序就能要求系 ...

  10. OpenST Basic tool library

    /***************************************************************************** * OpenST Basic tool l ...