高精——模板

紫書:

#include <iostream>  
#include <string>  
#include <cstring>  
#include <cstdio>  
using namespace std;  const int maxn = 1000;  struct bign{  int d[maxn], len;  void clean() { while(len > 1 && !d[len-1]) len--; }  bign()          { memset(d, 0, sizeof(d)); len = 1; }  bign(int num)   { *this = num; }   bign(char* num) { *this = num; }  bign operator = (const char* num){  memset(d, 0, sizeof(d)); len = strlen(num);  for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';  clean();  return *this;  }  bign operator = (int num){  char s[20]; sprintf(s, "%d", num);  *this = s;  return *this;  }  bign operator + (const bign& b){  bign c = *this; int i;  for (i = 0; i < b.len; i++){  c.d[i] += b.d[i];  if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;  }  while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;  c.len = max(len, b.len);  if (c.d[i] && c.len <= i) c.len = i+1;  return c;  }  bign operator - (const bign& b){  bign c = *this; int i;  for (i = 0; i < b.len; i++){  c.d[i] -= b.d[i];  if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;  }  while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;  c.clean();  return c;  }  bign operator * (const bign& b)const{  int i, j; bign c; c.len = len + b.len;   for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)   c.d[i+j] += d[i] * b.d[j];  for(i = 0; i < c.len-1; i++)  c.d[i+1] += c.d[i]/10, c.d[i] %= 10;  c.clean();  return c;  }  bign operator / (const bign& b){  int i, j;  bign c = *this, a = 0;  for (i = len - 1; i >= 0; i--)  {  a = a*10 + d[i];  for (j = 0; j < 10; j++) if (a < b*(j+1)) break;  c.d[i] = j;  a = a - b*j;  }  c.clean();  return c;  }  bign operator % (const bign& b){  int i, j;  bign a = 0;  for (i = len - 1; i >= 0; i--)  {  a = a*10 + d[i];  for (j = 0; j < 10; j++) if (a < b*(j+1)) break;  a = a - b*j;  }  return a;  }  bign operator += (const bign& b){  *this = *this + b;  return *this;  }  bool operator <(const bign& b) const{  if(len != b.len) return len < b.len;  for(int i = len-1; i >= 0; i--)  if(d[i] != b.d[i]) return d[i] < b.d[i];  return false;  }  bool operator >(const bign& b) const{return b < *this;}  bool operator<=(const bign& b) const{return !(b < *this);}  bool operator>=(const bign& b) const{return !(*this < b);}  bool operator!=(const bign& b) const{return b < *this || *this < b;}  bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}  string str() const{  char s[maxn]={};  for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';  return s;  }  
};  istream& operator >> (istream& in, bign& x)  
{  string s;  in >> s;  x = s.c_str();  return in;  
}  ostream& operator << (ostream& out, const bign& x)  
{  out << x.str();  return out;  
}  

最全:

