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 ...
随机推荐
- 转 MYSQL学习(一)
第一期主要是学习MYSQL的基本语法,陆续还有第二.第三.第四期,大家敬请期待o(∩_∩)o 语法的差异 我这里主要说语法的不同 1.默认约束 区别:mysql里面DEFAULT关键字后面是不用加括号 ...
- 【动态规划】拦截导弹_dilworth定理_最长递增子序列
问题 K: [动态规划]拦截导弹 时间限制: 1 Sec 内存限制: 256 MB提交: 39 解决: 10[提交][状态][讨论版] 题目描述 张琪曼:“老师,修罗场是什么?” 墨老师:“修罗是 ...
- phpstorm 8 license key
Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!XVcxFChJ2gTUR2lZtlLXrPLb ...
- hdu 1728:逃离迷宫(DFS,剪枝)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 蓝桥杯 入门训练 Fibonacci数列(水题,斐波那契数列)
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非 ...
- linux多种安装包格式的安装方法
linux多种安装包格式的安装方法 一.rpm包安装方式步骤:1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd s ...
- 解决来QQ消息后歌曲音量降低问题
今天学了一天,晚上听歌放松一下,谁知碰到了一个很纠结的问题,我正在聊天,每当来QQ消息后,我的歌曲音量自动降低,降到非常小,然后我就调高 音量,把音乐的音量调到最大,又把系统音量调到最大,谁知音乐的声 ...
- Java Hour 20 Spring
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为20 Hour,请各位不吝赐教. 今天心情不佳,只 ...
- Java Hour 12 Generic
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为12 Hour,请各位不吝赐教. 泛型程序设计 为 ...
- matlab练习程序(三角形内切圆)
三角形两角的角平分线就能确定内切圆. 结果如下: matlab代码如下: clear all;close all;clc; p=rand(,); %(x,y) v12=(p(,:)-p(,:))/no ...