华为机试题(2)

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)

  1. #include <stdio.h>   
  2. #include <string.h>   
  3.   
  4. int cal_score(int score[], int judge_type[], int n)  
  5. {  
  6.     int i;  
  7.     /*int   len_j = strlen(judge_type);*/  
  8.     int sum_exp= 0,sum_nml = 0;  
  9.     int cnt_exp= 0,cnt_nml = 0;  
  10.     int ave_exp,ave_nml,aver_score;  
  11.     for(i = 0;i<n;i++)  
  12.     {  
  13.         if(judge_type[i] == 1)  
  14.         {  
  15.             sum_exp+=score[i];  
  16.             cnt_exp++;  
  17.         }  
  18.         else  
  19.         {  
  20.             sum_nml+=score[i];  
  21.             cnt_nml++;  
  22.         }  
  23.     }  
  24.       
  25.     ave_exp = sum_exp/cnt_exp;  
  26.     if(cnt_nml == 0)  
  27.         return ave_exp;  
  28.     ave_nml = sum_nml/cnt_nml;  
  29.     aver_score = ave_exp*0.6+ave_nml*0.4;  
  30.     return aver_score;  
  31. }  
  32.   
  33. int main(/*int argc, char **argv*/)  
  34. {  
  35.     int score[5]={88,87,95,90,84};  
  36.     int judge_type[5]={1,1,2,1,2};  
  37.     int av_score = cal_score( score,  judge_type, 5);  
  38.     printf("av score is %d\n",av_score);  
  39.     system("pause");  
  40.     return 0;  
  41. }  
#include <stdio.h>
#include <string.h>

int cal_score(int score[], int judge_type[], int n)
{
	int i;
	/*int	len_j = strlen(judge_type);*/
	int sum_exp= 0,sum_nml = 0;
	int cnt_exp= 0,cnt_nml = 0;
	int ave_exp,ave_nml,aver_score;
	for(i = 0;i<n;i++)
	{
		if(judge_type[i] == 1)
		{
			sum_exp+=score[i];
			cnt_exp++;
		}
		else
		{
			sum_nml+=score[i];
			cnt_nml++;
		}
	}
	
	ave_exp = sum_exp/cnt_exp;
	if(cnt_nml == 0)
		return ave_exp;
	ave_nml = sum_nml/cnt_nml;
	aver_score = ave_exp*0.6+ave_nml*0.4;
	return aver_score;
}

int main(/*int argc, char **argv*/)
{
	int score[5]={88,87,95,90,84};
	int judge_type[5]={1,1,2,1,2};
	int av_score = cal_score( score,  judge_type, 5);
	printf("av score is %d\n",av_score);
	system("pause");
	return 0;
}


 

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

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <math.h>   
  4.   
  5. void large2small(int num[],int n)  
  6. {  
  7.     int i,j,tmp;  
  8.     for(i=0 ;i<n-1;i++)  
  9.     {  
  10.         for(j=i+1;j<n;j++)  
  11.             if(num[i]<num[j])  
  12.             {  
  13.                 tmp = num[i];  
  14.                 num[i] = num[j];  
  15.                 num[j] = tmp;  
  16.             }  
  17.     }  
  18. }  
  19.   
  20. void sort(int input[], int n, int output[])  
  21. {  
  22.     int i;  
  23.     int j;  
  24.     large2small(input, n);  
  25.     if(n%2 == 1)  
  26.     {  
  27.           
  28.         output[(n-1)/2]= input[0];  
  29.         j=1;  
  30.         for(i=(n-1)/2-1;i>=0;i--)  
  31.         {  
  32.             output[i] = input[j];  
  33.             j+=2;  
  34.         }  
  35.         j=2;  
  36.         for(i=(n-1)/2+1;i<n;i++)  
  37.         {  
  38.             output[i] = input[j];  
  39.             j+=2;  
  40.         }         
  41.     }  
  42.     else if(n%2 == 0)  
  43.     {  
  44.         output[(n)/2]= input[0];  
  45.         j=1;  
  46.         for(i=(n)/2-1;i>=0;i--)  
  47.         {  
  48.             output[i] = input[j];  
  49.             j+=2;  
  50.         }  
  51.         j=2;  
  52.         for(i=(n)/2+1;i<n;i++)  
  53.         {  
  54.             output[i] = input[j];  
  55.             j+=2;  
  56.         }         
  57.     }  
  58.   
  59. }  
  60.   
  61. int main(/*int argc, char **argv*/)  
  62. {  
  63.     int input[6]={3,6,1,9,7,8};  
  64.     int output[6]={0};  
  65.     int i;  
  66.     sort(input, 6, output);  
  67.     for(i=0;i<6;i++)  
  68.         printf("%d ",output[i]);  
  69.     system("pause");  
  70.     return 0;  
  71. }  
