非常烦人的题,思路比较简单,十分容易出错,细节非常重要。

从四个不同的行走方向讨论经过的每一个格子。

code:

  1. #include <iostream>
  2. #include <utility>
  3. #include <cmath>
  4. using namespace std;
  5. #define lb long double
  6. #define fi first
  7. #define se second
  8. pair<int , int > a, b;
  9. int n, tot;
  10. const lb eps = 1e-10;
  11. int floor2 (lb x)
  12. {
  13. if (ceil (x) - x < eps) return ceil (x);
  14. return floor (x);
  15. }
  16. int ceil2 (lb x)
  17. {
  18. if (x - floor (x) < eps) return floor (x);
  19. return ceil (x);
  20. }
  21. int main() {
  22. cin >> a.fi >> a.se >> b.fi >> b.se >> n;
  23. if (a.fi == b.fi || a.se == b.se) cout << "no solution";
  24. else {
  25. lb x = a.fi, y = a.se, lx = a.fi, ly = a.se;
  26. lb k = ( (lb) b.se - (lb) a.se) / ( (lb) b.fi - (lb) a.fi);
  27. //↗方向
  28. if (a.fi < b.fi && a.se < b.se) {
  29. while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
  30. lb dtx = min ( (floor2 (y) + 1 - y) / k, floor2 (x) + 1 - x);
  31. x += dtx, y += dtx * k;
  32. tot++;
  33. if (tot == n) break;
  34. lx = x, ly = y;
  35. }
  36. }
  37. //↙方向
  38. else if (a.fi > b.fi && a.se > b.se) {
  39. while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
  40. lb dtx = min ( (y - ceil2 (y) + 1 ) / k, x - ceil2 (x) + 1 );
  41. x -= dtx, y -= dtx * k;
  42. tot++;
  43. if (tot == n) break;
  44. lx = x, ly = y;
  45. }
  46. }
  47. //↘方向
  48. else if (a.fi < b.fi && a.se > b.se) {
  49. while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
  50. lb dtx = min ( (ceil2 (y) - 1 - y) / k, floor2 (x) + 1 - x);
  51. x += dtx, y += dtx * k;
  52. tot++;
  53. if (tot == n) break;
  54. lx = x, ly = y;
  55. }
  56. }
  57. //↖方向
  58. else if (a.fi > b.fi && a.se < b.se) {
  59. while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
  60. lb dtx = min ( (y - floor2 (y) - 1 ) / k, x - ceil2 (x) + 1);
  61. x -= dtx, y -= dtx * k;
  62. tot++;
  63. if (tot == n) break;
  64. lx = x, ly = y;
  65. }
  66. }
  67. if (tot < n) cout << "no solution";
  68. else
  69. cout << floor2 ( (lx + x) / 2) << ' ' << floor2 ( (ly + y) / 2);
  70. }
  71. return 0;
  72. }

  

SGU 150.Mr. Beetle II的更多相关文章

  1. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  2. 使用Flexible适配移动端html页面 - demo记录

    前段时间看了大神的博客文章[使用Flexible实现手淘H5页面的终端适配](地址:http://www.w3cplus.com/mobile/lib-flexible-for-html5-layou ...

  3. 笔记本电脑处理器(CPU)性能排行榜

    笔记本电脑处理器(CPU)性能排行榜 本排行榜随新款处理器(CPU)的发布而随时更新.更新日期:2012年7月15日   排名 型号 二级+三级缓存 前端总线(MHz) 功率(瓦) 主频(MHz) 核 ...

  4. 构造 - SGU 109 Magic of David Copperfield II

    Magic of David Copperfield II Problem's Link Mean: 略 analyse: 若i+j为奇数则称(i,j)为奇格,否则称(i+j)为偶格,显然每一次报数后 ...

  5. Lintcode 150.买卖股票的最佳时机 II

    ------------------------------------------------------------ 卧槽竟然连题意都没看懂,百度了才明白题目在说啥....我好方啊....o(╯□ ...

  6. sgu 109 Magic of David Copperfield II

    这个题意一开始没弄明白,后来看的题解才知道这道题是怎么回事,这道题要是自己想难度很大…… 你一开始位于(1,1)这个点,你可以走k步,n <= k < 300,由于你是随机的走的, 所以你 ...

  7. sgu 108 Self-numbers II

    这道题难在 hash 上, 求出答案很简单, 关键是我们如何标记, 由于 某个数变换后最多比原数多63 所以我们只需开一个63的bool数组就可以了! 同时注意一下, 可能会有相同的询问. 我为了防止 ...

  8. 150. Best Time to Buy and Sell Stock II【medium】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

随机推荐

  1. 利用python分析nginx日志

    最近在学习python,写了个脚本分析nginx日志,练练手.写得比较粗糙,但基本功能可以实现. 脚本功能:查找出当天访问次数前十位的IP,并获取该IP来源,并将分析结果发送邮件到指定邮箱. 实现前两 ...

  2. google font和@font-face

    会使用google字体 网址: http://www.google.com/fonts/ 选择字体, quick use 引用css: <link href='http://fonts.goog ...

  3. stringstream 与空格 (大家讨论一下代码结果的原因)

    #include <iostream> // std::cout, std::endl #include <iomanip> // std::setw #include < ...

  4. lightoj 1036 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1036 #include <cstdio> #include <cst ...

  5. Django的请求流程(url)

    一.Django是怎么处理请求的? 当你通过在浏览器里敲http://127.0.0.1:8000/hello/来访问Hello world消息得时候,Django在后台有些什么动作呢? 所有均开始于 ...

  6. Individual Contest #1 and Private Training #1

    第一次的增补赛,也是第一场个人排位赛,讲道理打的和屎一样,手速题卡了好久还WA了好多发,难题又切不出来,这种情况是最尴尬的吧! Individual Contest #1: Ploblem D: 题意 ...

  7. 【设计模式 - 8】之组合模式(Composite)

    1      模式简介 组合模式可以将对象以树形结构来表现"整体/部分"层次结构,让客户以一致的方式处理个别形象以及对象组合. 组合模式让我们能用树形方式创建对象的结构,树里面包含 ...

  8. 【Android - MD】之NavigationView的使用

    NavigationView是Android 5.0新特性--Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局). Nav ...

  9. 如何vs升级后10和12都能同时兼容

    如图: 项目2008解决方案sln文件升级2012后,都能同时使用. 升级办法:先复制vs2008版本的解决方案文件.升级2012后,再将文件复制到目录里面即可.注意升级过程中产生的升级文件(Upgr ...

  10. C程序内存管理

    C程序的内存管理 熟悉Java语言的肯定知道,Java中内存管理是由虚拟机帮助我们完毕的,在C/C++中可不是这样,程序猿须要自己去分配和回收内存空间.本文记录了C程序可运行文件的存储结构.在内存中的 ...