如果是分享那就写分享,
如果是填坑 - 自己选的坑自己填。- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- typedef unsigned long long int ULL;
- const ULL BASE = 1e8;
- const ULL LEN = 8;
-
- vector<int> vec_plus(const vector<int>& a, const vector<int>& b);
- string vec2str( const vector<int> &vec );
-
- int main(int argc, char *argv[] )
- {
- //1000 段 99999999
- vector<int> va(1000, 99999999);
- vector<int> vb(1000, 99999999);
-
- vector <int> vc;
- //1000次加法 耗时测试
- for (int i = 0; i < 1000; i++) vc =vec_plus(va, vb);
- cout << vec2str( vc );
- return 0;
- }
-
- vector<int> vec_plus(const vector<int> &a, const vector<int> &b)
- {
- static int ia; // iter
- vector<int> c( a.size() );
- int t, pool=0, ib = b.size()-1;
- int v, r;
- for (ia = a.size()-1; ia >= 0; ia-- )
- {
- t = ib >= 0 ? (a[ia]) + (b[ib--]) + pool : (a[ia]) + pool;
- v = t % BASE, pool = t / BASE;
- c[ia] = v;
- }
- if ( pool > 0 ) c.insert(c.begin(), pool);
- return c;
- }
-
- string vec2str( const vector<int> &vec )
- {
- string s("");
- s += to_string( vec[0] );
- for ( int it = 1; it < vec.size(); it++ )
- s += to_string(vec[it]+BASE).substr(1, LEN);
- return s;
- }
复制代码
|