zoukankan      html  css  js  c++  java
  • poj1408

    题意:给出一个正方形,每条边上有n个点,把这些点作为端点拉线组成的网格(整个正方形由一些四边行拼成),求最大格的面积。

    分析:计算几何,求出所有线的交点,分别计算每个格的面积。

    在求线段交点时可以用叉积面积的方式,求ac,bd交点:

    void intersection(Point &a, Point &b, Point &c, Point &d, Point &ret)
    {
        double s1 = xmulti(a, c, b);
        double s2 = xmulti(a, c, d);
        ret.x = (s1 * d.x - s2 * b.x) / (s1 - s2);
        ret.y = (s1 * d.y - s2 * b.y) / (s1 - s2);
    }

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cmath>
    usingnamespace std;

    #define maxn 50

    struct Point
    {
    double x, y;
    } point[maxn][maxn];

    double ans;
    int n;

    void input()
    {
    n
    ++;
    point[
    0][0].x =0;
    point[
    0][0].y =0;
    point[
    0][n].x =1;
    point[
    0][n].y =0;
    point[n][
    0].x =0;
    point[n][
    0].y =1;
    point[n][n].x
    =1;
    point[n][n].y
    =1;
    for (int i =1; i < n; i++)
    {
    scanf(
    "%lf", &point[0][i].x);
    point[
    0][i].y =0;
    }
    for (int i =1; i < n; i++)
    {
    scanf(
    "%lf", &point[n][i].x);
    point[n][i].y
    =1;
    }
    for (int i =1; i < n; i++)
    {
    scanf(
    "%lf", &point[i][0].y);
    point[i][
    0].x =0;
    }
    for (int i =1; i < n; i++)
    {
    scanf(
    "%lf", &point[i][n].y);
    point[i][n].x
    =1;
    }
    }

    double xmulti(Point &p0, Point &p1, Point &p2)
    {
    return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
    }

    void intersection(Point &a, Point &b, Point &c, Point &d, Point &ret)
    {
    double s1 = xmulti(a, c, b);
    double s2 = xmulti(a, c, d);
    ret.x
    = (s1 * d.x - s2 * b.x) / (s1 - s2);
    ret.y
    = (s1 * d.y - s2 * b.y) / (s1 - s2);
    }

    double area(Point &a, Point &b, Point &c, Point &d)
    {
    double s1 = xmulti(a, c, b);
    double s2 = xmulti(a, c, d);
    return (abs(s1) + abs(s2)) /2;
    }

    void work()
    {
    for (int i =1; i < n; i++)
    for (int j =1; j < n; j++)
    intersection(point[i][
    0], point[0][j], point[i][n], point[n][j], point[i][j]);
    ans
    =-1;
    for (int i =0; i < n; i++)
    for (int j =0; j < n; j++)
    {
    double temp = area(point[i][j], point[i +1][j], point[i +1][j +1], point[i][j +1]);
    ans
    = max(ans, temp);
    }
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    while (scanf("%d", &n), n)
    {
    input();
    work();
    printf(
    "%.6f\n", ans);
    }
    return0;
    }
  • 相关阅读:
    java中的IO整理
    指针 study~O(∩_∩)O~
    const形参与非const形参
    最大公约数递归求解
    值传递、地址传递、引用传递(参考网上各种资料总结哦~)
    windows8开发笔记(4)消息弹出动画
    Azure绑定域名以及利用FTP上传文件
    windows8安装第三方Modern UI(Metro)应用方法
    windows8开发笔记(2)通知
    windows8开发笔记(5)说说Json的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/rainydays/p/2097639.html
Copyright © 2011-2022 走看看