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

题意: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. [luogu3627 APIO2009] 抢掠计划 (tarjan缩点+spfa最长路)

    传送门 Description Input 第一行包含两个整数 N.M.N 表示路口的个数,M 表示道路条数.接下来 M 行,每行两个整数,这两个整数都在 1 到 N 之间,第 i+1 行的两个整数表 ...

  2. CentOS 笔记(四) Jexus部署相关

    ①设置jexus 为服务 cd /lib/systemd/system/ sudo vi jexus.service #注意 jexus 实际路径 [Unit] Description=jexus A ...

  3. js日期原型扩展

    当初做统计业务需要处理时间 周报:本周 上周 下周 近一周 月报上月 本月 等 需要使用时间处理 所以扩展了这些方法 <!DOCTYPE html> <html xmlns=&quo ...

  4. Servlet过滤器和监听器知识总结

    Servlet过滤器是 Servlet 程序的一种特殊用法,主要用来完成一些通用的操作,如编码的过滤.判断用户的登录状态.过滤器使得Servlet开发者能够在客户端请求到达 Servlet资源之前被截 ...

  5. 关于excel导入、导出(POI)

    当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中, ...

  6. HDU 4357

    这道题写起来没难度,但这种题确实很难,这种字符串的题难在证明.以后也要注意. 奇偶性不同的字符串肯定不能转换,因为每一次交换都是字符串的和增加2. 当字符串长度为2时,可以模拟交换,最多26次. 否则 ...

  7. APP为什么签名,使用keytool jarsigner进行签名

    签名(sign):在应用程序的特定字段写入特定的标记信息,表示该软件已经通过了签署者的审核.过程:使用私有密钥数字地签署一个给定的应用程序 作用: 识别应用程序作者 检測应用程序是否发生改变 有种程序 ...

  8. 王立平--poser

    Poser是Metacreations公司推出的一款lemmaId=234814&ss_c=ssc.citiao.link" style="color:rgb(51,102 ...

  9. 多线程编程TSL相关的技术文档

    线程本地存储 (TLS) https://msdn.microsoft.com/zh-cn/library/6yh4a9k1(v=vs.80).aspx Using Thread Local Stor ...

  10. swift-判断是否已获得相机、相册权限

    // 相机权限 func isRightCamera() -> Bool { let authStatus = AVCaptureDevice.authorizationStatus(forMe ...