Assignment 2: Mathematics

  • 1799 Yeehaa! (1)

  • 1401 Factorial (1)

  • 2262 Goldbach's Conjecture (2)

  • 2242 The Circumference of the Circle (2)

  • 1654 Area (3)

  • 2309 BST (3)

  • 2693 Chocolate Chip Cookies (4)

  • 2084 Game of Connections (4)

  • 2085 Inversion (5)

  • 1426 Find The Multiple (6)

  • 2356 Find a multiple (7, challenge problem)

  • 1148 Utopia Divided (9, challenge problem)


    poj2242

    已知不共线三点求圆

    #include <iostream>
    #include <vector>
    #include <cmath>
    #include <cassert>
    
    using namespace std;
    
    double INF = 1e100;
    double EPS = 1e-12;
    #define PI acos(-1.0)
    
    struct PT {
        double x, y;
    
        PT() {
        }
    
        PT(double x, double y) : x(x), y(y) {
        }
    
        PT(const PT &p) : x(p.x), y(p.y) {
        }
    
        PT operator +(const PT &p) const {
            return PT(x + p.x, y + p.y);
        }
    
        PT operator -(const PT &p) const {
            return PT(x - p.x, y - p.y);
        }
    
        PT operator *(double c) const {
            return PT(x*c, y * c);
        }
    
        PT operator /(double c) const {
            return PT(x / c, y / c);
        }
    };
    
    double dot(PT p, PT q) {
        return p.x * q.x + p.y * q.y;
    }
    
    double dist2(PT p, PT q) {
        return dot(p - q, p - q);
    }
    
    double cross(PT p, PT q) {
        return p.x * q.y - p.y * q.x;
    }
    
    
    
    // rotate a point CCW or CW around the origin
    
    PT RotateCCW90(PT p) {
        return PT(-p.y, p.x);
    }
    
    PT RotateCW90(PT p) {
        return PT(p.y, -p.x);
    }
    
    PT RotateCCW(PT p, double t) {
        return PT(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t));
    }
    
    // project point c onto line through a and b
    // assuming a != b
    
    PT ProjectPointLine(PT a, PT b, PT c) {
        return a + (b - a) * dot(c - a, b - a) / dot(b - a, b - a);
    }
    
    // project point c onto line segment through a and b
    
    PT ProjectPointSegment(PT a, PT b, PT c) {
        double r = dot(b - a, b - a);
        if (fabs(r) < EPS) return a;
        r = dot(c - a, b - a) / r;
        if (r < 0) return a;
        if (r > 1) return b;
        return a + (b - a) * r;
    }
    
    // compute distance from c to segment between a and b
    
    double DistancePointSegment(PT a, PT b, PT c) {
        return sqrt(dist2(c, ProjectPointSegment(a, b, c)));
    }
    
    // compute distance between point (x,y,z) and plane ax+by+cz=d
    
    double DistancePointPlane(double x, double y, double z,
            double a, double b, double c, double d) {
        return fabs(a * x + b * y + c * z - d) / sqrt(a * a + b * b + c * c);
    }
    
    // determine if lines from a to b and c to d are parallel or collinear
    
    bool LinesParallel(PT a, PT b, PT c, PT d) {
        return fabs(cross(b - a, c - d)) < EPS;
    }
    
    bool LinesCollinear(PT a, PT b, PT c, PT d) {
        return LinesParallel(a, b, c, d)
                && fabs(cross(a - b, a - c)) < EPS
                && fabs(cross(c - d, c - a)) < EPS;
    }
    
    // determine if line segment from a to b intersects with 
    // line segment from c to d
    
    bool SegmentsIntersect(PT a, PT b, PT c, PT d) {
        if (LinesCollinear(a, b, c, d)) {
            if (dist2(a, c) < EPS || dist2(a, d) < EPS ||
                    dist2(b, c) < EPS || dist2(b, d) < EPS) return true;
            if (dot(c - a, c - b) > 0 && dot(d - a, d - b) > 0 && dot(c - b, d - b) > 0)
                return false;
            return true;
        }
        if (cross(d - a, b - a) * cross(c - a, b - a) > 0) return false;
        if (cross(a - c, d - c) * cross(b - c, d - c) > 0) return false;
        return true;
    }
    
    // compute intersection of line passing through a and b
    // with line passing through c and d, assuming that unique
    // intersection exists; for segment intersection, check if
    // segments intersect first
    
    PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
        b = b - a;
        d = c - d;
        c = c - a;
        assert(dot(b, b) > EPS && dot(d, d) > EPS);
        return a + b * cross(c, d) / cross(b, d);
    }
    
    // compute center of circle given three points
    
    PT ComputeCircleCenter(PT a, PT b, PT c) {
        b = (a + b) / 2;
        c = (a + c) / 2;
        return ComputeLineIntersection(b, b + RotateCW90(a - b), c, c + RotateCW90(a - c));
    }
    
    int main(){
        PT points[3],center;
        int n,i;
        double r;
        while(scanf("%lf%lf%lf%lf%lf%lf",&points[0].x,&points[0].y,&points[1].x,&points[1].y,&points[2].x,&points[2].y)!=EOF){
            center=ComputeCircleCenter(points[0],points[1],points[2]);
        //  cout<<center<<endl;
            r=sqrt(dist2(center,points[0]));
            printf("%.2f\n",2*PI*r);
        }
        return 0;
    }

    poj1654

    求凸包面积

    #include <iostream>
    #include <vector>
    #include <cmath>
    #include <cassert>
    
    using namespace std;
    
    double INF = 1e100;
    double EPS = 1e-12;
    #define PI acos(-1)
    
    struct PT {
        long long x, y;
    
        PT() {
        }
    
        PT(long long x, long long y) : x(x), y(y) {
        }
    
        PT(const PT &p) : x(p.x), y(p.y) {
        }
    };
    
    char st[1000010];
    int dx[] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
    int dy[] = {0, -1, -1, -1, 0, 0, 0, 1, 1, 1};
    
    int main() {
        int T, x, y, i;
        scanf("%d", &T);
        while (T--) {
            scanf("%s", st);
            x = 0, y = 0;
            long long area = 0;
            PT p1(0, 0), p2;
            for (i = 0;; ++i) {
                if (st[i] == '5')
                    break;
                p2.x = p1.x + dx[st[i] - '0'];
                p2.y = p1.y + dy[st[i] - '0'];
                area += (p1.x * p2.y - p1.y * p2.x);
                p1 = p2;
            }
            if (area < 0)
                area = -area;
            if (area & 1) {
                printf("%lld.5\n", area >> 1);
            } else {
                printf("%lld\n", area >> 1);
            }
        }
        return 0;
    }

    poj2693

    已知两点及半径求圆

    #include <iostream>
    #include <vector>
    #include <cmath>
    #include <cassert>
    
    using namespace std;
    
    double INF = 1e100;
    double EPS = 1e-12;
    
    struct PT {
        double x, y;
    
        PT() {
        }
    
        PT(double x, double y) : x(x), y(y) {
        }
    
        PT(const PT &p) : x(p.x), y(p.y) {
        }
    
        PT operator +(const PT &p) const {
            return PT(x + p.x, y + p.y);
        }
    
        PT operator -(const PT &p) const {
            return PT(x - p.x, y - p.y);
        }
    
        PT operator *(double c) const {
            return PT(x*c, y * c);
        }
    
        PT operator /(double c) const {
            return PT(x / c, y / c);
        }
    };
    
    double dot(PT p, PT q) {
        return p.x * q.x + p.y * q.y;
    }
    
    double dist2(PT p, PT q) {
        return dot(p - q, p - q);
    }
    
    double cross(PT p, PT q) {
        return p.x * q.y - p.y * q.x;
    }
    const double ra = 2.5;
    
    struct CC {
        PT center;
        double r;
    
        CC() {
        }
    
        CC(PT cc, double rr) : center(cc), r(rr) {
        }
    
        CC(const CC &c) : center(c.center), r(c.r) {
        }
    };
    PT pt[210];
    
    const CC gao(const PT &a, const PT &b, double r) {
        double det = r * r / dist2(a, b) - 0.25;
        CC circle;
        if (det < 0)
            circle.r = -1;
        else {
            double h = sqrt(det);
            circle.center = (a + b)*0.5 + PT(a.y - b.y, b.x - a.x) * h;
            circle.r = r;
        }
        return circle;
    }
    
    int main() {
        int n = 0, i, j, k, tot;
        while (scanf("%lf%lf", &pt[n].x, &pt[n].y) != EOF) {
            if (pt[n].x < 0)
                break;
            ++n;
        }
        CC ans;
        tot = 1;
        int cnt;
        for (i = 0; i < n; ++i) {
            for (j = 0; j < n; ++j) {
                if (i == j)
                    continue;
                ans = gao(pt[i], pt[j], ra);
                if (ans.r < 0)
                    continue;
                cnt = 0;
                for (k = 0; k < n; ++k) {
                    if (dist2(pt[k], ans.center) <= 6.25+EPS)
                        ++cnt;
                }
                if (cnt > tot)
                    tot = cnt;
            }
        }
        printf("%d\n", tot);
        return 0;
    }

    poj2084

    大数加法和乘法

    #include<cstdio>
    #include<cstring>
    int ans[110][201];
    
    void add(int a[201], int b[201]) {
        int i, j = 0;
        for (i = 0; i <= 100; ++i) {
            a[i] = a[i] + b[i] + j;
            j = a[i] / 10;
            a[i] %= 10;
        }
    }
    int c[201];
    
    int* mul(int a[201], int b[201]) {
        memset(c, 0, sizeof (c));
        int i, j, k;
        for (i = 0; i <= 100; ++i) {
            k = 0;
            for (j = 0; j <= 100; ++j) {
                c[i + j] = c[i + j] + a[i] * b[j] + k;
                k = c[i + j] / 10;
                c[i + j] %= 10;
            }
        }
        return c;
    }
    
    void gao() {
        int i, j;
        ans[0][0] = 1;
        for (i = 1; i <= 100; ++i) {
            for (j = 0; j < i; ++j) {
                add(ans[i], mul(ans[j], ans[i - 1 - j]));
            }
        }
    }
    
    void print(int *a) {
        int i;
        for (i = 100; i >= 0; --i)
            if (a[i])
                break;
        if (i < 0)
            printf("0");
        for (; i >= 0; --i)
            printf("%d", a[i]);
        printf("\n");
    }
    
    int main() {
        gao();
        int n;
        while (scanf("%d", &n) != EOF && n > 0) {
            print(ans[n]);
        }
        return 0;
    }

    poj1148

    分横纵轴单独贪心。从最后一步向前回溯,和前一步轴的正负相同则取步长短的,不同则取长的。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int a[10010], b[10010], o[10010], sx[10010], sy[10010], x[10010], y[10010];
    
    void dfs(int *x, int *a, int *sx, int l, int r, int now) {
        if (now == 0) {
            x[now] = a[l];
            return;
        }
        if (sx[now] == sx[now - 1]) {
            x[now] = a[l];
            ++l;
        } else {
            x[now] = a[r];
            --r;
        }
        dfs(x, a, sx, l, r, now - 1);
    }
    
    int main() {
        int n, i, j, k;
        scanf("%d", &n);
        for (i = 0; i < n; ++i)
            scanf("%d", &a[i]);
        for (i = 0; i < n; ++i)
            scanf("%d", &b[i]);
        for (i = 0; i < n; ++i)
            scanf("%d", &o[i]);
        sort(a, a + n);
        sort(b, b + n);
        for (i = 0; i < n; ++i) {
            if (o[i] == 1) sx[i] = 1, sy[i] = 1;
            else if (o[i] == 2) sx[i] = -1, sy[i] = 1;
            else if (o[i] == 3) sx[i] = -1, sy[i] = -1;
            else sx[i] = 1, sy[i] = -1;
        }
        if (sx[n - 1] == 1) {
            for (i = n - 2; i >= 0; i -= 2)
                a[i] = -a[i];
        } else {
            for (i = n - 1; i >= 0; i -= 2)
                a[i] = -a[i];
        }
        if (sy[n - 1] == 1) {
            for (i = n - 2; i >= 0; i -= 2)
                b[i] = -b[i];
        } else {
            for (i = n - 1; i >= 0; i -= 2)
                b[i] = -b[i];
        }
        dfs(x, a, sx, 0, n - 1, n - 1);
        dfs(y, b, sy, 0, n - 1, n - 1);
        for (i = 0; i < n; ++i) {
            if (x[i] > 0)
                printf("+");
            printf("%d ", x[i]);
            if (y[i] > 0)
                printf("+");
            printf("%d\n", y[i]);
        }
        return 0;
    }



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值