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)
/*
环境 vs2012,win8
*/
#include<stdio.h>
int cal_score(int score[], int judge_type[], int n)
{
if( score==NULL ||judge_type==NULL || n<=0)
return -1;
int i;
bool nopublic=true;//判断是否有大众评审
double sum=0;
for( i=0; i<n; i++)
{
//专家打分
if(judge_type[i]==1)
{
sum=sum+0.6*score[i];
}
//大众评委打分
else if( judge_type[i]==2)
{
sum=sum+0.4*score[i];
nopublic=false;
}
else
{
return -1;
}
}
//如果没有大众评委,则取专家评委的平均分
if(nopublic)
{
sum = sum*5.0/(3.0*n);
}
return (int)sum;
}
int main()
{
int score[4]={5,4,6,5};
int judge_type[4]={1,1,1,1};
printf("%d \n",cal_score(score, judge_type, 4));
getchar();
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[])
/*
环境 vs2012,win8
*/
#include<stdio.h>
#include<stdlib.h>
int comp( const void *a, const void *b)
{
return *(int *)a- *(int *)b;
}
void sort(int input[], int n, int output[])
{
if( input==NULL|| output==NULL || n<=0)
return ;
if(n==1)
output[0]=input[0];
int i;
int left;
int right;
int maxloc;
bool isleft=true;//最大数的左边先开始放数字
//由于在本题中快排并不是重点,直接调用了库函数
qsort( input, n, sizeof(int), comp);
if(n%2==1)
{ //数组长度是奇数
maxloc=n/2;
}
else
{ //数组长度是偶数
maxloc=(n+1)/2;
}
//初始化
left=maxloc-1;
right=maxloc+1;
output[maxloc]=input[n-1];
for(i=n-2; i>=0; i--)
{
//往左边添加
if(isleft)
{
output[left--]=input[i];
isleft=!isleft;
}
//往右边添加
else
{
output[right++]=input[i];
isleft=!isleft;
}
}
for(i=0; i<n; i++)
{
printf("%d ",output[i]);
}
printf("\n");
}
int main()
{
int input1[] = {3, 6, 1, 9, 7} ;
int output1[] = {0, 0, 0, 0 , 0};
int input2[] = {3, 6, 1, 9, 7, 8};
int output2[] = {0, 0, 0, 0, 0, 0};
sort( input1, 5, output1);
sort( input2, 6, output2);
getchar();
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[])
/*
环境 vs2012,win8
*/
#include<stdio.h>
#include<malloc.h>
void scheduler(int task[], int n, int system_task[], int user_task[])
{
if( task==NULL || system_task==NULL || user_task==NULL ||n<=0)
return ;
int i,j,index;
int temp;
int i_system=0;
int i_user=0;
//用来记录位置
int *p_loc;
p_loc=(int *)malloc(sizeof(int)*n);
if(p_loc==NULL)
return ;
for(i=0; i<n; i++)
p_loc[i]=i;
//对任务进行排序,同时也要将原来的位置记录下来
//选择排序
for(i=0; i<n-1; i++)
{
index=i;
for(j=i+1; j<n; j++)
{
if(task[j]<task[index])
index=j;
}
if(index!=i)
{
//任务优先数交换
temp=task[i];
task[i]=task[index];
task[index]=temp;
//位置交换
temp=p_loc[i];
p_loc[i]=p_loc[index];
p_loc[index]=temp;
}
}//for
//判断任务
for(i=0; i<n;i++)
{
if(task[i]<50)
{
system_task[i_system++]=p_loc[i];
}
else if( 50<=task[i] && task[i]<=255)
{
user_task[i_user++]=p_loc[i];
}
}
system_task[i_system]=-1;
user_task[i_user]=-1;
}
int main()
{
int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99};
int system_task[7];
int user_task[7];
scheduler( task , 9, system_task , user_task);
getchar();
return 0;
}