zoukankan      html  css  js  c++  java
  • 百度--寻找三角形

    三维空间中有N个点,每个点可能是三种颜色的其中之一,三种颜色分别是红绿蓝,分别用'R', 'G', 'B'表示。 
    现在要找出三个点,并组成一个三角形,使得这个三角形的面积最大。
    但是三角形必须满足:三个点的颜色要么全部相同,要么全部不同。 

    输入描述:

    首先输入一个正整数N三维坐标系内的点的个数.(N <= 50)  
    接下来N行,每一行输入 c x y z,c为'R', 'G', 'B' 的其中一个。x,y,z是该点的坐标。(坐标均是0到999之间的整数)

    输出描述:

    输出一个数表示最大的三角形面积,保留5位小数。

    输入例子:

    5
    R 0 0 0
    R 0 4 0
    R 0 0 3
    G 92 14 7
    G 12 16 8
    

    输出例子:

    6.00000
    基本思想:

    这道题主要分三个步骤:

    1、从5个点中任意选择三个点分别判断:能否组成三角形;颜色是否全部相同或者全部不同

    2、将满足条件的三个点,计算其面积

    3、得到最大的面积。

    #include<bits/stdc++.h>
    using namespace std;
    
    struct node{
        char c;
        int x, y, z;
    };
    vector<node> str;
    class Bear
    {
    public:
        //求三维空间里两个点的距离
        double distance(node i, node j)
        {
            return sqrt(double((i.x - j.x)*(i.x - j.x) + (i.y - j.y)*(i.y - j.y) + (i.z - j.z)*(i.z - j.z)));
        }
        //判断是否能组能三角形,两边之和大于第三边
        bool IsTriangle(node p1,node p2,node p3)
        {
            double a = distance(p1,p2);
            double b = distance(p1,p3);
            double c = distance(p2,p3);
            if(a+b>c||a+c>b||b+c>a)
                return true;
            else
                return false;
        }
        //判断颜色是否全部相同或全部不同
        bool Iscolor(node p1,node p2,node p3)
        {
            if(p1.c==p2.c&&p1.c==p3.c&&p2.c==p3.c)
            {
                return true;
            }
            else if(p1.c!=p2.c&&p1.c!=p3.c&&p2.c!=p3.c)
            {
                return true;
            }
            else
                return false;
        }
        //计算三角形面积
        double GetS(node p1,node p2,node p3)
        {
            double a = distance(p1,p2);
            double b = distance(p1,p3);
            double c = distance(p2,p3);
            double p = (a + b + c) / 2;
            double s;
            return sqrt(p * (p - a) * (p - b) * (p - c));
        }
    };
    
    int main()
    {
        int n;
        cin>>n;
        vector<node> tri;
        bool istri;
        bool isame;
        double s=0.0;
        double max_s = -1.0;
        for(int i=0;i<n;i++)
        {
            node n;
            cin>>n.c>>n.x>>n.y>>n.z;
            str.push_back(n);
        }
        Bear bear;
        for(int i=0;i<str.size();i++)
        {
            for(int j=i+1;j<str.size();j++)
            {
                for(int k=j+1;k<str.size();k++)
                {
                    istri = bear.IsTriangle(str[i],str[j],str[k]);
                    isame = bear.Iscolor(str[i],str[j],str[k]);
                   
                    if(istri&&isame)
                    {
                        s=bear.GetS(str[i],str[j],str[k]);
                    }
    
                    if(max_s<s)
                        max_s = s;
    
                }
            }
        }
        cout.setf(ios::fixed);
        cout <<fixed<< setprecision(5) << max_s <<endl;
    }
  • 相关阅读:
    error in ./src/views/demo/ueditor.vue Module build failed: Error: Cannot find module 'node-sass' Require stack:
    Spring Cloud Stream 定时任务消息延迟队列
    项目结构介绍
    Java面试题
    SpringBoot中用SpringSecurity实现用户登录并返回其拥有哪些角色
    MySQL索引优化
    MySQL中的执行计划explain
    SpringBoot之单体应用
    SpringBoot之SSM多模块应用
    Spring-aop面向切面编程笔记
  • 原文地址:https://www.cnblogs.com/omelet/p/6804231.html
Copyright © 2011-2022 走看看