1、1三角形面积计算 1字典树模板 2求线段所在直线 5求外接圆 5求内接圆 6判断点是否在直线上 8简单多边形面积计算公式 8stein 算法求最大共约数 .9最长递增子序列模板 o(nlogn 算法实现) .9判断图中同一直线的点的最大数量 10公因数和公倍数 12已知先序中序求后序 12深度优先搜索模板 13匈牙利算法 二部图匹配 BFS 实现 15带输出路径的 prime 算法 17prime 模板 .18kruskal 模板 .19dijsktra22并查集模板 23高精度模板 24三角形面积计算/已知三条边和外接圆半径,公式为 s = a*b*c/(4*R)double GetArea
2、(double a, double b, double c, double R)return a*b*c/4/R;/已知三条边和内接圆半径,公式为 s = prdouble GetArea(double a, double b, double c, double r)return r*(a+b+c)/2;/已知三角形三条边,求面积double GetArea(doule a, double b, double c)double p = (a+b+c)/2;return sqrt(p*(p-a)*(p-b)*(p-c);2/已知道三角形三个顶点的坐标struct Pointdouble x, y
3、;Point(double a = 0, double b = 0)x = a; y = b;double GetArea(Point p1, Point p2, Point p3)double t = -p2.x*p1.y+p3.x*p1.y+p1.x*p2.y-p3.x*p2.y-p1.x*p3.y+p2.x*p3.y;if(t #include #include #define BASE_LETTER a#define MAX_TREE 35000#define MAX_BRANCH 26structint nextMAX_BRANCH; /记录分支的位置int cMAX_BRANCH;
4、 /查看分支的个数int flag; /是否存在以该结点为终止结点的东东,可以更改为任意的属性 trieMAX_TREE;int now;void init()now = 0;memset(3now +;int add ()memset(return now+;int insert( char *str)int pre = 0, addr;while( *str != 0 )addr = *str - BASE_LETTER;if( !triepre.nextaddr )triepre.nextaddr = add();triepre.caddr+;pre = triepre.nextaddr
5、;str +;triepre.flag = 1;return pre;int search( char *str )int pre = 0, addr;while( *str != 0 )addr = *str - BASE_LETTER;if ( !triepre.nextaddr )return 0;pre = triepre.nextaddr;str +;if( !triepre.flag )return 0;4return pre;pku2001 题,源代码:void check( char *str )int pre = 0, addr;while(*str != 0)addr =
6、*str - BASE_LETTER;if( triepre.caddr = 1)printf(“%cn“, *str);return;printf(“%c“, *str);pre = triepre.nextaddr;str +;printf(“n“);char input100125;int main()int i = 0,j;init();while(scanf(“%s“, inputi) != EOF)getchar();insert(inputi);i+;for(j = 0; j p1.x int main()int count,i,n,left,mid,right,Blen=0,n
7、um;scanf(“%d“, /case 的个数while(count-)scanf(“%d“, /每组成员的数量Blen = 0;for(i=1;i#include #include using namespace std;#define MAX 1010 /最大点的个数struct pointint x,y;numMAX;int usedMAXMAX*2; /条件中点的左边不会大于 1000,just equal MAXint countNMAXMAX*2;#define abs(a) (a0?a:(-a) int GCD(int x, int y)int temp;if(x #inclu
8、de using namespace std;string post;void fun(string pre, string mid)13if(pre = “ | mid = “) return;int i = mid.find(pre0);fun(pre.substr(1,i), mid.substr(0,i);fun(pre.substr(i+1, (int)pre.length()-i-1), mid.substr(i+1, (int)mid.length()-i-1);post += pre0;int main()string pre, mid;while(cin pre)cin mi
9、d;post.erase();fun(pre, mid);cout = 1 search(x-1,y); if(x+1 = 1 search(x,y-1);if(y+1 = 1)if(datax-1y = t)count+;search(x-1,y);if(y-1 = 1 search(x-1,y-1);if(y+1 = 1 15search(x+1,y-1);if(y+1 = 1 search(x,y-1);if(y+1 -1 )17 vm1vm2prevx = x; vm2x = vm2prevx; x = prevx; vm2x = i; vm1i = x; /匹配的边数加一 ans+;
10、 return ans; 带输出路径的 prime 算法#include #include using namespace std;const int MAX = 110;int dataMAXMAX;int lowcostMAX;int adjvexMAX;int main()int n;cin n;int i,j;for(i = 0 ;i dataij;/primfor(i = 1; i %dn“, adjvexchoose+1, choose+1, lowcostchoose);lowcostj = 0;for(j = 1; j datachoosej)lowcostj = datach
11、oosej;adjvexj = choose;return 0;prime 模板#include #include #include using namespace std;int const MAX = 110;int disMAXMAX;int lowcostMAX;int main()int n;int i,j;while(cin n)for(i = 0; i disij;/下面是 prim 算法部分,ans 是计算所有路径的和lowcost0 = 0;for(i = 1; i dischoosej)lowcostj = dischoosej;cout #include #include
12、 using namespace std;20const int MAX = 1010; /节点个数const int MAXEDGE = 15010; /边个数bool usedMAXEDGE; /标记边是否用过struct nodeint begin, end, dis;dataMAXEDGE;class UFSetprivate:int parentMAX+1;int size;public:UFSet(int s = MAX);int Find(int x);void Union(int root1, int root2);UFSet:UFSet(int s)size = s+1;me
13、mset(parent, -1, sizeof(int)*size);void UFSet:Union(int root1, int root2)int temp = parentroot1 + parentroot2;if(parentroot1 0)p = parentp;21int t = x;while(t != p)t = parentx;parentx = p;x = t;return p;bool cmp(node a, node b)return (a.dis #include using namespace std;const int maxint = 9999999;con
14、st int maxn = 1010;int datamaxnmaxn, lowcostmaxn; /data 存放点点之间的距离,lowcost 存放点到 start 的距离,从 0 开始存放bool usedmaxn;/标记点是否被选中int n; /顶点的个数void disktra(int start)/初始点是 start 的 dij 算法int i,j;memset(used, 0, sizeof(used);/initefor(i = 0; i lowcostj)choose = j;tempmin = lowcostj;usedchoose = true;23/updata o
15、thersfor(j = 0; j #include using namespace std;const int MAX = 5005;class UFSetprivate:int parentMAX+1;int size;public:UFSet(int s = MAX);/初始化void Union(int root1, int root2);/合并,注意参数为根节点int Find(int i );/返回根节点int SetNum();/返回集合的个数;UFSet:UFSet(int s)size = s+1;memset(parent, -1, sizeof(int)*size );v
16、oid UFSet:Union( int root1, int root2)int temp = parentroot1+parentroot2;if(parentroot1=0; j = parentj);while(i!=j)int temp = parenti;parenti = j; i = temp;return j;int UFSet:SetNum()int totalNum = 0,i;for(i = 0; i #include using namespace std;/ - 非负数计算部分:f1f14 -string operator+(string x, string y);
17、 / x、y 都必须非负string operator-(string x, string y); / x、y 都必须非负(结果可能为负)string operator*(string x, string y); / x、y 非负string operator*(string s, int a); / s,a 非负,且 a 必须小于 2*108.string MSDiv(string x, int y, int / 多精度除以 int,x 非负,y为正string operator/(string s, int a); / 调用 MSDiv25int operator%(string s, i
18、nt a); / 调用 MSDivstring MMDiv(string x, string y, string / 多精度除以多精度,x 非负,y 为正string operator/(string x, string y); / 调用 MMDivstring operator%(string x, string y); / 调用 MMDivstring HPower(string s, int a); / s,a 必须非负!string HSqrt(string s); / 开平方取整,s 非负!string Head_zero_remover(string num); / 除了开头可能有
19、0外,num 必须是非负数。bool Less(string x, string y); / 非负数之间的“小于”/ - 以下是负数支持:f15f19 -string operator-(string s); / 取负string SAdd(string x, string y);string SMinus(string x, string y);string SMul(string x, string y);string SMul(string s, int a); / 同样,a 的绝对值不能超过 2*108/ - f1 () -string operator+(string x, stri
20、ng y)if(x.size() =yx.swap(y);y.insert(y.begin(), x.size()-y.size(), 0); / y 开头补 0 到和 x 一样长string sum(x.size(), -1); / 初始大小:x.size()int carry=0;for(int i=x.size()-1; i = 0; -i)carry += xi+yi-2*0;sumi = carry%10+0;carry /= 10;if(carry 0) / 还有进位 1return string(“1“) += sum; / 给开头添加一个“1”return sum;26/ -
21、f2 (need: f13, f14) -string operator-(string x, string y)bool neg = false; / 结果为负标志if(Less(x, y)x.swap(y); / 如果 x= 0; -i)if(xi = yi+carry) / 本位够减diffi = xi-yi-carry+0;carry = 0;else / 需要借位diffi = 10+xi-yi-carry+0;carry=1;if(neg)return string(“-“) += Head_zero_remover(diff);return Head_zero_remover(d
22、iff); / - f3 (need f1, f4) -string operator*(string x, string y)string prod=“0“; /初值:0for(int i=y.size()-1; i = 0; -i)string p_sum = x * (yi-0); / p_sum:部分积if(p_sum != “0“) / 保证后面加 0 后也符合 UAdd 的要求!p_sum.insert(p_sum.end(), y.size()-1-i, 0);prod = prod + p_sum; 27return prod;/ - f4 () -string operato
23、r*(string s, int a)if(s = “0“ | a = 0) / 以免后面特殊处理!return “0“; string prod(s.size(), -1); / 先申请 s.size()位int carry=0;for(int i=s.size()-1; i = 0; -i)carry += (si-0)*a;prodi = carry%10+0;carry /= 10;while(carry0)prod.insert(prod.begin(), carry%10+0);carry /= 10;return prod;/ - f5 (need f13) -string MS
24、Div(string x, int y, int res=0;for(int i=0; i0)if(a%2 = 1)power = power * s;a /= 2;s = s * s;return power;/ - f12 (need f2, f4, f13, f14) -string HSqrt(string s) / 手工开平方。若要返回余数,return 前的 res 就是!string sqroot(s.size()+1)/2, -1);string res = s.substr(0, 2-s.size()%2); / 奇位取前 1,偶位取前 2string div=“0“; /
25、占一位置for(int i=0; isqroot.size(); +i)for(int quot=9; ; -quot)divdiv.size()-1 = quot+0; / 末位试商,从9到0string p_prod = div*quot;if( ! Less(res, p_prod) ) / p_prod = ressqrooti = quot+0; / 将结果追加!div = sqroot.substr(0, i+1)*20;res = res - p_prod;30string next2 = s.substr(i+1)*2-s.size()%2, 2);if(res = “0“)r
26、es = next2; / 取后 2 位elseres += next2; / 下移 2 位,追加;即 res = res*100+next2break;return sqroot;/ - f13 () -bool Less(string x, string y)return x.size()y.size() | x.size() = y.size() / - f14 () -string Head_zero_remover(string num) / 化简“003”等数if(num0 != 0)return num;int pos=num.find_first_not_of(0);if(pos = string:npos) / 全 0return “0“;return num.substr(pos, num.size()-pos);/ 以下是负数支持!/ - f15 () -string operator-(string s)if(s0 = -)return s.substr(1, s.size()-1);if(s = “0“)