zoukankan      html  css  js  c++  java
  • 大数模板——六种实现了加减乘除和求余

    六种实现了加减乘除和求余

    1、大数加法

    string add(string a,string b)
    {
        string c;
        int len1=a.length();
        int len2=b.length();
        int len=max(len1,len2);
        for(int i=len1;i<len;i++)
            a="0"+a;
        for(int i=len2;i<len;i++)
            b="0"+b;
        int ok=0;
        for(int i=len-1;i>=0;i--)
        {
            char temp=a[i]+b[i]-'0'+ok;
            if(temp>'9')
            {
                ok=1;
                temp-=10;
            }
            else ok=0;
            c=temp+c;
        }
        if(ok) c="1"+c;
        return c;
    }
    View Code

    2、大数减法

    string sub(string a,string b)
    {
        string c;
        bool ok=0;
        int len1=a.length();
        int len2=b.length();
        int len=max(len1,len2);
        for(int i=len1;i<len;i++)
            a="0"+a;
        for(int i=len2;i<len;i++)
            b="0"+b;
        if(a<b)
        {
            string temp=a;
            a=b;
            b=temp;
            ok=1;
        }
        for(int i=len-1;i>=0;i--)
        {
            if(a[i]<b[i]) 
            {
                a[i-1]-=1;
                a[i]+=10;
            }
            char temp=a[i]-b[i]+'0';
            c=temp+c;
        }
        int pos=0;
        while(c[pos]=='0' && pos<len) pos++;
        if(pos==len) return "0"; 
        if(ok) return "-"+c.substr(pos);
        return c.substr(pos);
    }
    View Code

    3、大数乘法

    string mul(string a,int b)
    {
        string c;
        char s;
        int len=a.length();
        int ok=0;
        for(int i=len-1;i>=0;i--)
        {
            int temp=(a[i]-'0')*b+ok;
            ok=temp/10;
            s=temp%10+'0';
            c=s+c;
        }
        while(ok)
        {
            s=ok%10+'0';
            c=s+c;
            ok/=10;
        }
        return c;
    }
    View Code

    4、大数除法

    string div(string a,int b)
    {
        string c;
        int len=a.length();
        int ans=0;
        char s;
        for(int i=0;i<len;i++)
        {
            ans=ans*10+a[i]-'0';
            s=ans/b+'0';
            ans%=b;
            c+=s;
        }
        int pos=0;
        while(pos<len && c[pos]=='0') pos++;
        if(pos==len) return "0";
        return c.substr(pos);
    }
    View Code

     

    5、大数取模

    int mod(string s,int x)
    {
        int len=s.length();
        int ans=0;
        for(int i=0;i<len;i++)
        {
            ans=(ans*10+s[i]-'0')%x;
        }
        return ans;
    }
    View Code

     

    6、大数判断相等

    string solve(string s)
    {
        int len=s.length();
        bool ok=false;
        int x=0,y=len-1;
        for(int i=0;i<len;i++)
        {
            if(s[i]=='.')
            {
                ok=true;
                break;
            }
        }
        if(ok)
        {
            for(;y>=x;y--)
                if(s[y]!='0') break;
            if(s[y]=='.') y--;
        }
        for(;x<len-1;x++)
            if(s[x]!='0') break;
        if(s[x]=='.') x--;
        return s.substr(x,y-x+1);
    }
    
    bool equal(string s1,string s2)
    {
        if(s1[0]=='-' && s2[0]=='-') 
        {
            s1=s1.substr(1);
            s2=s2.substr(1);
        }
        else if(s1[0]=='-' || s2[0]=='-')
        {
            if(s1[0]=='-') s1=s1.substr(1);
            if(s2[0]=='-') s2=s2.substr(1);
            s1=solve(s1);
            s2=solve(s2);
            if(s1=="0" && s2=="0") return true;
            return false;
        }
        if(s1[0]=='+') s1=s1.substr(1);
        if(s2[0]=='+') s2=s2.substr(1);
        s1=solve(s1);
        s2=solve(s2);
        if(s1==s2) return true;
        return false;
    }
    View Code
  • 相关阅读:
    javascript通用函数库
    Nginx的负载均衡的那点事
    四种多服务器共享session的方法
    Nginx 配置文件nginx.conf的完整配置说明<转>
    Struts2标签问题-using Struts tags without the associat
    转:ibatis的cacheModel
    大型互联网网站架构心得之一
    nginx的upstream(解决session问题)
    rails3项目解析之1——系统架构
    使用 Nginx 提升网站访问速度
  • 原文地址:https://www.cnblogs.com/www-cnxcy-com/p/5695432.html
Copyright © 2011-2022 走看看