SGU 150.Mr. Beetle II
非常烦人的题,思路比较简单,十分容易出错,细节非常重要。
从四个不同的行走方向讨论经过的每一个格子。
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的更多相关文章
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- 使用Flexible适配移动端html页面 - demo记录
前段时间看了大神的博客文章[使用Flexible实现手淘H5页面的终端适配](地址:http://www.w3cplus.com/mobile/lib-flexible-for-html5-layou ...
- 笔记本电脑处理器(CPU)性能排行榜
笔记本电脑处理器(CPU)性能排行榜 本排行榜随新款处理器(CPU)的发布而随时更新.更新日期:2012年7月15日 排名 型号 二级+三级缓存 前端总线(MHz) 功率(瓦) 主频(MHz) 核 ...
- 构造 - SGU 109 Magic of David Copperfield II
Magic of David Copperfield II Problem's Link Mean: 略 analyse: 若i+j为奇数则称(i,j)为奇格,否则称(i+j)为偶格,显然每一次报数后 ...
- Lintcode 150.买卖股票的最佳时机 II
------------------------------------------------------------ 卧槽竟然连题意都没看懂,百度了才明白题目在说啥....我好方啊....o(╯□ ...
- sgu 109 Magic of David Copperfield II
这个题意一开始没弄明白,后来看的题解才知道这道题是怎么回事,这道题要是自己想难度很大…… 你一开始位于(1,1)这个点,你可以走k步,n <= k < 300,由于你是随机的走的, 所以你 ...
- sgu 108 Self-numbers II
这道题难在 hash 上, 求出答案很简单, 关键是我们如何标记, 由于 某个数变换后最多比原数多63 所以我们只需开一个63的bool数组就可以了! 同时注意一下, 可能会有相同的询问. 我为了防止 ...
- 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 ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
随机推荐
- 利用python分析nginx日志
最近在学习python,写了个脚本分析nginx日志,练练手.写得比较粗糙,但基本功能可以实现. 脚本功能:查找出当天访问次数前十位的IP,并获取该IP来源,并将分析结果发送邮件到指定邮箱. 实现前两 ...
- google font和@font-face
会使用google字体 网址: http://www.google.com/fonts/ 选择字体, quick use 引用css: <link href='http://fonts.goog ...
- stringstream 与空格 (大家讨论一下代码结果的原因)
#include <iostream> // std::cout, std::endl #include <iomanip> // std::setw #include < ...
- lightoj 1036 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1036 #include <cstdio> #include <cst ...
- Django的请求流程(url)
一.Django是怎么处理请求的? 当你通过在浏览器里敲http://127.0.0.1:8000/hello/来访问Hello world消息得时候,Django在后台有些什么动作呢? 所有均开始于 ...
- Individual Contest #1 and Private Training #1
第一次的增补赛,也是第一场个人排位赛,讲道理打的和屎一样,手速题卡了好久还WA了好多发,难题又切不出来,这种情况是最尴尬的吧! Individual Contest #1: Ploblem D: 题意 ...
- 【设计模式 - 8】之组合模式(Composite)
1 模式简介 组合模式可以将对象以树形结构来表现"整体/部分"层次结构,让客户以一致的方式处理个别形象以及对象组合. 组合模式让我们能用树形方式创建对象的结构,树里面包含 ...
- 【Android - MD】之NavigationView的使用
NavigationView是Android 5.0新特性--Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局). Nav ...
- 如何vs升级后10和12都能同时兼容
如图: 项目2008解决方案sln文件升级2012后,都能同时使用. 升级办法:先复制vs2008版本的解决方案文件.升级2012后,再将文件复制到目录里面即可.注意升级过程中产生的升级文件(Upgr ...
- C程序内存管理
C程序的内存管理 熟悉Java语言的肯定知道,Java中内存管理是由虚拟机帮助我们完毕的,在C/C++中可不是这样,程序猿须要自己去分配和回收内存空间.本文记录了C程序可运行文件的存储结构.在内存中的 ...