您的位置:首页 > 其它

2012届华为校园招聘上机考试题目——自己也尝试了下

2011-09-09 15:09 453 查看
原文地址:http://blog.csdn.net/liuqiqi677/article/details/6755498#

昨天去参加了华为的校园招聘上机考试,题目一共三道,都比较简单,不要求算法效率,也不要求对所给数据的合法性作检测,主要还是注重基础知识的考察,和大家分享一下,希望对接下来的同学有所帮助。

1、选秀节目打分,分为专家评委和大众评委,score[] 数组里面存储每个评委打的分数,judge_type[] 里存储与 score[] 数组对应的评委类别,judge_type[i] == 1,表示专家评委,judge_type[i] == 2,表示大众评委,n表示评委总数。打分规则如下:专家评委和大众评委的分数先分别取一个平均分(平均分取整),然后,总分 = 专家评委平均分 * 0.6 + 大众评委 * 0.4,总分取整。如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数最终返回选手得分。

函数接口 int cal_score(int score[], int judge_type[], int n)

2、给定一个数组input[] ,如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。

例如:input[] = {3, 6, 1, 9, 7} output[] = {3, 7, 9, 6, 1}; input[] = {3, 6, 1, 9, 7, 8} output[] = {1, 6, 8, 9, 7, 3}

函数接口 void sort(int input[[, int n, int output[])

3、操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 50,用户任务的优先级 >= 50且 <= 255。优先级大于255的为非法任务,应予以剔除。现有一任务队列task[],长度为n,task中的元素值表示任务的优先级,数值越小,优先级越高。函数scheduler实现如下功能,将task[] 中的任务按照系统任务、用户任务依次存放到 system_task[] 数组和 user_task[] 数组中(数组中元素的值是任务在task[] 数组中的下标),并且优先级高的任务排在前面,优先级相同的任务按照入队顺序排列(即先入队的任务排在前面),数组元素为-1表示结束。

例如:task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99} system_task[] = {0, 3, 1, 7, -1} user_task[] = {4, 8, 2, 6, -1}

函数接口 void scheduler(int task[], int n, int system_task[], int user_task[])

以下是自己的实现:

<1>

int cal_score(int score[], int judge_type[], int n)
{
float pro = 0; //保存专家的分数
float custom = 0; //保存观众的分数
int i = 0; //循环
int count_pro = 0; //计数-专家
int count_custom = 0; //计数-观众

for(i=0;i<n;i++)
{
if(judge_type[i] == 1)
{
pro += score[i];
count_pro++;
}

if(judge_type[i] == 2)
{
custom += score[i];
count_custom++;
}
}

pro = pro/count_pro;
custom = custom/count_custom;

if( n != count_pro)
{
return 0.6*pro+0.4*custom;
}
else
{
return pro;
}
}


<2>第二题

#include <stdio.h>

//冒泡排序,实现简单
void pop(int input[], int n)
{
int i=0;
int j=0;

for(int i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(input[j] < input[j+1])
{
int temp;
temp = input[j];
input[j] = input[j+1];
input[j+1] = temp;
}
}
}
}

void sort(int input[], int n, int output[])
{
int i=0;
pop(input,n);
//先从第二位开始,考虑对称性
for(i=1;i<=(n/2);i++)
{
output[n/2-i]=input[i*2-1];
output[n/2+i]=input[i*2];
}
output[n/2] = input[0];
}

int main()
{
int array[6] = {3,6,1,9,7,8};
int output[6] = {0};
sort(array,6,output);
//pop(array,5);

for(int i=0;i<6;i++)
{
printf("%d\t",output[i]);
}
printf("\n");
}


<3>第三题

#include <stdio.h>
//冒泡排序
void pop(int input[], int n)
{
int i=0;
int j=0;

for(int i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(input[j] > input[j+1])
{
int temp;
temp = input[j];
input[j] = input[j+1];
input[j+1] = temp;
}
}
}
}

//查数的位置
int find(int num, int input[],int n)
{
int i=0;
int location=-1;
for(i=0;i<n;i++)
{
if(input[i] == num)
{
location = i;
break;
}
}
return location;
}
void scheduler(int task[], int n, int system_task[], int user_task[])
{
int i=0;
int system = 0;
int user = 0;
for(i=0;i<n;i++)
{
if(task[i]<50)
{
system_task[system] = task[i];
system++;
}
if(task[i]>=50 && task[i]<255)
{
user_task[user] = task[i];
user++;
}
}

pop(system_task,system);
pop(user_task,user);

for(i=0;i<system;i++)
{
int location = find(system_task[i],task,n);
if(location !=-1)
{
system_task[i] = location;
}
}

for(i=0;i<user;i++)
{
int location = find(user_task[i],task,n);
if(location !=-1)
{
user_task[i] = location;
}
}
}

int main()
{
int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99} ;
int system_task[9] ={-1};
int user_task[9] = {-1};
scheduler(task,sizeof(task)/sizeof(int),system_task,user_task);

return 0;
}


这三道题目做起来并不算难,在不考虑算法效率的情况下。从原文作者所说的两个小时来看,只有充分理解各种算法,才能在这么短的时间内写出最有效率的方法。个人想法是先写出来,再谈优化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: