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 ...
随机推荐
- Java如何读取XML文件 具体实现
转载自:http://www.jb51.net/article/44338.htm import java.io.*; import javax.xml.parsers.DocumentBuilder ...
- oracle的数据库,随笔
不多说,看代码 select b.*,a.kscj,a.paiming from (select t.kch,t.kcm,t.kscj,t.xh, rank() over (order ...
- 大神的游戏(codevs 1353)
题目描述 Description 在那遥远的机房,有一片神奇的格子.为了方便起见,我们编号为1~n.传说只要放入一些卡片,就能实现愿望.卡片一共有m种颜色,但是相邻的格子间不能放入相同颜色的卡片.只要 ...
- [hihoCoder] 博弈游戏·Nim游戏
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他们在玩一个取石子的游戏.在 ...
- 自定义viewgroup实现ArcMenu
最终效果如下 实现思路 通过效果图,会有几个问题: a.动画效果如何实现 可以看出动画是从顶点外外发射的,可能有人说,那还不简单,默认元素都在定点位置,然后TraslateAnimation就好了:这 ...
- java基础知识回顾之java Thread类学习(十二)-- 线程中断
官方文档翻译: 如果本线程是处于阻塞状态:调用线程的wait(), wait(long)或wait(long, int)会让它进入等待(阻塞)状态,或者调用线程的join(), join(long), ...
- Xamarin.Android开发实践(七)
Xamarin.Android广播接收器与绑定服务 一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架 ...
- Java Hour 27 Concurrent
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 27 Hours. 本小时主要过一下多线程相关的基本API. Defining ...
- Azure Mobile Services的REST API调用方式和自定义API
Azure Mobile Services(移动服务)是微软在Azure平台中提供的一种跨平台的移动应用后端服务,即移动后端即服务.支持.NET和JavaScript(Node.js)写后端代码:支持 ...
- chromium的Backtrace记录
ffmpeg处理完视频流后,上层的webrtc调用错误,可以看出webrtc的调用过程: Backtrace: webrtc::RTPFragmentationHeader::CopyFrom [0x ...