微软2014编程之美初赛第一场——题目3 : 活动中心
生活随笔
收集整理的這篇文章主要介紹了
微软2014编程之美初赛第一场——题目3 : 活动中心
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【來源】
題目3 : 活動中心
【分析】
本題採用的是三分法。
輸入的一組點中找出左右邊界。作為起始邊界。 while(右邊界-左邊界<精度){將左右邊界構成的線段均勻分成3段,推斷切割點的距離關系,抹去距離大的一段。更新左右邊界。 } 輸出左(右)邊界【代碼】
#include <iostream> #include <vector> #include <cmath> #include <iomanip> using namespace std;struct Point {int x;int y; };double calc(double x, vector<Point> points) {double distance = 0;for (int i = 0; i < points.size(); ++i){double d = (points[i].x - x)*(points[i].x - x) + (points[i].y)*(points[i].y);distance += sqrt(d);}return distance; }int main() {int T;cin >> T;for(int casenum = 0; casenum < T; ++casenum){int N;cin >> N;vector<Point> points;Point p;double maxX = -1000000;double minX = 1000000;for (int i = 0; i < N; ++i){cin >> p.x >> p.y;if (p.x < minX){minX = p.x;}if (p.x > maxX){maxX = p.x;}points.push_back(p);}double left = minX;double right = maxX;double m1, m2;while (right - left >= 5e-8){m1 = (left * 2 + right) / 3.0;m2 = (right * 2 + left) / 3.0;double v1 = calc(m1, points);double v2 = calc(m2, points);if (v1 < v2){right = m2;}else{left = m1;}}cout << fixed << setprecision(8);cout << "Case " << casenum+1 << ": " << left << endl;}//system("pause");return 0; }【點評】
本題與去年編程之美的題目集會很相似。
解法也比較雷同。
【備注】
本代碼小數據AC,大數據WA。
總結
以上是生活随笔為你收集整理的微软2014编程之美初赛第一场——题目3 : 活动中心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络课程设计实验报告
- 下一篇: 一个托盘程序演示 -闹钟 Alert