高精度之高精除以高精
来源:互联网 发布:世界语翻译的软件 编辑:程序博客网 时间:2024/06/02 18:01
Description
高精除以高精
Input
每组测试数据个栈2行,每行代表一个高精度整数(不超过100位)
Output
每组测试数据输出占2行,分别输出商和余数。
Sample Input
1405
Sample Output
280
思路:
高精除以高精是用减法模拟除法,对被除数的每一位都减去除数,一直减到当前位置的数字(包含前面的余数)小于除数(由于每一位的数字小于10,所以对于每一位最多进行10次计算).
代码:
#include<iostream>#include<string>#include<cstring>using namespace std;int newcopy(int a[],int b[],int n);int cmp(int a[],int b[]);int jianfa(int a[],int b[]);int print(int a[]);int chugao(int a[],int b[],int c[]);int main(){ string s1,s2; int a[110],b[110],c[110]; while(cin>>s1>>s2) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); a[0]=s1.length(); b[0]=s2.length(); for(int i=1;i<=a[0];i++)//倒序储存每一位数字 a[i]=s1[a[0]-i]-'0'; for(int i=1;i<=b[0];i++) b[i]=s2[b[0]-i]-'0'; chugao(a,b,c); print(c); print(a); } return 0;}int chugao(int a[],int b[],int c[]){ c[0]=a[0]-b[0]+1;//商的长度 for(int i=c[0];i>0;i--) { int temp[110]; memset(temp,0,sizeof(temp)); newcopy(b,temp,i); while(cmp(a,temp)>=0) { c[i]++; jianfa(a,temp); } } while(c[0]>0&&c[c[0]]==0) c[0]--; return 0;}int print(int a[]){ if(a[0]==0) cout<<0<<endl; else { for(int i=a[0];i>0;i--) cout<<a[i]; cout<<endl; } return 0;}int jianfa(int a[],int b[]){ if(cmp(a,b)==0) { a[0]=0; return 0; } if(cmp(a,b)==1) { for(int i=1;i<=a[0];i++) { if(a[i]<b[i]) { a[i+1]--; a[i]+=10; } a[i]-=b[i]; } while(a[0]>0&&a[a[0]]==0)//清除前导0 a[0]--; } return 0;}int newcopy(int a[],int b[],int n)//复制,逆序存放{ for(int i=1;i<=a[0];i++) b[i+n-1]=a[i]; b[0]=a[0]+n-1; return 0;}int cmp(int a[],int b[])//比较大小,因为后面会做减法{ if(a[0]>b[0]) return 1; if(a[0]<b[0]) return -1; for(int i=a[0];i>0;i--) { if(a[i]>b[i]) return 1;//a比b大 if(a[i]<b[i]) return -1;//a比b小 } return 0;//a b相等}
1 0
- 高精度之高精除以高精
- 高精度之高精度除法(高精除以高精)
- 高精度之高精度除法(高精除以低精)
- 高精度之高精除以低精取余(天使的起誓)
- 高精度除法(高精除以低精)——一步一步算法篇
- 高精度之高精除低精
- 高精度除法(高精除高精)
- 高精度除以低精精度数
- 【高精度模板】【高精加减乘没有除】
- 高精度加法 洛谷 P1601 A+B Problem(高精)
- 高精度——A+B Problem(高精)
- 高精度系列——反正高精除高精我不会
- 高精度除以高精度
- 高精度除法(高除高)
- 高精
- pascal 高精度压位(加减乘&高精除单精)
- BZOJ 1213 HNOI2004 高精度开根 二分+高(Py)精(thon)度
- 【给将来学神的算法详解--高精】(1)(我的)高精度模板
- c语言写高斯消去法
- svn和git的区别,maven是什么
- Linux系统编程第11章-系统限制和选项
- 排序算法---基数排序
- k-d树查询算法的简要说明:
- 高精度之高精除以高精
- 【Java】Date类
- Python语法整理
- Java基础知识集合
- error:C1189
- 二叉树的前序遍历
- Effective Java 2nd笔记第二章第五条:避免使用终结方法
- Leetcode/array/Container With Most Water
- 单例模式-懒汉式