高精度 a±b的实现
来源:互联网 发布:中国工控网plc编程 编辑:程序博客网 时间:2024/06/07 16:48
- 1.高精度a+b的实现
相关题目:hdoj1002
代码:
#include<cstdio> #include<string.h>#include<algorithm>#include<stdlib.h>#include<iostream>using namespace std;char a[10005];char b[10005];int a0[10005];int b0[10005];int store[10005];int main(){ int t; int i,j; scanf("%d",&t); getchar(); int num=1; int p=t; while(p--) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(a0,0,sizeof(a0)); memset(b0,0,sizeof(b0)); memset(store,0,sizeof(store)); scanf("%s",a); scanf("%s",b); int ans=0; for(i=strlen(a)-1;i>=0;i--) { a0[i]=a[ans++]-'0'; } ans=0; for(i=strlen(b)-1;i>=0;i--) { b0[i]=b[ans++]-'0'; } int judge=0; int cnt=0; if(strlen(a)>strlen(b)) { cnt=strlen(a)-1; judge=1; } else cnt=strlen(b)-1; if(judge) { for(i=0;i<strlen(a);i++) { store[i]=store[i]+(a0[i]+b0[i]); if(store[i]>=10) { store[i+1]++; store[i]=store[i]%10; } } } else { for(i=0;i<strlen(b);i++) { store[i]=store[i]+(a0[i]+b0[i]); if(store[i]>=10) { store[i+1]++; store[i]=store[i]%10; } } } printf("Case %d:\n",num++); for(i=strlen(a)-1;i>=0;i--) printf("%d",a0[i]); printf(" + "); for(i=strlen(b)-1;i>=0;i--) printf("%d",b0[i]); printf(" = "); for(i=cnt;i>=0;i--) printf("%d",store[i]); if(p!=0) printf("\n\n"); else printf("\n"); } return 0;}//0 85858585585
- 2.高精度a-b的实现:
代码:
#include<cstdio>#include<iostream>#include<stdlib.h>#include<string>#include<string.h>using namespace std;int a[1005],b[1005];int c[1005];int main(){ int i,j; string s1,s2; while(cin>>s1>>s2) { if(s1==s2) { printf("0\n"); continue; } memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); int s1_leng,s2_leng; s1_leng=s1.length(); s2_leng=s2.length(); int judge=0; //处理结果是正数 if(s2_leng>s1_leng)judge=1; //处理结果是负数 /* * 把输入的字符串倒置 * 从个位数开始处理 * */ int cnt=1,tot=1; for(i=s1_leng-1;i>=0;i--) { a[cnt++]=s1[i]-'0'; } cnt--;//a.length() for(i=s2_leng-1;i>=0;i--) { b[tot++]=s2[i]-'0'; } tot--;//b.length() //长度相同时特判 if(s1_leng==s2_leng) { for(i=tot;i>=1;i--) //从最高位开始判断 { if(a[i]<b[i]) { judge=1; //a<b break; } else { break; } } } if(judge)printf("-"); if(!judge)//a.length()>b.length() + { for(i=1;i<=tot;i++) { if(a[i]<b[i]) { a[i]+=10; a[i+1]-=1; } a[i]-=b[i]; } for(i=cnt;i>=1;i--) { if(a[i]!=0) { for(j=i;j>=1;j--) { printf("%d",a[j]); } break; } } printf("\n"); } else//b.length()>a.length() - { for(i=1;i<=cnt;i++) { if(b[i]<a[i]) { b[i]+=10; b[i+1]-=1; } b[i]-=a[i]; } for(i=tot;i>=1;i--) { if(b[i]!=0) { for(j=i;j>=1;j--) { printf("%d",b[j]); } break; } } printf("\n"); } } return 0;}/*18738171873819*/
0 0
- 高精度 a±b的实现
- 高精度 a*b的实现
- 1381 a*b 高精度
- 高精度大数A+B
- A+B Problem 高精度
- A + B高精度
- a+b(高精度)
- 【高精度算法】A*B
- 【高精度算法】A+B
- 高精度 A*B Problem
- 高精度 A+B Problem
- 高精度 A-B Problem
- a+b高精度
- A+B高精度问题
- 【高精度算法】A+B
- a+b 高精度
- 高精度求A+B
- HDU 1402 A * B Problem Plus(FFT实现高精度乘法)
- 40-变量的value和type存储
- 学习笔记一(List)
- Python实现欧拉计划第十题
- 41-全局变量Global
- Pycharm快捷键
- 高精度 a±b的实现
- sublime 设置背景透明度
- 42-变量类型的转换
- iOS应用层架构之CDD
- 43-内存管理开篇
- 话谈 iOS 目录结构的划分
- HDU 5671 Matrix(思维题目,记录变化)
- 44-Zend内存管理器
- 誤差逆伝播法(バック・プロパゲーション)