高精度除单精度

上回讲了高精度乘单精度↙

戳着

然后现在讲讲除的
ans[]:5 4 3 2 1
y:5
除法由于那个余数,所以我们要从高位到低位来求。
一变:5 4 3 2 0(1/5) x=1*10(注意到了吗,要乘10,因为1在2眼中就是10!)
------->5 4 3 12 0 x=0
二变:5 4 3 2(12/5) 0 x=2*10
------->5 4 23 2 0 x=0
三变:5 4 4 2 0 x=3
------->5 34 4 2 0 x=0
四变:5 6 4 2 0 x=4
------->45 6 4 2 0 x=0
最后一变:9 6 4 2 0 x=0
结果变为2469(倒过来便是答案)
标就是压位了的(想要没压位的就自己把mo和输出的地方改改就行了)
输出 printf("%05d",ans.a[i]);
如果有疑问的话:↙

戳着

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define mo 100000
using namespace std;
struct node {int len,a[5011];}ans,c;

node cheng(node a,int y)
{
	c.len=a.len+2;
	for (int i=1,x=0;i<=c.len;i++)
	{
		c.a[i]=a.a[i]*y+x;
		x=c.a[i]/mo,c.a[i]%=mo;
	}
	while (!c.a[c.len]) c.len--;
	return c;
}

node divide(node a,int y)
{
	c.len=a.len;
	for (int i=c.len,x=0,h;i>0;i--)
	{
		h=(a.a[i]+x)%y;
		c.a[i]=(a.a[i]+x)/y;
		x=h*mo;
	}
	while (!c.a[c.len]) c.len--;
	return c;
}

int main()
{
	ans.a[1]=ans.len=1;
	for (int i=1;i<=100;i++)
		ans=cheng(ans,i);
	for (int i=1;i<=50;i++)
		ans=divide(ans,i);
	printf("%d",ans.a[ans.len]);
	for (int i=ans.len-1;i>0;i--)
		printf("%05d",ans.a[i]);
	return 0;
}
posted @ 2019-01-05 16:12  jz929  阅读(345)  评论(0编辑  收藏  举报