- 博客(140)
- 资源 (4)
- 收藏
- 关注
转载 一道经典的面试题:如何从N个数中选出最大(小)的n个数?
<br /><br />一道经典的面试题:如何从N个数中选出最大(小)的n个数?<br />北京交大LuoBin<br />这个问题我前前后后考虑了有快一年了,也和不少人讨论过。据我得到的消息,Google和微软都面过这道题。这道题可能很多人都听说过,或者知道答案(所谓的“堆”),不过我想把我的答案写出来。我的分析也许存有漏洞,以交流为目的。但这是一个满复杂的问题,蛮有趣的。看完本文,也许会启发你一些没有想过的解决方案(我一直认为堆也许不是最高效的算法)。在本文中,将会一直以寻找n个最“大”的数为分析例子,
2010-07-31 22:29:00 1199
转载 第(前)k大数问题
<br />所谓“第(前)k大数问题”指的是在长度为n(n>=k)的乱序数组中S找出从大到小顺序的第(前)k个数的问题。<br />解法1: 我们可以对这个乱序数组按照从大到小先行排序,然后取出前k大,总的时间复杂度为O(n*logn + k)。<br />解法2: 利用选择排序或交互排序,K次选择后即可得到第k大的数。总的时间复杂度为O(n*k)<br />解法3: 利用快速排序的思想,从数组S中随机找出一个元素X,把数组分为两部分Sa和Sb。Sa中的元素大于等于X,Sb中元素小于X。这时有两种情况:<b
2010-07-31 22:28:00 1162
原创 第K大数的位置...
<br />写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,<br />第一大的数是7,位置在4。第二大、第三大的数都是4,位置在1、3随便输出哪一个均可。 <br />函数接口为:int find_orderk(const int* narry,const int n,const int k)<br /> <br />//写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,//第一大的数是7,位置在4。第二大、第三大的数都是4,位置在
2010-07-31 22:26:00 1844
原创 POJ_1050(最大子矩阵和)
<br /> <br />#include <cstdlib>#include <iostream>using namespace std;const int MAX = 101;int value[MAX][MAX] = {{0,0}};int subMaxSum(int a[], int n){ int sum = 0, b = 0; for(int i=0; i<n; i++) { if(b > 0)
2010-07-31 15:34:00 1777
原创 最长公共子序列...
<br />#include <cstdlib>#include <iostream>#include <cstring>using namespace std;const int MAX = 100;int c[MAX][MAX] = {{0, 0}};int b[MAX][MAX] = {{0, 0}};int LCSLength(char *x, char *y){ int m = strlen(x) - 1; int n = strlen(y
2010-07-31 12:21:00 716
原创 POJ_1458(最长公共子序列)
<br />#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int MAX = 1000;int c[MAX][MAX];int LCSLength(char *x, char *y){ int m = strlen(x); int n = strlen(y); for(int i=1; i<=m; i++)
2010-07-31 12:10:00 802
原创 POJ_2503(基本Hash)
<br />#include <cstdlib>#include <iostream>#include <cstdio>using namespace std;const int MAX = 100000;const int HASH_SIZE = 100003;const int STR_LEN = 11;typedef struct _NODE{ int hashIndex; struct _NODE *next;}NODE, *PTRNODE;
2010-07-30 23:59:00 1334
转载 10个经典的字符串hash函数的C代码实现
<br /> <br />所有内容均来自于:http://www.partow.net/programming/hashfunctions/<br />以下总共给出了10种字符串hash函数,在上面的链接中可以找到各个Hash函数的描述,以及一些其它的关于hash函数的资料下载。<br />这些hash函数各自的优缺点不详。其中,ELFHash函数是在unix系统中被广泛使用的,也是《算法艺术与信息学竞赛》中作者推荐的一个字符串hash函数。DEKHash函数是Knuth在其《The Art of Comp
2010-07-30 22:20:00 1240
原创 POJ_2299(归并排序)
#include #include #include using namespace std;const int MAX = 500001;int value[MAX];int temp[MAX];__int64 result = 0;void merge(int left, int mid, int right){ int k = 0; int i=left; int j=mid + 1; while(i
2010-07-30 13:55:00 757
原创 直接插入排序。。。
<br />#include <cstdlib>#include <iostream>#include <ctime>#include <algorithm>using namespace std;const int MAX = 500;const int COUNT = 10;void display(int value[], int count){ for(int i=0; i<count; i++) cout << value[i] << "
2010-07-30 09:36:00 592
原创 POJ_2524(并查集应用)
#include #include #include using namespace std;const int MAX = 50001;int parent[MAX];int num[MAX];int maxNum = 0;void initUnion(int count){ for(int i=0; i
2010-07-29 23:13:00 580
原创 POJ_1611(并查集应用)
#include #include #include using namespace std;const int MAX = 30001;int parent[MAX];int num[MAX];void initUnion(int count){ for(int i=0; i
2010-07-29 22:19:00 788
原创 stl nth_element使用...
<br />#include <algorithm>#include <vector>#include <ctime>#include <iostream>using namespace std;const int MAX = 50;int main(int argc, char *argv[]){ vector<int> vec; int each; int count = 10; srand((unsigned)time(
2010-07-29 15:01:00 985
原创 POJ_2388
<br />用了三种方法实现的...<br />1.stl nth_element() 16MS<br />#include <cstdlib>#include <iostream>#include <algorithm>#include <cstdio>#include <vector>using namespace std;int main(int argc, char *argv[]){ int count; scanf("%d", &count);
2010-07-29 14:47:00 750
原创 堆排序实现...
<br />#include <cstdlib>#include <iostream>#include <algorithm>#include <ctime>using namespace std;const int MAX = 100;void filerDown(int heap[], int start, int end){ int i = start; int j = 2 * i + 1; int temp = heap[i];
2010-07-29 11:54:00 622
原创 堆的建立, 元素插入, 删除的实现...
<br />#include <cstdlib>#include <iostream>#include <algorithm>#include <ctime>#include <vector>using namespace std;const int MAX = 200;void filerDown(int heap[], int start, int end){ int i = start; int j = 2 * i + 1; int tem
2010-07-29 11:22:00 1848
转载 面试的经典问题
<br /> 面试的经典问题面试时,有几个问题是公司面试人员常常会提出的,针对这些问题好好准备,在面试时也就不会哑口无言,无言以对了,下面就面试十大必考题做出分析: <br /><br /> (1)为什么想进本公司? <br /><br /> 这通常是面试官最先问到的问题。此时面试官就开始评断录用与否了,建议大家先判断自己去应征的工作性质,是专业能力导向呢,或是需要沟通能力,其实现在市场多以服务为方向,所以口才被视为基本能力之一,所以在此时就要好好表现自己的口才,而口才较差者就务必表现出自己的专业能力
2010-07-28 10:30:00 671
原创 memmove实现...
<br />#include <cstdlib>#include <iostream>using namespace std;const int MAX = 100;void* __memmove(void* dest, const void* src, size_t n){ assert(dest != NULL && src != NULL); char *ptrDest = static_cast<char*>(dest); const c
2010-07-28 09:34:00 694
原创 用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回...
<br />#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int MAX = 256;int main(int argc, char *argv[]){ char ch[MAX]; cin >> ch; int size = strlen(ch) - 1; int halfSize = (size + 1) / 2
2010-07-28 09:24:00 2506
原创 对任意输入的正整数N,编写C程序求N!的尾部连续0的个数...
对任意输入的正整数N,编写C程序求N!的尾部连续0的个数,并指出计算复杂度。如:18!=6402373705728000,尾部连续0的个数 是3。(不用考虑数值超出计算机整数界限的问题)#include #include using namespace std;int getCount(int n){ int count = 0; for(int i=5; i
2010-07-27 23:25:00 4211
原创 求n的m次方的后三位...
<br />#include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]){ int x, y; cin >> x >> y; int result = 1; for(int i=0; i<y; i++) result = result * x % 1000; cout << resul
2010-07-27 22:46:00 7312 1
原创 递归求解最大公约数...
<br />#include <cstdlib>#include <iostream>using namespace std;int gcd(int m, int n){ if(m % n == 0) return n; return gcd(n, m % n);}int main(int argc, char *argv[]){ int m = 128, n = 100; cout << gcd(m, n)
2010-07-27 15:55:00 1025
转载 assert用法...
<br /> <br />ASSERT()是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式,如果表达式为FALSE (0), 程序将报告错误,并终止执行。如果表达式不为0,则继续执行后面的语句。这个宏通常原来判断程序中是否出现了明显非法的数据,如果出现了终止程序以免导致严重后果,同时也便于查找错误。例如,变量n在程序中不应该为0,如果为0可能导致错误,你可以这样写程序: <br /> ...... <br /> ASSERT( n != 0); <br /> k = 10/
2010-07-27 15:23:00 1139
转载 声明虚函数的限制...
【1】只能用virtual声明类的成员函数,使它成为虚函数,而不能将类外的普通函数声明为虚函数。 【2】一个成员函数被声明为虚函数后,在同一类族中的类就不能再定义一个非virtual的但与该虚函数具有相同参数(个数与类型)和函数返回值类型的同名函数。 【3】静态成员函数不能是虚函数,因为静态成员函数不受限于某个对象。 【4】inline函数不能是虚函数,因为inline函数是不能在运行中动态确定其位置的。即使虚函数在类的内部定义,编译时,仍将其视为非inline的。
2010-07-27 15:02:00 745
原创 strcpy, memcpy实现...
<br />static char * strcpy(char *dest, const char *src){ assert(dest != NULL && src != NULL); char *ret = dest; while ((*dest++ = *src++) != '/0'); return ret;} <br /> <br />memcpy实现:<br /> <br />#include <cstdlib>#include <iostre
2010-07-27 14:34:00 724
原创 ZOJ_1295
<br />#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int MAX = 80;int main(int argc, char *argv[]){ //freopen("input.txt", "rt", stdin); //freopen("output.txt", "wt", stdout); char ch[MAX
2010-07-27 11:03:00 876
原创 ZOJ_1241
<br />#include <cstdlib>#include <iostream>#include <cmath>#include <iomanip>using namespace std;inline void cal(int a, int b, int c, char ch){ float result = 0.0; if(c == -1) result = sqrt(a * a + b * b); else
2010-07-27 10:31:00 1131
原创 数字转string的几种方法...
<br />#include <cstdlib>#include <iostream>#include <string>#include <sstream>using namespace std;const int MAX = 100;/*frist method*/inline void numToString0(int value){ string str; char buf[MAX]; sprintf(buf, "%d", va
2010-07-27 09:12:00 10114
原创 ZOJ_1115
<br />#include <cstdlib>#include <iostream>#include <map>#include <sstream>using namespace std;char ch[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};int main(int argc, char *argv[]){ //freopen("input.txt", "rt", stdin); /
2010-07-27 00:04:00 1223
原创 ZOJ_2932
<br />#include <cstdlib>#include <iostream>#include <map>#include <string>using namespace std;char ch[7] = {' ', '!', '</p>, '%', '(', ')', '*'};string decode[7] = {"%20", "%21", "%24", "%25", "%28", "%29", "%2a"};const int MAX = 80;int mai
2010-07-26 23:39:00 735
转载 cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
<br /><br />1、cin<br />1、cin.get()<br />2、cin.getline()<br />3、getline()<br />4、gets()<br />5、getchar()<br />1、cin>> <br />用法1:最基本,也是最常用的用法,输入一个数字:<br />#include <iostream><br />using namespace std;<br />main ()<br />{<br />int a,b;<br />cin>>a>>b
2010-07-26 23:27:00 517
原创 ZOJ_2497
<br />#include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]){ //freopen("input.txt", "rt", stdin); // freopen("output.txt", "wt", stdout); int caseCount; cin >> caseCount; while
2010-07-26 17:28:00 685
原创 ZOJ_2886
<br />#include <cstdlib>#include <iostream>#include <string>#include <cstdio>using namespace std;int main(int argc, char *argv[]){ int caseCount; scanf("%d", &caseCount); while(caseCount-- > 0) { string input;
2010-07-26 16:49:00 711
原创 ZOJ_2487
<br />#include <cstdlib>#include <iostream>#include <string>using namespace std;int main(int argc, char *argv[]){ int caseCount; cin >> caseCount; while(caseCount-- > 0) { string input; cin >> input;
2010-07-26 15:07:00 639
原创 ZOJ_1383
<br />#include <cstdlib>#include <iostream>#include <bitset>using namespace std;int main(int argc, char *argv[]){ int caseCount; cin >> caseCount; while(caseCount-- > 0) { int value; cin >> value;
2010-07-26 14:41:00 978
原创 ZOJ_1240
<br />#include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]){ int caseCount; cin >> caseCount; int index = 1; while(caseCount-- > 0) { string inputStr; cin >> input
2010-07-26 14:26:00 955
原创 n后问题迭代, 递归解法...
<br />迭代回溯:<br />#include <cstdlib>#include <iostream>using namespace std;const int MAX = 100;bool isPosVaild(int res[], int row){ for(int i=1; i<row; i++) if(res[row] == res[i] || (abs(res[row] - res[i]) == abs(row - i)))
2010-07-26 11:52:00 622
原创 ZOJ_2812
<br />#include <cstdlib>#include <iostream>using namespace std;const int MAX = 256;int main(int argc, char *argv[]){ //freopen("input.txt", "rt", stdin); //freopen("output.txt", "wt", stdout); char input[MAX] = {'/0'};
2010-07-26 00:08:00 1245
原创 斐波那契数列高效递归解法...
#include #include using namespace std;const int MAX = 100;__int64 fac[MAX] = {0};__int64 fact(int n){ if(n 0) return fac[n]; fac[n] = fact(n - 1) + fact(n - 2); return fac[n];} int main(int
2010-07-25 23:03:00 1060
原创 n个数的所有组合...
<br />#include <cstdlib>#include <iostream>using namespace std;const int MAX = 10;int val[MAX];int res[MAX];int m;void getAssem(int l, int p){ for(int i=0; i<l; i++) cout << res[i] << " "; cout << endl; fo
2010-07-25 22:59:00 1215
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人