10月13日百度笔试整理

一、简答题:

简述OSI七层模型

Linux中进程间通信的方式(至少三种)

TCP、UDP的区别,分别举出一个上层协议

二、程序设计题:

1、整型数组int[] arrays={a_0,a_1,a_2,...a_n};输出其中数字的所有组合

#include<stdio.h>
#include<stdlib.h>
void visit(int arrays[], int length)
{
	int i;
	printf("\n");
	for(i=0;i<length;i++)
	{
		printf("%d\t",arrays[i]);
	}
	printf("\n");
}
void swap(int arrays[], int i, int j)
{
	arrays[i]=arrays[i]^arrays[j];
	arrays[j]=arrays[i]^arrays[j];
	arrays[i]=arrays[i]^arrays[j];
}
void visit(int arrays[], int start, int length)
{
	if(start==length)
	{
		visit(arrays, length);
		return ;
	}
	visit(arrays, start+1, length);
	for(int i=start+1; i<length;i++)
	{
		swap(arrays, i, start);
		visit(arrays, start+1, length);
		swap(arrays, i, start);
	}

}
void rank(int arrays[], int length) 
{
	visit(arrays, 0, length);
}
int main()
{
	int arrays[]={1,2,3};
	rank(arrays, 3);
	return 0;
}


2、有这样一个数组A,大小为n,相邻元素差的绝对值都是1。如:A={4,5,6,5,6,7,8,9,10,9}。 现在,给定A和目标整数t,请找到t在A中的位置。

#include<stdio.h>
#include<stdlib.h>
void find(int arrays[], int num, int length)
{
	int i=0,find=0;
	while(i<length)
	{
		int minus=arrays[i]-num;
		if(minus==0)
		{
			find=1;
			printf("location:\t %d\n",i);
			i++;
		}
		else
		{
			if(minus<0)
				minus*=-1;
			i+=minus;
		}
	}
	if(!find)
	{
		printf("\nNo data!\n");
	}
}



int main()
{
	int arrays[]={4,5,6,5,6,7,8,9,10,9};
	printf("input number:\n");
	int num;
	scanf("%d",&num);
	find(arrays, num, 10);
	return 0;
}


3、给定一棵二叉树,二叉树的高度定义为二叉树的层数,二叉树的宽度定义为二叉树各层中节点个数的最大值,求二叉树的面积。

#include<stdlib.h>
#include<stdio.h>
typedef struct BNode
{
	BNode* lchild;
	BNode* rchild;
	int value;
}*BTree;
BNode* initNode(int value)
{
	BNode* node=(BNode*)malloc(sizeof(BNode));
	node->lchild=NULL;
	node->rchild=NULL;
	node->value=value;

	return node;
}
BNode* initNode()
{
	return initNode(0);
}
BTree createBTree()
{
	BTree root=initNode();
	root->lchild=initNode(1);
	root->rchild=initNode(2);
	root->lchild->lchild=initNode(3);
	root->lchild->rchild=initNode(4);
	root->rchild->lchild=initNode(5);
	root->rchild->rchild=initNode(6);

	return root;
}
int height(BTree root)
{
	if(root->lchild!=NULL&&root->rchild!=NULL)
	{
		int h1=height(root->lchild)+1;
		int h2=height(root->rchild)+1;
		return h1>h2?h1:h2;
	}
	else if(root->lchild!=NULL)
		return height(root->lchild)+1;
	else if(root->rchild!=NULL)
		return height(root->rchild)+1;
	else
		return 1;
}
void width(BTree root, int n[], int i, int* max)
{
	
	if(root)
	{
		i++;
		if(root->lchild) n[i]++;
		if(root->rchild) n[i]++;
		if(*max<n[i]) *max=n[i];
		width(root->lchild, n, i, max);
		width(root->rchild, n, i, max);
	}
}
int width(BTree root)
{
	int n[20]={0};
	int i=0;
	int max=0;
	if(root)
	{
		n[i]++;
		max=n[i];
		i++;
		if(root->lchild)	n[i]++;
		if(root->rchild)	n[i]++;
		if(max<n[i]) max=n[i];
		
		width(root->lchild, n, i, &max);
		width(root->rchild, n, i, &max);

	}
	return max;
}
int main()
{
	BTree root=createBTree();
	int h=height(root);
	int w=width(root);
	int squre=w*h;
	printf("squre is :%d\nwidth=%d\theight=%d\n",squre, w, h);
	return 0;
}


三、系统设计题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值