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

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

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. MVC 5 第一章 起航

    本章将讲述一些构建ASP.NET  MVC 5 web application的一些基础知识, 通过本章学习,你应该能够掌握到构建MVC 5应用程序的基本步骤,并且通过展示一个完整的MVC 5 hel ...

  2. poj 3620 Avoid The Lakes【简单dfs】

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6795   Accepted: 3622 D ...

  3. [置顶] [MATLAB技术贴]漫谈MATLAB矩阵转置

    矩阵转置是matlab最基本的操作了,但这个基本操作,也是很多初学者容易出现问题的地方.本帖通过几个实例演示matlab矩阵转置的操作. 方法一:'  运算符与  .'  运算符 >>a ...

  4. MySQL可视化管理工具 —— Navicat for MysSQL

    类似PL/SQL管理Oracle的工具 Navicat可以管理MySQL 1.安装 2.连接(输入IP.端口.用户名.密码) 3.新建sql语句:点击左侧database.点击菜单“查询”.点击“新建 ...

  5. java实现链表结构

    1. 定义节点node public class Node<T> { private Node<T> pre; private Node<T> next; priv ...

  6. ListView 分类: WinForm 2014-07-18 22:03 289人阅读 评论(0) 收藏

    一.ListView类(转载) 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLin ...

  7. nodejs端口被占用。

    I had the same issue. I ran: $ ps aux | grep node to get the process id, then: $ sudo kill -9 follow ...

  8. Adatper中获取宽高为0的问题

    但是我们想在getView()中获取ImageView的宽和高存在问题,在getView()里面刚开始显示item的时候利用ImageView.getWidth() 获取的都是0,为什么刚开始获取不到 ...

  9. [TypeScript] Understanding Generics with RxJS

    Libraries such as RxJS use generics heavily in their definition files to describe how types flow thr ...

  10. android 35 ListView增删改差

    MainActivity package com.sxt.day05_11; import java.util.ArrayList; import java.util.List; import and ...