最近参加深圳某互联网公司的笔试,有几道题。大家一起来看看。

xhz1234 2014-06-21 09:52:29
用C或C++写函数

1.写一个函数,找到二叉树中两个结点的最近共同祖先。

2.写一个函数,对一整形数组元素处理,输出不重复元素个数,处理完成后,数组变为不重复元素组成的数组。

输入int A[] = {1,2,3,4,3,2,1,6}和数组长度 输出: 返回值为5,A[] = {1,2,3,4,6}

...全文
548 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-06-23
  • 打赏
  • 举报
回复
二参考
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128      //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
   return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
   return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
    if (argc<3) {
        printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
        printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
        return 1;
    }
    if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
    f=fopen(argv[1],"r");
    if (NULL==f) {
        printf("Can not find file %s!\n",argv[1]);
        return 1;
    }
    buf=(char *)malloc(MAXLINES*MAXCHARS);
    if (NULL==buf) {
        fclose(f);
        printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
        return 2;
    }
    n=0;
    hh=0;
    i=0;
    while (1) {
        if (NULL==fgets(ln,MAXCHARS,f)) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
            while (1) {
                c=fgetc(f);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            strcpy(buf+i,ln);i+=MAXCHARS;
            n++;
            if (n>=MAXLINES) {
                MAXLINES2=MAXLINES*2;
                if (MAXLINES2==1280000) MAXLINES2=2500000;
                buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
                if (NULL==buf2) {
                    printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
                    printf("WARNING: Lines >%d ignored.\n",MAXLINES);
                    break;//
                }
                buf=buf2;
                MAXLINES=MAXLINES2;
            }
        }
    }
    fclose(f);
    if (n>1) {
        if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
        else qsort(buf,n,MAXCHARS,compare);
    }
    f=fopen(argv[2],"w");
    if (NULL==f) {
        free(buf);
        printf("Can not create file %s!\n",argv[2]);
        return 2;
    }
    fprintf(f,"%s\n",buf);
    if (n>1) {
        if (ignore_case) {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        } else {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        }
    }
    fclose(f);
    free(buf);
    return 0;
}
苒止 2014-06-23
  • 打赏
  • 举报
回复
不懂数据结构,给跪
xhz1234 2014-06-23
  • 打赏
  • 举报
回复
忘记说了,不能使用STL
我看你有戏 2014-06-22
  • 打赏
  • 举报
回复
第二题

法一
#include "map"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	int A[] = {1,2,3,4,3,2,1,6};
	int item = sizeof(A)/sizeof(int);
	map<int,int> mp;
	for (int i=0;i<item;i++)
	{
		mp[A[i]]=A[i];
	}

	map<int,int>::iterator itor;
	for (itor=mp.begin();itor!=mp.end();itor++)
	{
		printf("%d,",itor->first);
	}
	system("pause");
}


//法二
void printarr(int *a,int n)
{
	for(int i=0;i<n;i++)
	{
		printf("%d  ",a[i]);
	}
	printf("\n");
}


void bubble(int *a,int n)
{
	int i,j,temp;
	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(a[i]>a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
	printarr(a,n);
}
int _tmain_f1(int argc, _TCHAR* argv[])
{
	int A[] = {1,2,3,4,3,2,1,6};
	int item = sizeof(A)/sizeof(int);
	bubble(A,item);

	int* p = new int[item];
	int ic = 0;
	for (int i=0;i<item;i++)
	{
		if (i==0)
		{
			*p = A[0];
			ic++;
		}
		else
		{
			if (p[ic-1]!=A[i])
			{
				p[ic]=A[i];
				ic++;
			}
		}
		
	}

	printarr(p,ic);


	system("pause");
	return 0;
}
没事人 2014-06-22
  • 打赏
  • 举报
回复
感觉第二题不太难
zlm0913 2014-06-22
  • 打赏
  • 举报
回复
引用 5 楼 henry3695 的回复:
第二题

法一
#include "map"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	int A[] = {1,2,3,4,3,2,1,6};
	int item = sizeof(A)/sizeof(int);
	map<int,int> mp;
	for (int i=0;i<item;i++)
	{
		mp[A[i]]=A[i];
	}

	map<int,int>::iterator itor;
	for (itor=mp.begin();itor!=mp.end();itor++)
	{
		printf("%d,",itor->first);
	}
	system("pause");
}


//法二
void printarr(int *a,int n)
{
	for(int i=0;i<n;i++)
	{
		printf("%d  ",a[i]);
	}
	printf("\n");
}


void bubble(int *a,int n)
{
	int i,j,temp;
	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(a[i]>a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
	printarr(a,n);
}
int _tmain_f1(int argc, _TCHAR* argv[])
{
	int A[] = {1,2,3,4,3,2,1,6};
	int item = sizeof(A)/sizeof(int);
	bubble(A,item);

	int* p = new int[item];
	int ic = 0;
	for (int i=0;i<item;i++)
	{
		if (i==0)
		{
			*p = A[0];
			ic++;
		}
		else
		{
			if (p[ic-1]!=A[i])
			{
				p[ic]=A[i];
				ic++;
			}
		}
		
	}

	printarr(p,ic);


	system("pause");
	return 0;
}
为何不直接用STL中的set?
灌水号 2014-06-21
  • 打赏
  • 举报
回复
1
//查找p1,p2共同祖先
//parent为pNode,的父结点pNode为当前要处理的结点。若pNde为根时,parent 为NULL
//FindNodeByValue为根据元素来查找结点的位置,这个很简单,自己写。
PtrTreeNode FindAncestor( PtrTreeNode parent, PtrTreeNode pNode, Element p1, Element p2 )
{
    PtrTreeNode pTemp = NULL;
    PtrTreeNode pTempP1 = NULL, pTempP2 = NULL;
    if ( pNode == NULL  ) return false;
    pTempP1 = FindNodeByValue( pNode, p1 );
    pTempP2 = FindNodeByValue( pNode, p2 );

    if ( (pTempP1==pNode) || (pNode ==pTempP2) ) return parent; //如果当前结点与p1,或者p2一样那就返回父结点

    if ( pTempP1 && pTempP2 )
    {
        if (pNode->pLeftChild != NULL ) pTemp = FindAncestor( pNode, pNode->pLeftChild, p1, p2 );
        if ( pTemp != NULL ) return pTemp;
        return FindAncestor( pNode, pNode->pRightChild, p1, p2 );
    }
    else
    {
        return parent;
    }
}
2 目前能想到的就是用散列来做。
你怎么了熊吉 2014-06-21
  • 打赏
  • 举报
回复
第二个,直接排序行么
你怎么了熊吉 2014-06-21
  • 打赏
  • 举报
回复
第一个,假设二叉树是数组存放,就比较两个结点的下标,把较大者除以2,然后再次比较,重复直至两数相等,就是共同祖先的下标

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