#include<string>
#include<iostream>
#include<iosfwd>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
using namespace std;const int  MAX_L=2005; //最大長度,可以修改class bign
{
public:int len, s[MAX_L];//數的長度,記錄數組
//構造函數bign();bign(const char*);bign(int);bool sign;//符號 1正數 0負數string toStr() const;//轉化為字符串,主要是便于輸出friend istream& operator>>(istream &,bign &);//重載輸入流friend ostream& operator<<(ostream &,bign &);//重載輸出流
//重載復制bign operator=(const char*);bign operator=(int);bign operator=(const string);
//重載各種比較bool operator>(const bign &) const;bool operator>=(const bign &) const;bool operator<(const bign &) const;bool operator<=(const bign &) const;bool operator==(const bign &) const;bool operator!=(const bign &) const;
//重載四則運算bign operator+(const bign &) const;bign operator++();bign operator++(int);bign operator+=(const bign&);bign operator-(const bign &) const;bign operator--();bign operator--(int);bign operator-=(const bign&);bign operator*(const bign &)const;bign operator*(const int num)const;bign operator*=(const bign&);bign operator/(const bign&)const;bign operator/=(const bign&);
//四則運算的衍生運算bign operator%(const bign&)const;//取模(余數)bign factorial()const;//階乘bign Sqrt()const;//整數開根(向下取整)bign pow(const bign&)const;//次方
//一些亂亂的函數void clean();
};
#define max(a,b) a>b ? a : b
#define min(a,b) a<b ? a : bbign::bign()
{memset(s, 0, sizeof(s));len = 1;sign = 1;
}bign::bign(const char *num)
{*this = num;
}bign::bign(int num)
{*this = num;
}string bign::toStr() const
{string res;res = "";for (int i = 0; i < len; i++)res = (char)(s[i] + '0') + res;if (res == "")res = "0";if (!sign&&res != "0")res = "-" + res;return res;
}istream &operator>>(istream &in, bign &num)
{string str;in>>str;num=str;return in;
}ostream &operator<<(ostream &out, bign &num)
{out<<num.toStr();return out;
}bign bign::operator=(const char *num)
{memset(s, 0, sizeof(s));char a[MAX_L] = "";if (num[0] != '-')strcpy(a, num);elsefor (int i = 1; i < strlen(num); i++)a[i - 1] = num[i];sign = !(num[0] == '-');len = strlen(a);for (int i = 0; i < strlen(a); i++)s[i] = a[len - i - 1] - 48;return *this;
}bign bign::operator=(int num)
{char temp[MAX_L];sprintf(temp, "%d", num);*this = temp;return *this;
}bign bign::operator=(const string num)
{const char *tmp;tmp = num.c_str();*this = tmp;return *this;
}bool bign::operator<(const bign &num) const
{if (sign^num.sign)return num.sign;if (len != num.len)return len < num.len;for (int i = len - 1; i >= 0; i--)if (s[i] != num.s[i])return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));return !sign;
}bool bign::operator>(const bign&num)const
{return num < *this;
}bool bign::operator<=(const bign&num)const
{return !(*this>num);
}bool bign::operator>=(const bign&num)const
{return !(*this<num);
}bool bign::operator!=(const bign&num)const
{return *this > num || *this < num;
}bool bign::operator==(const bign&num)const
{return !(num != *this);
}bign bign::operator+(const bign &num) const
{if (sign^num.sign){bign tmp = sign ? num : *this;tmp.sign = 1;return sign ? *this - tmp : num - tmp;}bign result;result.len = 0;int temp = 0;for (int i = 0; temp || i < (max(len, num.len)); i++){int t = s[i] + num.s[i] + temp;result.s[result.len++] = t % 10;temp = t / 10;}result.sign = sign;return result;
}bign bign::operator++()
{*this = *this + 1;return *this;
}bign bign::operator++(int)
{bign old = *this;++(*this);return old;
}bign bign::operator+=(const bign &num)
{*this = *this + num;return *this;
}bign bign::operator-(const bign &num) const
{bign b=num,a=*this;if (!num.sign && !sign){b.sign=1;a.sign=1;return b-a;}if (!b.sign){b.sign=1;return a+b;}if (!a.sign){a.sign=1;b=bign(0)-(a+b);return b;}if (a<b){bign c=(b-a);c.sign=false;return c;}bign result;result.len = 0;for (int i = 0, g = 0; i < a.len; i++){int x = a.s[i] - g;if (i < b.len) x -= b.s[i];if (x >= 0) g = 0;else{g = 1;x += 10;}result.s[result.len++] = x;}result.clean();return result;
}bign bign::operator * (const bign &num)const
{bign result;result.len = len + num.len;for (int i = 0; i < len; i++)for (int j = 0; j < num.len; j++)result.s[i + j] += s[i] * num.s[j];for (int i = 0; i < result.len; i++){result.s[i + 1] += result.s[i] / 10;result.s[i] %= 10;}result.clean();result.sign = !(sign^num.sign);return result;
}bign bign::operator*(const int num)const
{bign x = num;bign z = *this;return x*z;
}
bign bign::operator*=(const bign&num)
{*this = *this * num;return *this;
}bign bign::operator /(const bign&num)const
{bign ans;ans.len = len - num.len + 1;if (ans.len < 0){ans.len = 1;return ans;}bign divisor = *this, divid = num;divisor.sign = divid.sign = 1;int k = ans.len - 1;int j = len - 1;while (k >= 0){while (divisor.s[j] == 0) j--;if (k > j) k = j;char z[MAX_L];memset(z, 0, sizeof(z));for (int i = j; i >= k; i--)z[j - i] = divisor.s[i] + '0';bign dividend = z;if (dividend < divid) { k--; continue; }int key = 0;while (divid*key <= dividend) key++;key--;ans.s[k] = key;bign temp = divid*key;for (int i = 0; i < k; i++)temp = temp * 10;divisor = divisor - temp;k--;}ans.clean();ans.sign = !(sign^num.sign);return ans;
}bign bign::operator/=(const bign&num)
{*this = *this / num;return *this;
}bign bign::operator%(const bign& num)const
{bign a = *this, b = num;a.sign = b.sign = 1;bign result, temp = a / b*b;result = a - temp;result.sign = sign;return result;
}bign bign::pow(const bign& num)const
{bign result = 1;for (bign i = 0; i < num; i++)result = result*(*this);return result;
}bign bign::factorial()const
{bign result = 1;for (bign i = 1; i <= *this; i++)result *= i;return result;
}void bign::clean()
{if (len == 0) len++;while (len > 1 && s[len - 1] == '\0')len--;
}bign bign::Sqrt()const
{if(*this<0)return -1;if(*this<=1)return *this;bign l=0,r=*this,mid;while(r-l>1){mid=(l+r)/2;if(mid*mid>*this)r=mid;elsel=mid;}return l;
}bign num0,num1,res;int main() {num0 = 1, num1 = 2;res=num0-num1;cout << res << endl;return 0;
}

