• 高精度模板


    C++类,可以进行正整数之间的加减乘除,模,大小比较。输入输出用cin,cout;

      1 #include <stdio.h>
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <cstdlib>
      5 #include <cstring>
      6 #include <string>
      7 #include <algorithm>
      8 using namespace std;
      9 
     10 const int MAXN = 410;
     11 
     12 struct bign
     13 {
     14     int len, s[MAXN];
     15     bign ()
     16     {
     17         memset(s, 0, sizeof(s));
     18         len = 1;
     19     }
     20     bign (int num) { *this = num; }
     21     bign (const char *num) { *this = num; }
     22     bign operator = (const int num)
     23     {
     24         char s[MAXN];
     25         sprintf(s, "%d", num);
     26         *this = s;
     27         return *this;
     28     }
     29     bign operator = (const char *num)
     30     {
     31         for(int i = 0; num[i] == '0'; num++) ;  //去前导0
     32         len = strlen(num);
     33         for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
     34         return *this;
     35     }
     36     bign operator + (const bign &b) const //+
     37     {
     38         bign c;
     39         c.len = 0;
     40         for(int i = 0, g = 0; g || i < max(len, b.len); i++)
     41         {
     42             int x = g;
     43             if(i < len) x += s[i];
     44             if(i < b.len) x += b.s[i];
     45             c.s[c.len++] = x % 10;
     46             g = x / 10;
     47         }
     48         return c;
     49     }
     50     void clean()
     51     {
     52         while(len > 1 && !s[len-1]) len--;
     53     }
     54     bign operator * (const bign &b) //*
     55     {
     56         bign c;
     57         c.len = len + b.len;
     58         for(int i = 0; i < len; i++)
     59         {
     60             for(int j = 0; j < b.len; j++)
     61             {
     62                 c.s[i+j] += s[i] * b.s[j];
     63             }
     64         }
     65         for(int i = 0; i < c.len; i++)
     66         {
     67             c.s[i+1] += c.s[i]/10;
     68             c.s[i] %= 10;
     69         }
     70         c.clean();
     71         return c;
     72     }
     73     bign operator - (const bign &b)
     74     {
     75         bign c;
     76         c.len = 0;
     77         for(int i = 0, g = 0; i < len; i++)
     78         {
     79             int x = s[i] - g;
     80             if(i < b.len) x -= b.s[i];
     81             if(x >= 0) g = 0;
     82             else
     83             {
     84                 g = 1;
     85                 x += 10;
     86             }
     87             c.s[c.len++] = x;
     88         }
     89         c.clean();
     90         return c;
     91     }
     92     bign operator / (const bign &b)
     93     {
     94         bign c, f = 0;
     95         for(int i = len-1; i >= 0; i--)
     96         {
     97             f = f*10;
     98             f.s[0] = s[i];
     99             while(f >= b)
    100             {
    101                 f -= b;
    102                 c.s[i]++;
    103             }
    104         }
    105         c.len = len;
    106         c.clean();
    107         return c;
    108     }
    109     bign operator % (const bign &b)
    110     {
    111         bign r = *this / b;
    112         r = *this - r*b;
    113         return r;
    114     }
    115     bool operator < (const bign &b)
    116     {
    117         if(len != b.len) return len < b.len;
    118         for(int i = len-1; i >= 0; i--)
    119         {
    120             if(s[i] != b.s[i]) return s[i] < b.s[i];
    121         }
    122         return false;
    123     }
    124     bool operator > (const bign &b)
    125     {
    126         if(len != b.len) return len > b.len;
    127         for(int i = len-1; i >= 0; i--)
    128         {
    129             if(s[i] != b.s[i]) return s[i] > b.s[i];
    130         }
    131         return false;
    132     }
    133     bool operator == (const bign &b)
    134     {
    135         return !(*this > b) && !(*this < b);
    136     }
    137     bool operator != (const bign &b)
    138     {
    139         return !(*this == b);
    140     }
    141     bool operator <= (const bign &b)
    142     {
    143         return *this < b || *this == b;
    144     }
    145     bool operator >= (const bign &b)
    146     {
    147         return *this > b || *this == b;
    148     }
    149     string str() const
    150     {
    151         string res = "";
    152         for(int i = 0; i < len; i++) res = char(s[i]+'0') + res;
    153         return res;
    154     }
    155 };
    156 
    157 istream& operator >> (istream &in, bign &x)
    158 {
    159     string s;
    160     in >> s;
    161     x = s.c_str();
    162     return in;
    163 }
    164 
    165 ostream& operator << (ostream &out, const bign &x)
    166 {
    167     out << x.str();
    168     return out;
    169 }
    View Code

    JAVA BigInteger类。

    函数:

    valueOf(parament); 将参数转换为制定的类型

    比如 int a=3;

      BigInteger b=BigInteger.valueOf(a);

      则b=3;

      String s=”12345”;

      BigInteger c=BigInteger.valueOf(s);

      则c=12345;

    add();  subtract();  multiply();  divide();  pow();

    gcd();  abs();  mod();  max();  min();  equals();

     1 import java.math.BigInteger;
     2 import java.util.Scanner;
     3 public class Main {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         BigInteger a,b,c;
     8         Scanner cin=new Scanner(System.in);
     9         while(cin.hasNext())
    10         {
    11             a=cin.nextBigInteger();
    12             b=cin.nextBigInteger();
    13             c=a.max(b);
    14             System.out.println(c);
    15         }
    16     }
    17 
    18 }
    View Code

     参考文章:http://blog.csdn.net/wall_f/article/details/8373395

         http://blog.sina.com.cn/s/blog_622bd1660100oqpv.html

  • 相关阅读:
    linux 常用命令大全
    socket的读写函数readn和writen函数
    python中exec 和eval的用法
    python中set集合介绍
    python中下划线变量的规则和意义
    关于小端字节序和大端字节序的解释
    需要学习的网站
    关于尾递归节省内存空间
    python中的with语句
    python中的类变量和对象变量,以及传值传引用的探究
  • 原文地址:https://www.cnblogs.com/L-King/p/5431234.html
Copyright © 2020-2023  润新知