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

    题目描述:

    就是计算A*B(高精)啦。

    代码如下

    a=int(input())
    b=int(input())
    printf(a*b)

    当然这是python语言(python真好

    下面正题

    我们先模拟一下高精度乘法:

        2 6 2

       x  2 2 2   

           5 2 4

        5 2 4

        5 2 4         

        5 8 1 6 4

    是不是有写明白了呢,事实上高精度就有几分返璞归真的感觉,直接回到竖式乘法。

    我们先用把输入的字符串s1,s2转化为数组a,b。

    用数组c保存a,b数组的每一位乘积,即用数组c[i+j-1]位保存a[i]*b[j]。

    这个时候我们得到len为c数组长度。

    将c数组进位。

    最后倒着输出即好。

    下面是代码:

    #include<bits/stdc++.h>
    using namespace std;
    string s1,s2;
    int len;
    int a[10001],b[10001],c[10001];
    int main()
    {
    	cin>>s1;
    	cin>>s2;
    	int l1=s1.length();
    	int l2=s2.length();
    	for(int i=0; i<l1; i++)//转为数组
    	{
    		a[l1-i]=int(s1[i]-48);
    	}
    	for(int i=0; i<l2; i++)//转为数组
    	{
    		b[l2-i]=int(s2[i]-48);
    	}
    	for (int i=1; i<=l1; ++i)
    	for (int j=1; j<=l2; ++j)
    	c[i+j-1]+=a[i]*b[j];//将a[i]*b[j]保存在c[i+j-1]中
    	len=l1+l2;//获取c数组长度
    	for (int i=1; i<len; ++i)
    		if (c[i]>9)//进位
    		{
    			c[i+1]+=c[i]/10;
    			c[i]%=10;
    		}
    	while (c[len]==0&&len>1)//特判最前面是否有0
    	len--;
    	for (int i=len; i>=1; --i)//倒着输出
    	cout <<c[i];
    	return 0;
    }
    

      

  • 相关阅读:
    小程序解析html(使用wxParse)
    error: the HTTP rewrite module requires the PCRE library
    CMake Error at cmake/boost.cmake:76 (MESSAGE)
    Centos7安装mysql后无法启动,提示 Unit mysql.service failed to load:No such file or directory
    mysql的三种安装方式
    epel源
    cmake
    yum
    wget
    tar指令
  • 原文地址:https://www.cnblogs.com/yigejuruo/p/10409106.html
Copyright © 2011-2022 走看看