zoukankan      html  css  js  c++  java
  • 高精度模版

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define int long long
    inline int read()
    {
    int ans = 0,op = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
    if(ch == '-') op = -1;
    ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
    (ans *= 10) += ch - '0';
    ch = getchar();
    }
    return ans * op;
    }
    #define E 10050
    struct bign
    {
    int len,v[E];
    bign operator + (const bign& b) const
    {
    bign c;
    c.len = max(len,b.len);
    for(int i = 0;i <= c.len + 2;i++) c.v[i] = 0;
    for(int i = 0;i < c.len;i++) c.v[i] = v[i] + b.v[i];
    for(int i = 0;i < c.len;i++) c.v[i + 1] += c.v[i] / 10,c.v[i] %= 10;
    if(c.v[c.len]) c.len++;
    return c;
    }
    bign operator * (const bign& b) const
    {
    bign c;
    c.len = len + b.len + 1;
    for(int i = 0;i <= c.len + 2;i++) c.v[i] = 0;
    for(int i = 0;i < len;i++)
    for(int j = 0;j < b.len;j++)
    {
    c.v[i + j] += v[i] * b.v[j];
    c.v[i + j + 1] += c.v[i + j] / 10,c.v[i + j] %= 10;
    }
    while(c.v[c.len] == 0) c.len--;
    c.len++;
    return c;
    }
    bool operator < (const bign& b) const
    {
    if(len > b.len) return 0;
    if(len < b.len) return 1;
    for(int i = len - 1;i >= 0;i--) 
    {
    if(v[i] > b.v[i]) return 0;
    if(v[i] < b.v[i]) return 1;
    }
    }
    bool operator > (const bign &b) const 
    {
    if(len > b.len) return 1;
    if(len < b.len) return 0;
    for(int i = len - 1;i >= 0;i--) 
    {
    //printf("%lld %lld %lld
    ",i,v[i],b.v[i]);
    if(v[i] > b.v[i]) return 1;
    if(v[i] < b.v[i]) return 0;
    }
    return 0;
    }
    bool operator <= (const bign& b) const
    {
    return !(*this > b); 
    }
    bool operator >= (const bign& b) const
    {
    return !(*this < b); 
    }
    bool operator == (const bign& b) const
    {
    return *this <= b && b <= *this; 
    }
    bign operator = (char* s)
    {
    len = strlen(s);
    memset(v,0,sizeof(v));
    for(int i = 0;i < len;i++) v[i] = s[len - i - 1] - '0';
    return *this;
    }
    bign operator = (int x) 
    {
    char s[E]; 
    sprintf(s,"%lld",x);
    *this = s;
    return *this;
    }
    bign operator += (const bign& b) 
    { 
    return *this = *this + b;
    }
    bign operator / (int x)
    {
    bign c;
    c.len = len;
    int res = 0;
    for(int i = len - 1;i >= 0;i--)
    {
    res = res * 10 + v[i];
    c.v[i] = res / x;
    res %= x;
    }
    while(!c.v[c.len - 1] && c.len > 0) c.len--;
    return c;
    }
    };
    inline void print(bign a)
    {
    for(int i = a.len - 1;i >= 0;i--) printf("%lld",a.v[i]);
    //cout << endl;
    printf(" " );
    }
    main()
    {
    return 0;
    }
    View Code
  • 相关阅读:
    kvm
    Javascript 笔记与总结(2-7)对象
    [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes
    Swift5.3 语言指南(五) 基本运算符
    C#6.0语言规范(一) 介绍
    [Swift]LeetCode171. Excel表列序号 | Excel Sheet Column Number
    [Swift]LeetCode169. 求众数 | Majority Element
    [Swift]LeetCode168. Excel表列名称 | Excel Sheet Column Title
    [Swift]LeetCode167. 两数之和 II
    [Java]LeetCode141. 环形链表 | Linked List Cycle
  • 原文地址:https://www.cnblogs.com/LM-LBG/p/10484318.html
Copyright © 2011-2022 走看看