Bloom Filter 讲解:http://blog.csdn.net/jiaomeng/article/details/1495500
-----------------------------------------------------------------------------------------------------------------------
Programming Question - 6
-----------------------------------------------------------------------------------------------------------------------第一题的代码
#include <iostream>
#include <fstream>
#define MAX 1000000
#define MIN 4000
using namespace std;
int hash[MIN + 1] = { 0 };
int count = 0;
void readData() {
ifstream fin("HashInt.txt");
int temp = 0;
while(fin>>temp) {
if(temp < MIN) hash[temp]++;
}
}
bool hashMap(int n) {
if(n > MIN) return false;
if(hash[n]) return true;
else return false;
}
int main() {
readData();
for(int i = 2500; i <= 4000; i++) {
for(int j = 1; j <= (i - 1) / 2; j++) {
if(hashMap(j) && hashMap(i - j)) {
count++;
break;
}
}
}
cout<<count<<endl;
return 0;
}
第二题的思路:http://www.cnblogs.com/lienhua34/archive/2011/12/06/2381299.html
利用大顶堆+小顶堆的方法应该是最高效的
main.cpp的代码, 我的“heap.h”用到的是这里的 http://blog.csdn.net/neostar2008/article/details/7769058
#include <iostream>
#include <vector>
#include <fstream>
#include "heap.h"
using namespace std;
int main() {
ifstream fin("Median.txt");
MaxHeap maxh;
MinHeap minh;
int medianSum = 0;
int temp;
while(fin>>temp) {
if(temp < maxh.top()) maxh.insert(temp);
else minh.insert(temp);
if(maxh.heapSize() - minh.heapSize() > 1) {
minh.insert(maxh.extractMax());
}
if(minh.heapSize() > maxh.heapSize()) {
maxh.insert(minh.extractMin());
}
medianSum += maxh.top();
}
cout<<medianSum % 10000<<endl;
return 0;
}