zoukankan      html  css  js  c++  java
  • 大数加减乘除取模运算模板

    #include <bits/stdc++.h>
    using namespace std;
    
    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;
    }
    
    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);
    }
    
    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;
    }
    
    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);
    }
    
    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;
    }
    
    string delZero(string s) {
        int len = s.length();
        string ans = "";
        int temp = -1;
        int flag = 0;
        if(s[0] == '-') flag = 1;
        for(int i = flag; i < len; i ++) {
            if(s[i] != '0') {
                temp = i;
                break;
            }
        }
        if(temp == -1)
            return "0";
        if(flag) ans += '-';
        for(int i = temp; i < len; i ++)
            ans += s[i];
        return ans;
    }
    
    int main() {
        string s, t;
        return 0;
    }
    

      

  • 相关阅读:
    【2017 Multi-University Training Contest
    【CS Round #39 (Div. 2 only) D】Seven-segment Display
    【CS Round #39 (Div. 2 only) C】Reconstruct Sum
    【CS Round #39 (Div. 2 only) B】Circle Elimination
    【CS Round #39 (Div. 2 only) A】Removed Pages
    【Uva 10163】Storage Keepers
    【Uva 1632】Alibaba
    【2017 Multi-University Training Contest
    Network Function Virtualization for a Network Device
    C#程序集相关的概念
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10664325.html
Copyright © 2011-2022 走看看