2012届华为校园招聘机试题

/*
 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) 
 */
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<math.h>


int cal_score(int score[], int judge_type[], int n)
{
	int expert_totalscore=0;
	int expertnum=0;
	int public_totalscore=0;
	int publicnum=0;
	int i;
	int finalscore;
	for(i=0;i<n;i++)
	{
		if(judge_type[i]==1)
		{
			expertnum++;
			expert_totalscore+=score[i];
		}
		else if(judge_type[i]==2)
		{
			publicnum++;
			public_totalscore+=score[i];
		}
	}
	if(expertnum==n)
	{
		finalscore=(int)(expert_totalscore/n);
	}
	else
	{
		finalscore=(int)(expert_totalscore/expertnum *0.6+public_totalscore/publicnum *0.4);
	}
	return finalscore;
}
int main(void)
{
	int score[5]={10,20,30,40,50};
	int judge_type[5]={1,2,1,1,1};
	int finalscore;
	finalscore=cal_score(score,judge_type,5);
	printf("finalscore=%d\n",finalscore);
	return 0;
}


/*
给定一个数组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[])
 */
//即从“中间”那个位置两边按从大到小的顺序一左一右的填充数据
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<math.h>
#define MAX 20
void insertsort(int input[],int n)
{
	int p;
	int j;
	int temp;
	for(p=1;p<n;p++)
	{
		temp=input[p];
		for(j=p;j>0 && input[j-1]>temp;j--)
		{
			input[j]=input[j-1];
		}
		input[j]=temp;
	}
}
void sort(int input[], int n, int output[])
{
	int i;
	int mid=n/2;//当n为偶数时表示最中间的元素,为奇数时表示中间靠右的那个元素
	int d=1;

	insertsort(input,n);//插入排序
	output[mid]=input[n-1];//将数组input的最大值赋给output[mid]
	for(i=n-2;i>=0;i--)
	{
		output[mid-d]=input[i];
		if(i>0)
		{
			i--;
			output[mid+d]=input[i];
		}
		d++;
	}
}
int main(void)
{
	int *input=(int *)malloc(sizeof(int)*MAX);
	int *output=(int *)malloc(sizeof(int)*MAX);
	int i,n;
	if(input==NULL || output==NULL)
	{
		exit(0);
	}
    while(scanf("%d",&n)!=EOF)
	{
	   if(n<=0)
	   {
		   exit(0);
	   }
	   for(i=0;i<n;i++)
	   scanf("%d",&input[i]);
	   sort(input,n,output);
	   for(i=0;i<n;i++)
	   {
		printf("%d ",output[i]);
	   }
	   printf("\n");
	}
	return 0;
}
/*
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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<cmath>
#include<ctime>
using namespace std;

#define MAX 20

void Bubblesort(int task[],int input[],int n)
{
	int i,j,temp;
	for(i=0;i<n-1;i++)
		for(j=0;j<n-i-1;j++)
		{
			if(task[input[j]]>task[input[j+1]])
			{
				temp=input[j];
				input[j]=input[j+1];
				input[j+1]=temp;
			}
		}
}
//void Insertsort(int task[],int input[],int n)
//{
//	int p;
//	int j;
//	int temp;
//	for(p=1;p<n;p++)
//	{
//		temp=input[p];
//		for(j=p;j>0 && task[input[j-1]]>task[temp];j--)
//		{
//			input[j]=input[j-1];
//		}
//		input[j]=temp;
//	}
//}
void scheduler(int task[], int n, int system_task[], int user_task[])
{
	int i;
	int j=0;//数组system_task的下标索引
	int k=0;//数组user_task的下标索引
	for(i=0;i<n;i++)
	{
		if(task[i]>255)
			continue;
		if(task[i]>=0 && task[i]<50)
		{
			system_task[j++]=i;
		}
		else if(task[i]>=50 && task[i]<=255)
		{
			user_task[k++]=i;
		}
	}
	//Insertsort(task,system_task,j);
	//Insertsort(task,user_task,k);
	Bubblesort(task,system_task,j);
	Bubblesort(task,user_task,k);
	system_task[j]=-1;
	user_task[k]=-1;
}

int main(void)
{
	int *task       =(int *)malloc(sizeof(int )*MAX);
	int *system_task=(int *)malloc(sizeof(int )*MAX+1);//如果只有一种优先级的话,由于末尾要加元素-1,有可能就有n+1个元素
	int *user_task  =(int *)malloc(sizeof(int )*MAX+1);
	assert(task!=NULL && system_task!=NULL && user_task!=NULL);
	int n,i;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
			scanf("%d",&task[i]);

        scheduler(task,n,system_task,user_task);
		for(i=0;system_task[i]!=-1;i++)
			printf("%d ",system_task[i]);
		printf("\n");
		for(i=0;user_task[i]!=-1;i++)
			printf("%d ",user_task[i]);
		printf("\n");
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值