您的位置:首页 > 编程语言 > PHP开发

2012届华为校园招聘上机考试题目程序实现(9月6日下午1点场)

2011-09-14 21:02 549 查看
这三道题相对来讲,因没有涉及到高级数据结构或者算法,所以实现起来并不难。

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)

int cal_score(int score[], int judge_type[], int n) {
double aver_exp = 0.0, aver_pub = 0.0;
int cnt_exp = 0, cnt_pub = 0;
while( (-- n) >= 0 ) {
if( judge_type
== 1 )
++cnt_exp, aver_exp += score
;
if( judge_type
== 2 )
++cnt_pub, aver_pub += score
;
}

// make sure that cnt_exp > 0
if( cnt_pub == 0 ) return (int)(aver_exp / cnt_exp + 1e-6);
return (int)(0.6 * aver_exp / cnt_exp + 0.4 * aver_pub / cnt_pub + 1e-6);
// eps = 1e-6, Control accuracy
}


     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[])

void sort(int input[], int n, int output[]) {
bool *vst = new bool
;
for(int i = 0; i < n; ++i) vst[i] = 0;

int Lpos, Rpos; bool dir;
Lpos = n / 2 - 1; Rpos = Lpos + 1; dir = 1;

int cmpIdx;
while( Lpos >= 0 || Rpos < n ) {
cmpIdx = -1;
for(int i = 0; i < n; ++i)
if( !vst[i] && (cmpIdx == -1 \
|| input[i] > input[cmpIdx]) )
cmpIdx = i;

if( cmpIdx == -1 ) break;

if( dir ) output[Rpos++] = input[cmpIdx];
else output[Lpos--] = input[cmpIdx];

dir ^= 1; vst[cmpIdx] = 1;
}

delete[] vst; vst = NULL;
}

      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[])

void scheduler(int task[], int n, int system_task[], int user_task[]) {
int cntSyst = 0, cntUser = 0;
for(int i = 0; i < n; ++i)
if( task[i] >= 0 && task[i] < 50 )
system_task[cntSyst++] = i;
else
if( task[i] >= 50 && task[i] <= 255 )
user_task[cntUser++] = i;

system_task[cntSyst] = -1; user_task[cntUser] = -1;

int tmp;
for(int i = 0, j; i < cntSyst; ++i)
for(j = i + 1; j < cntSyst; ++j)
if( task[ system_task[i] ] > task[ system_task[j] ] )
tmp = system_task[i], system_task[i] = system_task[j],  system_task[j] = tmp;

for(int i = 0, j; i < cntUser; ++i)
for(j = i + 1; j < cntUser; ++j)
if( task[ user_task[i] ] > task[ user_task[j] ] )
tmp = user_task[i], user_task[i] = user_task[j],  user_task[j] = tmp;
}


下述程序用于程序测试

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>

using namespace std;

#define dbg(x) cout << #x << " = " << x << endl

class ProA
{
public:

int cal_score(int score[], int judge_type[], int n) {
double aver_exp = 0.0, aver_pub = 0.0;
int cnt_exp = 0, cnt_pub = 0;
while( (-- n) >= 0 ) {
if( judge_type
== 1 )
++cnt_exp, aver_exp += score
;
if( judge_type
== 2 )
++cnt_pub, aver_pub += score
;
}

// make sure that cnt_exp > 0
if( cnt_pub == 0 ) return (int)(aver_exp / cnt_exp + 1e-6);
return (int)(0.6 * aver_exp / cnt_exp + 0.4 * aver_pub / cnt_pub + 1e-6);
// eps = 1e-6, Control accuracy
}

// For Judge
int score[50], judge_type[50], n;
inline void calc() {
cout << "ProA:" << endl;
cin >> n;
for(int i = 0; i < n; ++i) cin >> score[i];
for(int i = 0; i < n; ++i) cin >> judge_type[i];
cout << "Ans = " << cal_score(score, judge_type, n) << endl;
}
} A;

class ProB
{
public:

void sort(int input[], int n, int output[]) {
bool *vst = new bool
;
for(int i = 0; i < n; ++i) vst[i] = 0;

int Lpos, Rpos; bool dir;
Lpos = n / 2 - 1; Rpos = Lpos + 1; dir = 1;

int cmpIdx;
while( Lpos >= 0 || Rpos < n ) {
cmpIdx = -1;
for(int i = 0; i < n; ++i)
if( !vst[i] && (cmpIdx == -1 \
|| input[i] > input[cmpIdx]) )
cmpIdx = i;

if( cmpIdx == -1 ) break;

if( dir ) output[Rpos++] = input[cmpIdx];
else output[Lpos--] = input[cmpIdx];

dir ^= 1; vst[cmpIdx] = 1;
}

delete[] vst; vst = NULL;
}

// For Judge
int input[50], output[50], n;
inline void calc() {
cout << "ProB:" << endl;
cin >> n;
for(int i = 0; i < n; ++i) cin >> input[i];
sort(input, n, output);
for(int i = 0; i < n; ++i) cout << output[i] << " ";
cout << endl;
}
} B;

class ProC
{
public:

void scheduler(int task[], int n, int system_task[], int user_task[]) {
int cntSyst = 0, cntUser = 0;
for(int i = 0; i < n; ++i)
if( task[i] >= 0 && task[i] < 50 )
system_task[cntSyst++] = i;
else
if( task[i] >= 50 && task[i] <= 255 )
user_task[cntUser++] = i;

system_task[cntSyst] = -1; user_task[cntUser] = -1;

int tmp;
for(int i = 0, j; i < cntSyst; ++i)
for(j = i + 1; j < cntSyst; ++j)
if( task[ system_task[i] ] > task[ system_task[j] ] )
tmp = system_task[i], system_task[i] = system_task[j],  system_task[j] = tmp;

for(int i = 0, j; i < cntUser; ++i)
for(j = i + 1; j < cntUser; ++j)
if( task[ user_task[i] ] > task[ user_task[j] ] )
tmp = user_task[i], user_task[i] = user_task[j],  user_task[j] = tmp;
}

// For Judge
int task[50], system_task[50], user_task[50], n;
inline void calc() {
cout << "ProC:" << endl;
cin >> n;
for(int i = 0; i < n; ++i) cin >> task[i];
scheduler(task, n, system_task, user_task);
for(int i = 0; system_task[i] != -1; ++i) cout << system_task[i] << " ";
cout << "-1" << endl;
for(int i = 0; user_task[i] != -1; ++i) cout << user_task[i] << " ";
cout << "-1" << endl;
}
} C;

int main()
{
while(1) {
//A.calc();
//B.calc();
C.calc();
}
return 0;
}


试题参考网址: http://blog.csdn.net/liuqiqi677/article/details/6755498
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息