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的更多相关文章

  1. Careercup | Chapter 1

    1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...

  2. Careercup | Chapter 3

    3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...

  3. Careercup | Chapter 2

    链表的题里面,快慢指针.双指针用得很多. 2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow w ...

  4. Careercup | Chapter 8

    8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director ...

  5. 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. ...

  6. 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 ...

  7. CareerCup Chapter 4 Trees and Graphs

    struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),rig ...

  8. Careercup | Chapter 6

    6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...

  9. 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 ...

随机推荐

  1. Android Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法

    在Android开发中,ListView有着很重要的地位,使用的场合也非常的多 错误提示:Your content must have a ListView whose id attribute is ...

  2. CUDA学习笔记(一)——CUDA编程模型

    转自:http://blog.sina.com.cn/s/blog_48b9e1f90100fm56.html CUDA的代码分成两部分,一部分在host(CPU)上运行,是普通的C代码:另一部分在d ...

  3. 第一套增删改查(htm+ashx完成)

    1.展示人员列表htm文件: <a href="PersonEditAddNew.ashx?action=addnew">添加</a> </p> ...

  4. 一、HTML和CSS基础--网页布局--如何用css进行网页布局

    什么叫做布局? 又称版式布局,是网页UI设计师将有限的视觉元素进行有机的排列组合. 网页设计的特点 网页可以自适应宽度 网页的高度理论上可以无限延长 网页分栏 分栏又称为分列,常见的布局分为:一列布局 ...

  5. ytu 1041: 迭代法求平方根(水题)

    1041: 迭代法求平方根 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 227  Solved: 146[Submit][Status][Web Bo ...

  6. linux tricks 之 FIELD_SIZEOF.

    ------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...

  7. Git 分布式版本管理

    Git是分布式版本控制系统,我们常用的版本控制工具还有SVN.这里就得区分下什么是分布式版本控制系统,什么是集中化的版本控制系统. 集中化的版本控制系统 集中化的版本控制系统( Centralized ...

  8. M方法和D方法的区别

    M方法和D方法的区别 ThinkPHP 中M方法和D方法都用于实例化一个模型类,M方法 用于高效实例化一个基础模型类,而 D方法 用于实例化一个用户定义模型类. 使用M方法 如果是如下情况,请考虑使用 ...

  9. hdu 5312 数学

  10. SQLServer2008默认服务配置

    SQLServer2008默认服务配置