程序员考试试题.docx
- 文档编号:3399464
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:5
- 大小:17.24KB
程序员考试试题.docx
《程序员考试试题.docx》由会员分享,可在线阅读,更多相关《程序员考试试题.docx(5页珍藏版)》请在冰豆网上搜索。
程序员考试试题
程序员考试下午试题(模拟)
一、把一个字符串插入到另一个字符串的某个位置(指元素个数)之后
char*insert(char*s,char*t,intposition)
{inti;
char*target;
if(position>strlen(t))printf("error");
else
{for(i=0;i<
(1);i++)
{if(i target[i]=s[i]; else {if(i< (2)) target[i]=t[i]; else(3); } } } returngarget; } 二、辗转相除法求两个正整数的最大公约数 intf(inta,intb) {if(a==b)(4); else {if(a>b)returnf(a-b,b); else(5); } } 三、求一个链表的所有元素的平均值 typedefstruct{intnum; floatave; }Back; typedefstructnode{floatdata; structnode*next; }Node; Back*aveage(Node*head) {Back*p,*q; p=(Back*)malloc(sizeof(Back)); if(head==NULL) {p->num=0; p->ave=0;} else {(6); p->num=q->num+1; (7);} retuenp; } main() {Node*h;Back*p; h=create();/*建立以h为头指针的链表*/ if(h==NULL)printf("没有元素"); else{p=aveage(h); printf("链表元素的均值为: %6f",p->ave); } } 四、希尔排序 已知待排序序列data[n];希尔排序的增量序列为d[m],其中d[]序列降序排列,且d[m-1]=1。 其方法是对序列进行m趟排序,在第i趟排序中,按增量d[i]把整个序列分成d[i]个子序列,并按直接插入排序的方法对每个子序列进行排序。 希尔排序的程序为: voidshellsort(int*data,int*d,intn,intm) {inti,j; for(i=0;i for(j=0; (1);j++) shell( (2)); } voidshell(int*data,intd,intnum,intn) {inti,j,k,temp; for(i=1;(3);i++) {j=0; temp=data[j+i*d]; while((j j++; for(k=j;k data[k+1]=data[k]; (5); (6)} } 五、求树的宽度 所谓宽度是指在二叉树的各层上,具有结点数最多的那一层上的结点总数。 本算法是按层次遍历二叉树,采用一个队列q,让根结点入队列,最后出队列,若有左右子树,则左右子树根结点入队列,如此反复,直到队列为空。 intWidth(BinTree*T) {intfront=-1,rear=-1;/*队列初始化*/ intflag=0,count=0,p;/*p用于指向树中层的最右边的结点,flag记录层中结点数的最大值。 */ if(T! =Null) {rear++; (1);flag=1;p=rear; } while( (2)) {front++; T=q[front]; if(T->lchild! =Null) {rear++;(3);count++;}// if(T->rchild! =Null) {rear++;q[rear]=T->rchild;(4);} if(front==p)/*当前层已遍历完毕*/ {if((5))flag=count;count=0;// p=rear;/*p指向下一层最右边的结点*/ } } return(flag); } 六、区间覆盖 设在实数轴上有n个点(x0,x1,……,xn-2,xn-1),现在要求用长度为1的单位闭区间去覆盖这n个点,则需要多少个单位闭区间。 intcover(floatx[],intnum) {floatstart[num],end[num]; inti,j,flag,count=0; for(i=0;i {flag=1; for(j=0;j< (1);j++) {if((start[j]>x[i])&&(end[j]-x[i]<=1)) (2); elseif((3))end[j]=x[i]; elseif((x[i]>start[j])&&(x[i] if(flag)break; } if((4)) {end[count]=x[i];(5);count++;} } returncount-1; } start[count]=x[i] 七、围棋中的提子 在围棋比赛中,某一方(假设为黑方)在棋盘的某个位置(i,j)下子后,有可能提取对方(白方的一串子)。 以W[19][19]表示一个棋盘,若W[i][j]=0表示在位置(i,j)上没有子,W[i][j]=1表示该位置上的是黑子,W[i][j]=-1表示该位置上是白子。 可以用回溯法实现提子算法。 下列程序是黑棋(tag=1)下在(i,j)位置后判断是否可以吃掉某些白子,这些确定可以提掉的白子以一个线性表表示。 问题相应的数据结构有: #definelength19/*棋盘大小*/ #definemax_num361/*棋盘中点的数量*/ structposition{introw;intcol; };/*棋子位置*/ structkilled{structpositiondata[max_num];intnum; }*p;/*存储可以吃掉的棋子位置*/ structstack{structpositionnode[max_num];inttop; };/*栈*/ intw[length][length];/*棋盘中双方的棋子分布*/ intvisited[length][length];/*给已搜索到的棋子位置作标记,初值为0,搜索到后为1*/ structkilled*kill(intw[length][length],intr,intc,inttag) {structkilled*p; structposition*s; structstackS; for(i=0;i for(j=0;j (1); S.top=-1;p->num=-1; if(w[r-1][c]==tag*(-1))s->row=r-1;s->col=c; elseif(w[r+1][c]==tag*(-1))s->row=r+1;s->col=c; elseif(w[r][c-1]==tag*(-1))s->row=r;s->col=c-1; elseif(w[r][c+1]==tag*(-1))s->row=r;s->col=c+1; elsep->len=0;returnp; push(S,s);visited[s->row][s->col]=1; flag=search(s,tag); while( (2)) {push(S,s);visited[s->row][s->col]=1; (3); } while(S->top>=0) {pop(S); (4); flag=search(s,tag); while(flag) {push(S,s); visit(s); flag=search(s); } } } voidpush(structstack*S,structposition*s) {S->top++; S->node[S->top].row=s->row; S->node[S->top].col=s->col; p->num++; p->data[p->num].row=s->row; p->data[p->num].col=s->col; } voidpop(structstack*S) {S->top--; } structposition*gettop(structstack*S) {structposition*s; s->row=S->data[S->top].row; s->row=S->data[S->top].row; returns; } intsearch(structposition*s,inttag) {introw,col; row=s->row;col=s->col; if(W[row+1][col]=(-1)*tag)&&(! visited[row+1][col]) {s->row=row+1;s->col=col;return1;} if(W[row-1][col]=(-1)*tag)&&(! visited[row-1][col]) {s->row=row-1;s->col=col;return1;} if(W[row][col+1]=(-1)*tag)&&(! visited[row][col+1]) {s->row=row;s->col=col+1;return1;} if(W[row][col-1]=(-1)*tag)&&(! visited[row][col-1]) {s->row=row;s->col=col-1;return1} (5); } 答案: (1)strlen(s)+strlen(t) (2)position+strlen(t)(3)target[i]=s[i-strlen(t)] (4)returna(5)returnf(a,b-a) (6)q=aveage(head->next) (7)p->ave=(head->data+q->ave*q->num)/p->num (1)j (2)data,d[i],j,n (3)num+i*d (1)q[rear]=T (2)front lchild(4)count++(5)flag (1)count (2)(x[i]>end[j])&&(x[i]-start[j]<=1)(3)start[j]=x[i](4)! flag(5) (1)visited[i][j]=0 (2)flag (3)flag=search(s,tag)(4)s=gettop(S) (5)return0
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序员 考试 试题