出處:
紫書和
https://www.cnblogs.com/HarryGuo2012/p/4524041.html

轉載于:https://www.cnblogs.com/Menteur-Hxy/p/9248016.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250937.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250937.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250937.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

認識及實現MVC

gitee M&#xff1a;Model 數據模型&#xff08;模型層&#xff09;→ 操作數據庫&#xff08;對數據進行增刪改查&#xff09; V&#xff1a;View視圖層 → 顯示視圖或視圖模板 C&#xff1a;Controller 控制器層 → 邏輯層 數據和視圖關聯掛載和基本的邏輯操作 API層 前端請…

算法 --- 翻轉二叉樹

解(C): 1.二叉樹判空 if(root 0) 或 if(root nullptr); 2.二叉樹的左子樹: root->left . 3.使用遞歸,將當前根節點的左右指針指向互換左向右子樹(此時右子樹也進行了翻轉) // C /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode…

float 常見用法與問題--摘抄

float 屬性絕對是眾多切圖仔用的最多的 CSS 屬性之一&#xff0c;它的用法很簡單&#xff0c;常用值就 left、right、none 三個&#xff0c;但是它的特性你真的弄懂了嗎&#xff1f; 我會在這里介紹我對 float 的認識與使用&#xff0c;以及使用過程中遇到的問題。 對 float 的…

javascipt -- find方法和findIndex方法的實現

