一、机试题目与要求
1、选秀节目打分,分为专家评委和大众评委,score[] 数组里面存储每个评委打的分数,judge_type[] 里存储与 score[] 数组对应的评委类别,judge_type == 1,表示专家评委,judge_type == 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[])
二、题目实现与结果
实现代码:
#include "vector"
#include "math.h"
#include "string"
#include "iostream"using namespace std;
int cal_score(int score[], int judge_type[], int n);
void sort(int input[], int n, int output[]);
void scheduler(int task[], int n, int system_task[], int user_task[]);
void main()
{
//The test case of exam1
cout<<"***************EXAM1****************"<<endl;
int score[] = {7,8,6,4,3,9,5};
int judge_type[] = {1,1,2,1,2,1,1};
int n = sizeof(score)/sizeof(int);
cout<<"The score array is: ";
for (int i = 0; i < n; i++)
{
cout<<score[i]<<" ";
}
cout<<endl;
cout<<"The judge type array is: ";
for (int i = 0; i < n; i++)
{
cout<<judge_type[i]<<" ";
}
cout<<endl;
cout<<"The score result is: "<<cal_score(score, judge_type, n)<<endl;
//The test case of exam2
cout<<"***************EXAM2****************"<<endl;
int input[] = {3,6,1,9,7,8};
int output[] = {0,0,0,0,0,0};
n = sizeof(input)/sizeof(int);
cout<<"The input array is:";
for (int i = 0; i < n; i++)
{
cout<<input[i]<<" ";
}
cout<<endl;
sort(input,n,output);
cout<<"The output array is: ";
for (int i = 0; i < n; i++)
{
cout<<output[i]<<" ";
}
cout<<endl;
//The test case of exam3
cout<<"***************EXAM3****************"<<endl;
int task[] = {0,30,155,1,80,300,100,40,255};
int system_task[] = {0,0,0,0,0,0,0,0,0};
int user_task[] = {0,0,0,0,0,0,0,0,0};
n = sizeof(task)/sizeof(int);
cout<<"The task array is: ";
for (int i = 0; i < n; i++)
{
cout<<task[i]<<" ";
}
cout<<endl;
scheduler(task, n, system_task, user_task);
int i = 0, j = 0;
cout<<"The system task array is: ";
do
{
cout<<system_task[i]<<" ";
} while (system_task[i++]!=-1);
cout<<endl;
cout<<"The user task array is: ";
do
{
cout<<user_task[j]<<" ";
} while (user_task[j++]!=-1);
cout<<endl;
}
int cal_score(int score[], int judge_type[], int n)
{
int score_final = 0;
if (n<=0)
{
return -1;
}
else
{
int sum_ex=0, sum_no=0, num_ex=0, num_no=0;
for (int i = 0; i < n; i++)
{
if (judge_type[i]==1)
{
sum_ex+=score[i];
num_ex++;
}
else if (judge_type[i]==2)
{
sum_no+=score[i];
num_no++;
}
else
return -1;
}
if (num_no==0)
{
score_final = sum_ex/num_ex;
}
else
{
score_final = 0.6*(sum_ex/num_ex) + 0.4*(sum_no/num_no);
}
}
return score_final;
}void sort(int input[], int n, int output[])
{
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (input[i]>input[j])
{
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}
int m = n/2;
int k = n - 1;
output[m] = input[k--];
for (int i = 1; i <= m; i++)
{
output[m-i] = input[k--];//此处k--的使用
output[m+i] = input[k--];
}
if (n%2==0)
{
output[0] = input[0];
}
}void scheduler(int task[], int n, int system_task[], int user_task[])
{
int m = 0,k = 0;
for (int i = 0; i < n; i++)
{
if (task[i]<50)
{
system_task[m] = i;
for (int j = m; j > 0; j--)
{
if (task[system_task[j-1]]>task[system_task[j]])
{
int temp = system_task[j];
system_task[j] = system_task[j-1];
system_task[j-1] = temp;
}
}
m++;
}
else if (task[i]>=50 && task[i]<=255)
{
user_task[k] = i;
for (int j = k; j > 0; j--)
{
if (task[user_task[j-1]]>task[user_task[j]])//此处相邻元素用j和j-1表示
{
int temp = user_task[j];
user_task[j] = user_task[j-1];
user_task[j-1] = temp;
}
}
k++;
}
}
system_task[m] = -1;
user_task[k] = -1;
}
运行结果:
三、总结
题目并不很难,基本上都是考察的基本知识点和基本功,在这些题目的实现过程中还是学到了不少,比如代码中注释部分,同时也充分暴露了自己不少方面的欠缺,所以,还得继续苦练内功啊。。。
评论