programming-challenges Dermuba Triangle (111207) 题解
2015-10-30 02:02
197 查看
很考验细心的一道题。把各种情况分析清楚了,就不难写出对的程序了。
#include <iostream> #include <sstream> #include <fstream> #include <string> #include <vector> #include <list> #include <queue> #include <map> #include <set> #include <stack> #include <assert.h> #include <algorithm> #include <math.h> #include <ctime> #include <functional> #include <string.h> #include <stdio.h> #include <numeric> #include <float.h> using namespace std; struct Point { int x, y; Point(int ax, int ay) : x(ax), y(ay) {} }; int getRow(long long index) { long long r = sqrt(index); if (r * r == index) { return r; } else return r + 1; } Point getPoint(long long index) { int row = getRow(index); int lastRow = row - 1; int col = index - lastRow * lastRow; return Point(row, col - row); } int main() { long long e1, e2; double a = sqrt(3) / 3 / 2; double b = sqrt(3) / 3; while (cin >> e1) { cin >> e2; if (e1 > e2) { long long t = e2; e2 = e1; e1 = t; } Point p1 = getPoint(e1 + 1); Point p2 = getPoint(e2 + 1); double l = ((double)llabs(p1.y - p2.y)) / 2; double h; double h1 = 0, h2 = 0, h3 = 0; if (p2.x > p1.x) { if ((p1.x + p1.y) % 2 == 0) { h1 = b; } else { h1 = a; } if ((p2.x + p2.y) % 2 == 0) { h2 = a; } else { h2 = b; } h3 = (p2.x - p1.x - 1) * sqrt(3) / 2; h = h1 + h2 + h3; } else { if (abs(p1.y % 2) == abs(p2.y % 2)) { h = 0; } else { h = sqrt(3) / 6; } } printf("%.3f\n", sqrt((l * l + h * h))); } return 0; }
相关文章推荐
- Word Search
- programming-challenges Bee Maja (111204) 题解
- 夺命雷公狗jquery---46查找操作
- Android对Bitmap的内存优化
- HDU-1069-DP-Monkey and Banana
- js插件使用整理
- HDU 1754 I Hate It
- Git 教程(二) git 的基本构成及原理
- 获取当前的URL
- Session
- Swift 编程思想 阅读笔记
- Flask -- 路由
- C++中的关键字用法---typename
- Windows7 GNS3 模拟路由器成功PING通网外
- 基于AssetBundle的客户端资源
- Android merge优化UI
- 项目冲刺--第四天
- 论坛总结
- 多渠道打包资源
- 夺命雷公狗jquery---45包裹操作