life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
re: 深信服笔试(转) life02 2009-10-17 20:22
void Divide(int array[], int n)
{
int i = 0;
for (int j = 0; j < n; j++)
{
if (array[j] < 0)
{
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
}
}
}
@zhaoyg
#include <iostream>
#include<string.h>
#include<ctype.h>
#include<malloc.h> //malloc()等
#include<limits.h> // INT_MAX等
#include<io.h> // eof()
#include<math.h>
using namespace std;

// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
// #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int Boolean; // Boolean是布尔类型,其值是TRUE或FALSE

typedef int SElemType; // 定义栈元素类型为整型
typedef struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针 */
int stacksize; // 当前已分配的存储空间,以元素为单位
}SqStack; // 顺序栈

//bo3-1.c 顺序栈(存储结构由c3-1.h定义)的基本操作(9个)
Status InitStack(SqStack *S,int Init_SIZE)
{ // 构造一个空栈S
(*S).base=(SElemType *)malloc(Init_SIZE*sizeof(SElemType));
if(!(*S).base)
exit(OVERFLOW); /* 存储分配失败 */
(*S).top=(*S).base;
(*S).stacksize=Init_SIZE;
return OK;
}

Status DestroyStack(SqStack *S)
{ /* 销毁栈S,S不再存在 */
free((*S).base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stacksize=0;
return OK;
}

Status ClearStack(SqStack *S)
{ /* 把S置为空栈 */
(*S).top=(*S).base;
return OK;
}

Status StackEmpty(SqStack S)
{ /* 若栈S为空栈,则返回TRUE,否则返回FALSE */
if(S.top==S.base)
return TRUE;
else
return FALSE;
}

int StackLength(SqStack S)
{ /* 返回S的元素个数,即栈的长度 */
return S.top-S.base;
}

Status GetTop(SqStack S,SElemType *e)
{ /* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */
if(S.top>S.base)
{
*e=*(S.top-1);
return OK;
}
else
return ERROR;
}

Status Push(SqStack *S,SElemType e)
{ /* 插入元素e为新的栈顶元素 */
if((*S).top-(*S).base>=(*S).stacksize) /* 栈满,追加存储空间 */
{
(*S).base=(SElemType *)realloc((*S).base,((*S).stacksize+2)*sizeof(SElemType));
if(!(*S).base)
exit(OVERFLOW); /* 存储分配失败 */
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=2;
}
*((*S).top)++=e;
return OK;
}

Status Pop(SqStack *S,SElemType *e)
{ /* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
if((*S).top==(*S).base)
return ERROR;
*e=*--(*S).top;
return OK;
}

Status StackTraverse(SqStack S,Status(*visit)(SElemType))
{ /* 从栈底到栈顶依次对栈中每个元素调用函数visit()。 */
/* 一旦visit()失败,则操作失败 */
while(S.top>S.base)
visit(*S.base++);
printf("\n");
return OK;
}
Status visit(SElemType c)
{
printf("%d ",c);
return OK;
}

void spell(int num){

SqStack s;
InitStack(&s,(num/2+1));
int i=0;
int sum=0;
int j=0;
while(i<=(num/2+1)){
if (sum<num)
{
i++;
Push(&s,i);
sum+=*(s.top-1);

}else if (sum>num)
{
sum-=*(s.base);
s.base++;

}else{
int* temp=s.base;
int k=0;
/* cout<<"hell"<<endl;*/
while(k<StackLength(s)){
cout<<*temp<<" ";
temp++;
k++;
}
cout<<endl;
i++;
Push(&s,i);
sum+=*(s.top-1);

}
}

}

int main(){
spell(1245);

return 0;
}
re: 笔试题 life02 2009-09-18 10:09
#include <iostream>
#include <stdio.h>
#include <assert.h>
using namespace std;


static unsigned int *pindex;

#define L 10000001
unsigned get_bigger_count(unsigned value)
{
assert(value<L-1);
static binit(false);
unsigned int rval(0);
if(!binit)
{
FILE *fp = fopen("datafile", "r+b");
unsigned int i = 0;
unsigned int shu;
pindex = new unsigned int[L];
memset(pindex, 0, sizeof(unsigned int)*L);

while(fscanf(fp,"%ld",&shu)!=-1){
/* cout<<shu<<endl;*/
pindex[shu]++;
}
fclose(fp);
for(i=L-1; i>0; --i)
pindex[i-1] += pindex[i];
}
binit = true;
rval = pindex[value+1];
return rval;
}




int main(){
unsigned un;
un=get_bigger_count(8738787);
cout<<un<<endl;
return 0;
}
re: 笔试题 life02 2009-09-14 18:16
第三题正解如下:
这题很明显已经告诉各位了,算法复杂度是o(1),任何其他排序算法都是错的,只有桶排序可行,原因也很明显,符合桶排序的特征,0到1000万的取值范围已经固定了,相对10亿来说1000万是很小了。

所以只要设立0到1000万的桶,中间附加计数即可,什么意思呢,10亿个数据,如果缩小一点:范围是0到10的话
0,1,2,3,4,5,6,7,8,9,10

在每一个桶下面挂一个count,扫描完后,10亿个数据肯定都在这10个桶里(1000万类似)

这样在接下来查询的时候,其实比较空间只有0到1000万,和10亿一点关系都没有,而且每个桶还帮你记着大于该数的个数,所以假定要比较的数值是5,则结果就是5,6,7,8,9,10的count之和。

计算空间最大浪费就是在0到1000万里面,找到你的那个数值,但是总比N要小很多,这个结论拿出去,面试来说,应该可以得满分,至于效率,空间什么的,就自己优化1000万数据的比较算法啦。
re: [翻译]游戏主循环 life02 2009-09-04 14:17
好文,学习下