百度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; }
相关文章推荐
- 4--百度2017春招笔试真题编程题集合--有趣的排序 (Python)
- 百度2017春招笔试真题编程题集合之寻找三角形
- 2--百度2017春招笔试真题编程题集合--度度熊回家 (Python)
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合:不等式数列 [python]
- 百度2017春招笔试真题编程题集合:买帽子 [python]
- 百度2017春招笔试真题编程题集合之买帽子
- 1--百度2017春招笔试真题编程题集合--买帽子(Python)
- 百度2017春招笔试真题编程题集合:寻找三角形 [python]
- 百度2017春招笔试真题编程题集合-5
- 百度2017春招笔试真题编程题集合--度度熊回家
- 百度2017春招笔试真题编程题集合:度度熊回家 [python]
- 百度2017春招笔试真题编程题集合:有趣的排序 [python]
- 百度2017春招笔试真题编程题集合-1
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合-2
- 百度2017春招笔试真题编程题集合--买帽子
- 百度2017春招笔试真题编程题集合--寻找三角形
- 百度2017春招笔试真题编程题集合
- 百度2017春招笔试真题编程题集合-3