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;
}
05-11 19:21