zoukankan      html  css  js  c++  java
  • 游记(4)

    0  数组存在的目的不仅是为了操作数据,更是为了将数据保存起来;

    1 在程序中不确定数据的大小的情况下,可以申请一个较大的数组,比如 512,1024 之类,一般不会对系统有影响,但是注意,这种大数组不要在 main 函数内部声明,而是要拿到 main 函数外面申请,不然会引发程序 终止。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<vector>
     4 #include<iostream>
     5 using namespace std;
     6 
     7 struct BigInteger {
     8   static const int BASE = 100000000;
     9   static const int WIDTH = 8;
    10   vector<int> s;
    11 
    12   BigInteger(long long num = 0) { *this = num; } // 构造函数
    13   BigInteger operator = (long long num) { // 赋值运算符
    14     s.clear();
    15     do {
    16       s.push_back(num % BASE);
    17       num /= BASE;
    18     } while(num > 0);
    19     return *this;
    20   }
    21   BigInteger operator = (const string& str) { // 赋值运算符
    22     s.clear();
    23     int x, len = (str.length() - 1) / WIDTH + 1;
    24     for(int i = 0; i < len; i++) {
    25       int end = str.length() - i*WIDTH;
    26       int start = max(0, end - WIDTH);
    27       sscanf(str.substr(start, end-start).c_str(), "%d", &x);
    28       s.push_back(x);
    29     }
    30     return *this;
    31   }
    32   BigInteger operator + (const BigInteger& b) const {
    33     BigInteger c;
    34     c.s.clear();
    35     for(int i = 0, g = 0; ; i++) {
    36       if(g == 0 && i >= s.size() && i >= b.s.size()) break;
    37       int x = g;
    38       if(i < s.size()) x += s[i];
    39       if(i < b.s.size()) x += b.s[i];
    40       c.s.push_back(x % BASE);
    41       g = x / BASE;
    42     }
    43     return c;
    44   }
    45 };
    46 
    47 ostream& operator << (ostream &out, const BigInteger& x) {
    48   out << x.s.back();
    49   for(int i = x.s.size()-2; i >= 0; i--) {
    50     char buf[20];
    51     sprintf(buf, "%08d", x.s[i]);
    52     for(int j = 0; j < strlen(buf); j++) out << buf[j];
    53   }
    54   return out;
    55 }
    56 
    57 istream& operator >> (istream &in, BigInteger& x) {
    58   string s;
    59   if(!(in >> s)) return in;
    60   x = s;
    61   return in;
    62 }
    63 
    64 #include<set>
    65 #include<map>
    66 set<BigInteger> s;
    67 map<BigInteger, int> m;
    68 
    69 int main() {
    70   BigInteger y;
    71   BigInteger x = y;
    72   BigInteger z = 123;
    73 
    74   BigInteger a, b;
    75   cin >> a >> b;
    76   cout << a + b << "
    ";
    77   cout << BigInteger::BASE << "
    ";
    78   return 0;
    79 }

    分析一下这个代码:

    把大整数 8 位 这么宽截取,并且存放在vector <int> s 里面,如1234567890,放在 vector 里面就是 s = { 34567890, 12 },支持整数x=1234567890,或者 x= "1234567890",这样输入输出代码,第一个 operator = 没什么可讲的,没有重载,第二个处理字符串的时候,substr( int start,int count) ,start 是截取的起始地址, count 是截取的数量,“%d”,&x 分别是sscanf 的函数参数,代表了格式和输出的地址。+ 求和中 g = x / BASE,是算了低位数字的和之后得到 进位,然后算高位的和,有进位则加上去。

  • 相关阅读:
    【Luogu1095】守望者的逃离
    python基础学习1-类相关内置函数
    python基础学习1-面向对象
    python基础学习1 -异常捕获
    python基础学习1-类,对象
    python基础学习1-正则表达式
    python基础学习1-反射
    python基础学习1-日志信息
    python基础学习1-生成器,递归函数
    python基础学习1-json,pickle的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/hanxinle/p/7892084.html
Copyright © 2011-2022 走看看