Careercup - Microsoft面试题 - 5120588943196160
2014-05-10 22:58
原题:
Three points are given A(x1, y1), B(x2, y2), C(x3, y3). Write a method returning an array of points (x, y) inside the triangle ABC.
题目:给定三个点,找出所有这三点组成的三角形内的整点。(不能组成三角形也无所谓,结果为空即可。)
解法:出题者没有说是整点,但如果不是整点,就有无穷多个了。求整点的个数可以用Pick定律的公式。要求出所有整点的话,我的方法是找出一个内部的整点,然后向四个方向进行DFS,直到找出所有点。搜索过程中,需要判断点是否在三角形内部。我的判断方法是计算面积。对于整数问题,计算公式不要引入浮点数,误差是不必要的。所以海伦公式不可行,用行列式计算面积更为方便、准确。
代码:
// http://www.careercup.com/question?id=5120588943196160
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std; struct Point {
int x;
int y;
Point(int _x = , int _y = ): x(_x), y(_y) {};
}; struct hashFunctor {
size_t operator () (const Point &p) {
return p.x * + p.y;
};
}; struct equalFunctor {
bool operator () (const Point &p1, const Point &p2) {
return p1.x == p2.x && p1.y == p2.y;
};
}; typedef unordered_set<Point, hashFunctor, equalFunctor> point_set; int twoArea(int x1, int y1, int x2, int y2, int x3, int y3)
{
return abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
} bool inside(int x[], int y[], int px, int py)
{
int sum = ; sum += twoArea(x[], y[], x[], y[], px, py);
sum += twoArea(x[], y[], x[], y[], px, py);
sum += twoArea(x[], y[], x[], y[], px, py);
return sum == twoArea(x[], y[], x[], y[], x[], y[]);
} void DFS(int x[], int y[], int px, int py, point_set &um, point_set &visited)
{
static const int dir[][] = {
{-, },
{+, },
{, -},
{, +}
};
int i;
int newx, newy; visited.insert(Point(px, py));
um.insert(Point(px, py)); for (i = ; i < ; ++i) {
newx = px + dir[i][];
newy = py + dir[i][];
if (visited.find(Point(newx, newy)) == visited.end() &&
inside(x, y, newx, newy)) {
DFS(x, y, newx, newy, um, visited);
}
}
} void insidePoints(int x[], int y[], vector<Point> &points)
{
point_set um;
point_set visited;
int mx, my; mx = (x[] + x[] + x[]) / ;
my = (y[] + y[] + y[]) / ;
DFS(x, y, mx, my, um, visited); point_set::const_iterator usit;
for (usit = um.begin(); usit != um.end(); ++usit) {
points.push_back(Point(usit->x, usit->y));
}
um.clear();
visited.clear();
} int main()
{
int x[];
int y[];
vector<Point> points;
int i;
int n; while (cin >> x[] >> y[]) {
cin >> x[] >> y[];
cin >> x[] >> y[];
insidePoints(x, y, points);
n = (int)points.size();
for (i = ; i < n; ++i) {
cout << points[i].x << ' ' << points[i].y << endl;
}
points.clear();
} return ;
}
Careercup - Microsoft面试题 - 5120588943196160的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- 蘑菇街iOS客户端应用源码
蘑菇街iOS客户端应用源码 随着蘑菇街由导购向电商转型,蘑菇街自己的IM也应运而生,IM起初只是用于商家和买家之间沟通的工具.后面我们问自己,既然已经有了用于客服的IM,为什么不自己做一个IM,用于公 ...
- s3c6410_uart初始化及读写
参考: 1)<USER'S MANUAL-S3C6410X>第31章 UART 2)u-boot uart初始化及读写:u-boot-x.x.x/board/samsumg/smdk641 ...
- CSS3新增伪类
p:last-of-type 选择其父元素的最后的一个P元素 p:last-child 选择其父元素的最后子元素(一定是P才行) p:first-of-type ...
- Spark RDD的依赖解读
在Spark中, RDD是有依赖关系的,这种依赖关系有两种类型 窄依赖(Narrow Dependency) 宽依赖(Wide Dependency) 以下图说明RDD的窄依赖和宽依赖 窄依赖 窄依赖 ...
- php验证是否是md5编码的代码
php验证是否是md5编码的示例. 代码很简单,使用了正则表达式. function is_md5($password) { return preg_match("/^[a-z0-9 ...
- JS正则表达式获取字符串中特定字符
JS正则表达式获取字符串中得特定字符,通过replace的回调函数获取. 实现的效果:在字符串中abcdefgname='test'sddfhskshjsfsjdfps中获取name的值test 实 ...
- C++primer 阅读点滴记录(一)
第十三章 复制控制:(copy control) 复制构造函数(copy constructor) 复制操作符(assignment operator) ps: 什么时候需要显示的定义复制控制操作:类 ...
- Eclipse 4.6 Neon, could not create the java virtual machine
下了eclipse 4.6,打开报错:could not create the java virtual machine. a fatal exception has occurred. 命令行用 e ...
- 第十八章 数据访问(In .net4.5) 之 I/O操作
1. 概述 本章内容包括 文件操作.流操作.读写网络数据 以及 异步I/O操作. 2. 主要内容 2.1 文件操作 ① 使用 Drive 和 DriveInfo 访问磁盘信息. DriveInfo[] ...
- Java jdk环境变量配置
首先安装jdk,现在已经是jdk 8了,也不知道能不能好好用了...