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

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

code:

#include <iostream>
#include <utility>
#include <cmath>
using namespace std;
#define lb long double
#define fi first
#define se second
pair<int , int > a, b;
int n, tot;
const lb eps = 1e-10;
int floor2 (lb x)
{
if (ceil (x) - x < eps) return ceil (x);
return floor (x);
}
int ceil2 (lb x)
{
if (x - floor (x) < eps) return floor (x);
return ceil (x);
}
int main() {
cin >> a.fi >> a.se >> b.fi >> b.se >> n;
if (a.fi == b.fi || a.se == b.se) cout << "no solution";
else {
lb x = a.fi, y = a.se, lx = a.fi, ly = a.se;
lb k = ( (lb) b.se - (lb) a.se) / ( (lb) b.fi - (lb) a.fi);
//↗方向
if (a.fi < b.fi && a.se < b.se) {
while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
lb dtx = min ( (floor2 (y) + 1 - y) / k, floor2 (x) + 1 - x);
x += dtx, y += dtx * k;
tot++;
if (tot == n) break;
lx = x, ly = y;
}
}
//↙方向
else if (a.fi > b.fi && a.se > b.se) {
while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
lb dtx = min ( (y - ceil2 (y) + 1 ) / k, x - ceil2 (x) + 1 );
x -= dtx, y -= dtx * k;
tot++;
if (tot == n) break;
lx = x, ly = y;
}
}
//↘方向
else if (a.fi < b.fi && a.se > b.se) {
while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
lb dtx = min ( (ceil2 (y) - 1 - y) / k, floor2 (x) + 1 - x);
x += dtx, y += dtx * k;
tot++;
if (tot == n) break;
lx = x, ly = y;
}
}
//↖方向
else if (a.fi > b.fi && a.se < b.se) {
while (fabs (x - b.fi) > eps && fabs (y - b.se) > eps) {
lb dtx = min ( (y - floor2 (y) - 1 ) / k, x - ceil2 (x) + 1);
x -= dtx, y -= dtx * k;
tot++;
if (tot == n) break;
lx = x, ly = y;
}
}
if (tot < n) cout << "no solution";
else
cout << floor2 ( (lx + x) / 2) << ' ' << floor2 ( (ly + y) / 2);
}
return 0;
}

  

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. Delphi使用NativeXml访问XML文件

    Delphi使用NativeXml访问XML文件 1.创建XML文件var Doc: TNativeXml;//声明上下文对象var filepath:string;//文件路径DOC:=TNativ ...

  2. poj-1782 Run Length Encoding

    http://poj.org/problem?id=1782 Run Length Encoding Time Limit: 1000MS   Memory Limit: 30000K Total S ...

  3. pm grant 命令

    CustomLocale.apk所需要的权限"android.permission.CHANGE_CONFIGURATION"自Android 4.2,4.2.2起系统定义为and ...

  4. Win32中常用消息

    一 .WM_PAINT消息 1 WM_PAINT的产生   由于窗口的互相覆盖等,产生需要绘制的区域,那么会产生WM_PAINT消息. 一般情况下,不直接发送WM_PAINT消息,通过API声明需要绘 ...

  5. ssh互信自动化脚本(待更新)

    1.建立一个ip,端口,用户,密码列表 [root@localhost shell-key]# cat arg_list.txt 172.16.56.215 172.16.56.215 172.16. ...

  6. 《University Calculus》-chape8-无穷序列和无穷级数-基本极限恒等式

    基于基本的极限分析方法(诸多的无穷小以及洛必达法则),我们能够得到推导出一些表面上看不是那么显然的式子,这些极限恒等式往往会在其他的推导过程中用到,其中一个例子就是概率论中的极限定理那部分知识.

  7. 简单粗暴地理解 JavaScript 原型链

    尼玛!你特么也是够了! Don’t BB! Show me the code! function Person (name) { this.name = name; } function Mother ...

  8. 使用Calendar获取近三年的财务信息

    1.

  9. PMD使用手册

    文中测试例子均采用JHotDraw7源码,官网关于 JHotDraw的检测结果:http://pmd.sourceforge.net/reports/jhotdraw_JHotDraw.html 下载 ...

  10. Web App和Native App 谁将是未来

    未来是Web App的天下,还是Native App的天下?作为设计师,我们是应该努力把客户端的体验提升到最优,还是在网页应用层面上做更多的设计?这个一直是大家关心的话题.那么,我们首先应该立体的认识 ...