zoukankan      html  css  js  c++  java
  • 高精度乘单精度

    高精度乘单精度其实很简单的

    没压位的:
    举个例子,12345*5
    ans[]: 5 4 3 2 1
    y:5
    一变:25 4 3 2 1 x=0
    ------->5(25%10) 4 3 2 1 x=2(25/10)
    二变:5 22 3 2 1 x=0
    ------->5 2(22%10) 3 2 1 x=2(22/10)
    三变:5 2 17 2 1 x=0
    ------->5 2 7 2 1 x=1
    四变:5 2 7 11 1 x=0
    ------->5 2 7 1 1 x=1
    五变:5 2 7 1 6 x=0
    ------->5 2 7 1 6 x=0
    结果变为:61725(倒过来便是答案)

    注意,这时候的x可能会大于0,也就是说要进位!!!(997*233=232301)具体看代码

    输出 printf("%05d",ans.a[i]);
    如果有疑问的话:↙

    戳着

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define ll long long
    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]/10,c.a[i]%=10;
    	}
    	while (!c.a[c.len]) c.len--;
    	return c;
    }
    
    int main()
    {
    	ans.len=ans.a[1]=1;
    	for (int i=1;i<=100;i++)
    		ans=cheng(ans,i);
    	for (int i=ans.len;i>0;i--)
    		printf("%d",ans.a[i]);
    	return 0;
    }
    

    压位的便和不压位的差不多,就是。。。
    ans[]:12345
    y:5
    一变:61725 x=0
    一次过,快了很多哦哦哦~
    压位的:

    #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;
    }
    
    int main()
    {
    	ans.len=ans.a[1]=1;
    	for (int i=1;i<=100;i++)
    		ans=cheng(ans,i);
    	printf("%d",ans.a[ans.len]);
    	for (int i=ans.len-1;i>0;i--)
    		printf("%05d",ans.a[i]);
    	return 0;
    }
    

    我想,你应该学会了吧?(别白费我打了那么久哦~)

    转载需注明出处。
  • 相关阅读:
    HDU 1711
    HDU 4135
    HDU 4462
    HDU 1969
    flask的nocache防止js不刷新
    python2.x里unicode错误问题
    使用SwingWork反而阻塞SwingUI
    利用JProfile 7分析内存OOM
    编译android的一些坑
    java jmenu的替代方案
  • 原文地址:https://www.cnblogs.com/jz929/p/11817755.html
Copyright © 2011-2022 走看看