Careercup | Chapter 7
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only the add operator.
比较简单。但是要封装得好。
7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in half. Assume that the top and the bottom sides of the square run parallel to the x-axis.
怎样写得简洁。要解决:
1. 怎么把多种情况综全考虑?
这类题就是先把special case想法,再写算法。
7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of points.
见此。当时没考虑精度的问题。
int findMaxLine(vector<int> &points) {
int max = ;
int dup = ;
map<int, int> counts;
double epison = 0.0001;
for (int i = ; i < points.size(); ++i) {
counts.clear();
dup = ;
int m = ;
for (int j = i + ; j < points.size(); ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
dup++;
} else if (points[i].x == points[j].x) {
counts[]++;
if (counts[] > m) m = counts[];
} else {
double k = (points[i].y - points[j].y) * 1.0 / (points[i].x - points[j].x);
counts[(int)(k/epison)]++;
if (counts[int)(k/epison)] > m) m = counts[int)(k/epison)];
}
}
if (m + dup > max) max = m + dup;
}
return max;
}
7.7 Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7.
int findKthMagicNumber(int k) {
vector<queue<int> > queues();
queues[].push();
for (int i = ; i < k; ++i) {
int minIndex = , minNumber;
for (int j = ; j < ; ++j) {
if (!queues[j].empty() && queues[j].front() < queues[minIndex].front()) minIndex = j;
}
minNumber = queues[minIndex].front();
for (int j = minIndex; j < ; ++j) {
queues[j].push(minNumber * nums[j]);
}
queues[minIndex].pop();
}
return minNumber;
}
Careercup | Chapter 7的更多相关文章
- Careercup | Chapter 1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...
- Careercup | Chapter 3
3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...
- Careercup | Chapter 2
链表的题里面,快慢指针.双指针用得很多. 2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow w ...
- Careercup | Chapter 8
8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director ...
- CareerCup Chapter 9 Sorting and Searching
9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. ...
- CareerCup chapter 1 Arrays and Strings
1.Implement an algorithm to determine if a string has all unique characters What if you can not use ...
- CareerCup Chapter 4 Trees and Graphs
struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),rig ...
- Careercup | Chapter 6
6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...
- Careercup | Chapter 5
5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to inse ...
随机推荐
- nginx服务器的网站权限问题
有时候我们的网站根目录会从一个目录迁移到另一个目录,如果我们服务器使用的是nginx或者Apache,我们一般会配置好网站根目录后然后往直接把网站解压或者上传到根目录中,这样引起的问题是无法对对文件进 ...
- 【动态规划】盖房子(house)--未提交--已提交
问题 D: 盖房子(house) 时间限制: 1 Sec 内存限制: 64 MB提交: 27 解决: 16[提交][状态][讨论版] 题目描述 FJ最近得到了面积为n*m的一大块土地,他想在这块土 ...
- BestCoder12 1002.Help him(hdu 5059) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...
- Car的旅行路线(codevs 1041)
题目描述 Description 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第I ...
- linux安装gcc
方法:输入命令: :(1) :yum -y install gcc (2) yum -y install gcc-c++(3)yum install make 我的再输入 yum -y instal ...
- 自定义viewgroup实现ArcMenu
最终效果如下 实现思路 通过效果图,会有几个问题: a.动画效果如何实现 可以看出动画是从顶点外外发射的,可能有人说,那还不简单,默认元素都在定点位置,然后TraslateAnimation就好了:这 ...
- Java Hour 12 Generic
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为12 Hour,请各位不吝赐教. 泛型程序设计 为 ...
- LinQ的一些基本语句
LINQ(Language Integrated Query)即语言集成查询.它是一种语言特性和API,使得你可以使用统一的方式编写各种查询,查询的对象包括xml.对象集合.SqlServer数据库等 ...
- Android基础环境搭建
1.安装JDK 从官网下载JDK安装, http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...
- COGS731 [网络流24题] 最长递增子序列(最大流)
给定正整数序列x1,..., xn (n<=500).(1)计算其最长递增子序列的长度s.(2)计算从给定的序列中最多可取出多少个长度为s的递增子序列.(3)如果允许在取出的序列中多次使用x1和 ...