百度2017春招笔试真题编程题集合

寻找三角形

#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include<algorithm>
#include <string>

using namespace std;

bool isSameColor(vector<int>&a, vector<int>&b, vector<int>&c){
    if (a.size() < 4 || b.size() < 4 || c.size() < 4)
        return false;
    if (a[0] == b[0] && a[0] == c[0] && b[0] == c[0])
        return true;
    if (a[0] != b[0] && a[0] != c[0] && b[0] != c[0])
        return true;
    return false;
}

double distance99(vector<int>&a, vector<int>&b){
    if (a.size() < 4 || b.size() < 4)
        return -1.0;
    return sqrt((a[1] - b[1])*(a[1] - b[1]) + (a[2] - b[2])*(a[2] - b[2]) + (a[3] - b[3])*(a[3] - b[3]));
}





int main(){
    int num;
    while (cin >> num){
        // 1.存储各点
        vector<vector<int>> points;
        double maxArea = 0;
        for (int i = 0; i < num; ++i){
            string c;
            int color, x, y, z;
            cin >> c;
            cin >> x >> y >> z;
            if (c == "R")
                color = 1;
            else if (c == "G")
                color = 2;
            else
                color = 3;
            vector<int> onePoint{ color, x, y, z };
            points.push_back(onePoint);
        }
        // 2.暴力循环
        for (auto it1 = points.begin(); it1 != points.end(); ++it1){
            for (auto it2 = it1 + 1; it2 != points.end(); ++it2){
                for (auto it3 = it2 + 1; it3 != points.end(); ++it3){
                    // 检查颜色是否相同
                    if (isSameColor(*it1, *it2, *it3)){


                        // 检查是否能构成三角形
                        double a = distance99(*it1, *it2);
                        double b = distance99(*it1, *it3);
                        double c = distance99(*it2, *it3);
                        if (a + b > c && a + c >b && b + c > a){
                            double s = (a + b + c) / 2;
                            double area = sqrt(s*(s - a)*(s - b)*(s - c));
                            if (area > maxArea)
                                maxArea = area;
                        }
                    }
                }
            }
        }
        printf("%.5f\n", maxArea);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值