UVa 10182 Bee Maja (规律&O(1)算法)
2013-11-22 13:12
1606 查看
10182 - Bee Maja
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123
Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive consists of many hexagonal honey combs where the honey is stored in.
But bee Maja has a problem. Willi told her where she can meet him, but because Willi is a male drone and Maja is a female worker they have different coordinate systems.
Maja's Coordinate System | Willi's Coordinate System |
---|---|
Maja who often flies directly to a special honey comb has laid an advanced two dimensional grid over the whole hive. | Willi who is more lazy and often walks around just numbered the cells clockwise starting from 1 in the middle of the hive. |
Input Specification
The input file contains one or more integers which represent Willi's numbers. Each number stands on its own in a separate line, directly followed by a newline. The honey comb numbers are all less than 100 000.Output Specification
You should output the corresponding Maja coordinates to Willi's numbers, each coordinate pair on a separate line.Sample Input
1 2 3 4 5
Sample Output
0 0 0 1 -1 1 -1 0 0 -1
O(1)复杂度的思路:这道题如果只看左边那个图的话就会发现竖着的每一列纵坐标相同,而向左下方斜着的每一列纵坐标相同。但是这样并不能找出这些点的关系,其实换个角度,如果把左边的坐标在直角坐标系上画出来,就比较容易看出这是一个很有规律的一个图,只需要横纵坐标的加减就好了,至于加减多少那就看这个数所在的层数,层数可以根据x轴上的坐标的通项求出。
完整代码:
/*0.015s*/ #include<cstdio> #include<cmath> int main() { int n, m, i, j; while (~scanf("%d", &m)) { if (m == 1) { puts("0 0"); continue; } --m; n = ceil((sqrt(12 * m + 9) - 3) / 6); if (3 * n * (n + 1) == m) printf("%d %d\n", n, 0);///x轴为起点 else { --n;///前一个n m -= 3 * n * (n + 1); if (m <= n + 1) printf("%d %d\n", n - m + 1, m); else if (m <= (n + 1) << 1) { m -= (n + 1); printf("%d %d\n", -m, n + 1); } else if (m <= 3 * (n + 1)) { m -= (n + 1) << 1; printf("%d %d\n", -(n + 1), n + 1 - m); } else if (m <= (n + 1) << 2) { m -= 3 * (n + 1); printf("%d %d\n", -(n - m + 1), -m); } else if (m <= 5 * (n + 1)) { m -= (n + 1) << 2; printf("%d %d\n", m, -(n + 1)); } else if (m <= 6 * (n + 1)) { m -= 5 * (n + 1); printf("%d %d\n", n + 1, -(n + 1 - m)); } } } return 0; }
相关文章推荐
- UVA 10182 - Bee Maja
- UVa 375 Inscribed Circles and Isosceles Triangles (等腰三角形内切圆&规律)
- UVA - 11774 Doom's Day (规律)
- UVa 10254 The Priest Mathematician (组合数学&规律发现&高精度)
- UVa 10706 / POJ 1019 Number Sequence (打表&O(1)算法)
- uva 10881 Piotr's Ants 规律
- uva 1531 & poj 1518 Problem Bee(几何计算+贪心)
- UVa 10025 The ? 1 ? 2 ? ... ? n = k problem (数学&想法题&常数算法)
- UVA 11774 - Doom's Day(规律)
- 算法->完全背包问题 UVa 674 Coin Change
- UVa 808 (建坐标系、找规律) Bee Breeding
- 【数论 && 找规律】UVA 11752 The Super Powers
- hiho一下 第三周 "KMP"算法
- UVA 10404 Bachet's Game
- UVALive 3905-Meteor-扫描线算法
- 算法竞赛入门经典3.5 谜题UVa455
- uva 11795 Mega Man's Mission(动态规划-状态压缩DP)
- UVa 10007 & hdu 1131 Count the Trees (卡特兰数)
- UVA - 11795 Mega Man's Mission
- UVA 10905 Children's Game(数字组合求最大)