This essay is the first of my blog math class, I think it is better to write some feelings. The mathematical class in this algorithm is generally to solve some of the tolerance, number theory, or combinatorial mathematics and other problems. But frankly speaking, this is only a narrow sense of mathematics, but we use what data structure ah, what DP ah, what graph theory, all should belong to the general mathematics. Therefore, regardless of who, should be mathematics as their strongest auxiliary. For the theoretical things, many acmer are reluctant to think, will only take some ready-made things directly to apply, although this is a problem-handling skills, but if the long-range, will certainly have a bad impact on our thinking ability. Therefore, we have problems must be independent thinking, less use of other people's routines, more of their own routines.
Okay, so much nonsense, let's discuss the little thing about Euclid. Euclidean algorithm, also known as the greatest common divisor method, refers to the calculation of two positive integers, a, B. Oh, do you think this is simple? You see this must not hesitate to write the following code
1 int gcd (intint b) {2 return a? gcd (b%A, a): b; 3 }
Well, it is very simple, but note that Euclid can only solve the problem of the greatest common divisor of two integers, but decimals (of course, decimals do not have the concept of greatest common divisor, here all unify the decimal number as an integer)? Fractional computers do not support modulo arithmetic. Well, yes, we've been using the computer's modulo operator% to help us with the modulo operation, but many students (including my t_t) have been exposed to the ancestor that led to it in the first place, which is actually very simple, for any non-0 number modulo (of course, there is no modulo this thing, we analogy on the line), We can actually unify this.
1 Double MOD (doubledouble// a% b2 return A-floor (A/b) * B; 3 }
Since mod can do this, then the GCD operation of decimals can be drawn
1 #define ESP 1e-423double decimal_gcd (doubledouble b) { 4 return a >= esp? DECIMAL_GCD (B-floor (b/a) *A, a): b; 5 }
All right, here we go, we'll do the actual drill.
Http://codeforces.com/contest/1/problem/C
This problem is a classic example of a decimal greatest common divisor, you can try it.
Here's my answer.
1#include <bits/stdc++.h>2 #defineEPS 1e-43 #definePie ACOs (-1.0)4 #definePOWs (x) ((x) * (x))5 6 using namespacestd;7typedefDoubleIntd;8 9Intd e[3], tri[3];Ten OneIntd dis (intd x1,intd y1,intd x2,intd y2) {//Calculate Edge Length A returnsqrt (POWs (x2-x1) +pows (y2-y1)); - } - theIntd Gettri (inti) {//to obtain the corresponding center angle of the edge I, cosine theorem -Intd s=0.0, m=2.0; - for(intj=0;j<3; j + +)if(i!=j) -s + = e[j] * E[j], M *=E[j]; +Intd temp = (S-e[i]*e[i])/m; - returnACOs (temp); + } A atIntd gcd (DoubleADoubleb) { - returnA >= EPS? GCD (B-floor (b/a) *A, a): b; - } - - intMain () { -Intd a[3], b[3]; in for(intI=0;i<3; i++) -scanf"%LF%LF", &a[i], &b[i]); to + intCN =0; - for(intI=0;i<3; i++) the for(intj=i+1;j<3; j + +) *e[cn++] = dis (a[i],b[i],a[j],b[j]);//get three sides $ Panax Notoginseng for(intI=0;i<3; i++) -Tri[i] =Gettri (i); the +Intd g = gcd (tri[0], gcd (tri[1], tri[2])); AIntd R = e[0]/(2.0*sin (tri[0])); the +G *=2.0; -printf"%.6f\n",2.0*pie/g * POWs (R) *sin (g)/2.0); $ return 0; $}
Math Talk (algorithm class + Ancient Berland Circus solution)