您的位置:首页 > 编程语言

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

2017-06-20 16:46 495 查看
寻找三角形

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: