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 ...
随机推荐
- Ajax清除浏览器js、css、图片缓存的方法
做东东时都是把图片在服务器的地址存放在数据库里面,然后到浏览器中显示,但是后来发现了两个问题. 第一:为了安全起见,js是无法读取本地的图片的,不然你写一个js,岂不是可以获取任何人电脑里面的文件了. ...
- POJ C++程序设计 编程题#3 编程作业—多态与虚函数
编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出 ...
- 泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- Win7下 httpRequest带证书请求https网站
常规情况下创建Web请求,并获取请求数据的代码如下: WebRequest req = WebRequest.Create(url); req.Timeout = 15000; WebResponse ...
- 将List<T>转化成 DataTable--调整可空类型的转化错误
加载表结构并保持成XML string cmdText = @"select * from kb_lable_temp where 1=2"; using (SqlConnecti ...
- jquery中的mouseenter实现理解
说在前面:首先说一下两者之间的区别,假设当前元素为element,mouseover事件具有冒泡特性,也就是说无论鼠标是从别的元素移动到element或者是从element的子元素移动到element ...
- (转)Android如何编程设置APP安装位置(外部存储或内部存储)?
Beginning with API Level 8, you can allow your application to be installed on the external storage ( ...
- Android———从GitHub上下载源码的方法【Written By KillerLegend】
首先声明,本文说的是从GitHub上下载源码而非上传源码! 1:下载tortoisegit,下载地址为: https://code.google.com/p/tortoisegit/wiki/Down ...
- 【转】利用DCC32实现命令行批量编译
*.dof [Compiler] A=1 B=0 C=1 D=1 E=0 F=0 G=1 H=1 I=1 J=1 K=0 L=1 M=0 N=1 O=1 P=1 Q=0 R=0 S=0 T=0 U=0 ...
- 限制<input>输入内容 只允许数字 或者 字母
只能输入数字: 有回显 <input onkeyup="value=value.replace(/[^\d]/g,'')"> 只能输入数字:无回显 <input ...