find: 根據傳入的條件函數,返回符合條件的第一項 var arr [{id: 1, name: zs, age: 18},{id: 2, name: zs, age: 17},{id: 3, name: ls, age: 16},{id: 4, name: ls, age: 15}]Array.prototype._find_ function(cb){for(var i0; i< this.length; i){if(cb(this[i],i)){ret…

bzoj 2179 FFT快速傅立葉 FFT

題面 題目傳送門 解法 題如其名…… 不妨將多項式的\(x^i\)變成\(10^i\)&#xff0c;然后就是一個比較簡單的FFT了 md讀進來的是一個字符串&#xff0c;并且要倒序 最后注意進位問題 時間復雜度&#xff1a;\(O(n\ log\ n)\) 代碼 #include <bits/stdc.h> #define N 1 &l…

【探討】javascript事件機制底層實現原理

前言 又到了扯淡時間了&#xff0c;我最近在思考javascript事件機制底層的實現&#xff0c;但是暫時沒有勇氣去看chrome源碼&#xff0c;所以今天我來猜測一把 我們今天來猜一猜&#xff0c;探討探討&#xff0c;javascript底層事件機制是如何實現的 博客里面關于事件綁定與執行…

node --- 在node中使用mongoosemongoDB的安裝

*首先確保,你的電腦安裝了mongodb,網址: mongodb官網 *使用npm安裝 mongoose: mongoose官網 ps:mongoose是Node中操作mongoDB的第三方插件.用于提高數據庫操作效率(相當于在mongoDB上封裝了一次,暴露出更友好的API) MongoDB的安裝 1.下載地址 2.下載好了后,傻瓜式的安裝(我的…

websocket demo

git node.js創建websocket 的服務 Nodejs Websocket包 ws.createServer([options], [callback]) The callback is a function which is automatically added to the “connection” event. 前端代碼 1. 創建實例、打開連接 this.websocket new WebSocket(ws://127.0.0.1:80…

shell常用命令總結總結

打rpm包&#xff1a; rpmbuild -bb SPECS/smplayer.spec --define "_topdir pwd" 安裝rpm包&#xff1a; rpm -ivh [rpm包文件] 如果安裝不上 rpm -ivh [rpm包文件] --force #強制安裝 打包的時候可能需要一些依賴&#xff1a; dnf install 【依賴文件名】 sed -i常用…

Filter

一、簡介 Filter也稱之為過濾器&#xff0c;它是Servlet技術中最激動人心的技術&#xff0c;WEB開發人員通過Filter技術&#xff0c;對web服務器管理的所有web資源&#xff1a;例如Jsp&#xff0c;Servlet&#xff0c;靜態圖片文件或靜態html文件進行攔截&#xff0c;從而實現一…

前端面試手寫題

深拷貝 // 深拷貝 function deepClone(ori) {let tar;if (typeof ori object && ori ! null) {tar Array.isArray(ori) ? [] : {}for (let k in ori) {if (ori.hasOwnProperty(k)) {tar[k] deepClone(ori[k])}}} else {tar ori}return tar}繼承 // 圣杯模式實現…

node --- 使用express.Router與body-parser

express框架提供了一個Router方法,用于監聽路由 // 命令行(windows*64) npm install express --save// router.js const express require("express"); // 定義路由 const router express.Router();// 處理http://host:port/students/ 路由(GET方法) router.get…

python基礎1 第一天

TEST 1 阿斯蒂芬 day1test1 while 1&#xff1a;print&#xff08;333&#xff09; import randomprint轉載于:https://www.cnblogs.com/shuangzhu/p/9243853.html

【數據庫】《SQL必知必會 4th》部分筆記

9.匯總數據 count(*) 包括空 count(name) 不包括空 10.分組數據 group by 分組 having 過濾分組 where 過濾行 11.子查詢 select .. from .. where in (select ...) 由內向外處理 A.子查詢過濾 作為子查詢的語句只能查詢單個列。 B.作為計算字段使用子查詢 select cust_name, …

微軟認知服務應用秘籍 – 漫畫翻譯篇

概述 微軟認知服務包括了影像、語音、語言、搜索、知識五大領域&#xff0c;通過對這些認知服務的獨立或者組合使用&#xff0c;可以解決很多現實世界中的問題。作為AI小白&#xff0c;我們可以選擇艱難地攀登崇山峻嶺&#xff0c;也可以選擇像牛頓一樣站在巨人的肩膀上。本章節…

01 React初步認知、React元素、渲染、工程化

定義 react&#xff1a;用于構建用戶界面的 JavaScript 庫 &#xff08;僅負責View層渲染、應在視圖上體現交互邏輯&#xff09;vue&#xff1a;漸進式JavaScript 框架&#xff08;MVVM&#xff09; 使用 引入CDN腳本添加根容器 div #app創建React組件 ReactDOM.render Re…

node --- 在express中配置使用模板引擎(art-template)

下載依賴: npm install --save art-template express-art-template配置: // app.js const express require("express"); const app express(); app.engine("html", require("express-art-template"));使用: 例如處理瀏覽器GET請求 /students…

PAM認證機制

一、PAM簡介 Sun公司1995年開發的一種與認證相關的通用框架機制&#xff0c;PAM只關注如何為服務驗證用戶的API&#xff0c;通過提供一些動態鏈接庫和一套統一的API&#xff0c;將系統提供的服務和該服務的認證方式分開&#xff1b;PAM只是一個框架而已&#xff0c;自身不做認證…

02 JSX學習

使用vite處理jsx vite引入的腳本必須是ESM的 npm init -y yarn add vite package.json 添加vite命令 index.html引入jsxJSX是什么 一種標簽語法&#xff0c;在JS基礎上進行的語法擴展不是字符串、也不是HTML是描述UI呈現與交互的直觀的表現形式JSX被編譯后會生成React元素 &am…

使用FreeCookies 控制瀏覽器cookies及修改http響應內容

FreeCookies 插件安裝 1&#xff1a;您的計算機需要已經安裝Fiddler &#xff08;如未安裝&#xff0c;請至官網下載安裝 http://docs.telerik.com/fiddler/configure-fiddler/tasks/configurefiddler&#xff09; 2&#xff1a;進入Fiddler安裝目錄下的Scripts目錄下&#xff…