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

     1 /*
     2     高精度减法
     3     洛谷极端数据 最大位数开到100000
     4     若被减数小于减数 输出负号后交换减数与被减数 
     5     结构体初始化
     6     定义去除前导0函数 
     7     重载赋值符号 减号 小于号(用于比较大小)
     8     定义字符串输出函数
     9     重载输入输出流  
    10 */
    11 #include<bits/stdc++.h>
    12 using namespace std;
    13 const int maxn=100000;
    14 struct bign{
    15     int d[maxn],len;
    16     inline bign(){len=1;memset(d,0,sizeof(d));}
    17     inline void clean() {while(len>1&&!d[len-1]) len--;}
    18     inline bign operator = (const char* num)
    19     {
    20         memset(d,0,sizeof(d));
    21         len=strlen(num);
    22         for(int i=0;i<len;i++) d[i]=num[len-i-1]-48;
    23         return *this;
    24     }
    25     inline bign operator - (const bign &b)
    26     {
    27         bign c=*this;
    28         for(int i=0;i<c.len;i++)
    29         {
    30             c.d[i]-=b.d[i];
    31             if(c.d[i]<0) {c.d[i]+=10;c.d[i+1]--;};
    32         }
    33         c.clean();
    34         return c;
    35     }
    36     inline bool operator < (const bign &b) const
    37     {
    38         if(len!=b.len) return len<b.len;
    39         for(int i=0;i<len;i++) if(d[i]!=b.d[i]) return d[i]<b.d[i];
    40         return false;
    41     }
    42     inline string str() const
    43     {
    44         char s[maxn]={};
    45         for(int i=0;i<len;i++) s[len-i-1]=d[i]+48;
    46         return s;
    47     }
    48 }a,b;
    49 inline istream& operator >> (istream &in,bign &x)
    50 {
    51     char s[maxn]={};
    52     in>>s;
    53     x=s;
    54     return in;
    55 }
    56 inline ostream& operator << (ostream &out,const bign &x)
    57 {
    58     out<<x.str();
    59     return out;
    60 }
    61 int main()
    62 {
    63     cin>>a>>b;
    64     if(a<b) {swap(a,b);cout<<"-";}
    65     cout<<a-b;
    66     return 0;
    67 }
  • 相关阅读:
    LeetCode5 Longest Palindromic Substring
    LeetCode4 Median of Two Sorted Arrays
    LeetCode3 Longest Substring Without Repeating Characters
    LeetCode2 Add Two Numbers
    LeetCode1 Two Sum
    算法总结—深度优先搜索DFS
    c++ 设计模式9 (Abstract Factory 抽象工厂模式)
    题解 P3374 【【模板】树状数组 1】
    题解 HDU1565 【方格取数(1)】
    题解 P2431 【正妹吃月饼】
  • 原文地址:https://www.cnblogs.com/yu-xing/p/10125433.html
Copyright © 2011-2022 走看看