#include <stdio.h>
#include <string.h>
#include <math.h>

void large2small(int num[],int n)
{
	int i,j,tmp;
	for(i=0 ;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
			if(num[i]<num[j])
			{
				tmp = num[i];
				num[i] = num[j];
				num[j] = tmp;
			}
	}
}

void sort(int input[], int n, int output[])
{
	int i;
	int j;
	large2small(input, n);
	if(n%2 == 1)
	{
		
		output[(n-1)/2]= input[0];
		j=1;
		for(i=(n-1)/2-1;i>=0;i--)
		{
			output[i] = input[j];
			j+=2;
		}
		j=2;
		for(i=(n-1)/2+1;i<n;i++)
		{
			output[i] = input[j];
			j+=2;
		}		
	}
	else if(n%2 == 0)
	{
		output[(n)/2]= input[0];
		j=1;
		for(i=(n)/2-1;i>=0;i--)
		{
			output[i] = input[j];
			j+=2;
		}
		j=2;
		for(i=(n)/2+1;i<n;i++)
		{
			output[i] = input[j];
			j+=2;
		}		
	}

}

int main(/*int argc, char **argv*/)
{
	int input[6]={3,6,1,9,7,8};
	int output[6]={0};
	int i;
	sort(input, 6, output);
	for(i=0;i<6;i++)
		printf("%d ",output[i]);
	system("pause");
	return 0;
}

3、操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 50,用户任务的优先级 >= 50 <= 255。优先级大于255的为非法任务,应予以剔除。现有一任务队列task[],长度为ntask中的元素值表示任务的优先级,数值越小,优先级越高。函数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. #include <stdio.h>   
  2. #include <string.h>   
  3.   
  4. void scheduler(int task[], int n, int system_task[], int user_task[])  
  5. {  
  6.     int i,j,k,tmp;  
  7.     int r=0,s=0;  
  8.     int *task_order = (int *)malloc(n*sizeof(int));  
  9.     for(i=0;i<n;i++)  
  10.         task_order[i] = task[i];  
  11.     for(j=0;j<n-1;j++)  
  12.         for(k=j+1;k<n;k++)  
  13.             if(task_order[j]>task_order[k])  
  14.             {  
  15.                 tmp=task_order[j];  
  16.                 task_order[j]=task_order[k];  
  17.                 task_order[k]=tmp;  
  18.             }  
  19.     for(i=0;i<n;i++)  
  20.     {  
  21.         if(task_order[i]<50)  
  22.         {  
  23.             for(j=0;j<n;j++)  
  24.                 if(task_order[i] == task[j])  
  25.                     system_task[r++]=j;  
  26.         }  
  27.         if(task_order[i]>=50 && task_order[i]<255)  
  28.         {  
  29.             for(j=0;j<n;j++)  
  30.                 if(task_order[i] == task[j])  
  31.                     user_task[s++]=j;  
  32.         }  
  33.     }  
  34.     system_task[r]=-1;  
  35.     user_task[s]=-1;  
  36.     free(task_order);  
  37. }  
  38.   
  39. int main(/*int argc, char **argv*/)  
  40. {  
  41.     int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99};  
  42.     int system_task[9]={0};  
  43.     int user_task[9]={0};  
  44.     int i;  
  45.     scheduler(task,9,system_task,user_task);  
  46.     for(i=0;i<9;i++)  
  47.         printf("%d ",system_task[i]);  
  48.     printf("\n");  
  49.     for(i=0;i<9;i++)  
  50.         printf("%d ",user_task[i]);  
  51.     system("pause");  
  52.     return 0;  
  53. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值