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<string.h>
-
intcal_score(intscore[],intjudge_type[],intn)
- {
- inti;
- intsum_exp=0,sum_nml=0;
- intcnt_exp=0,cnt_nml=0;
- intave_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)
- returnave_exp;
- ave_nml=sum_nml/cnt_nml;
- aver_score=ave_exp*0.6+ave_nml*0.4;
- returnaver_score;
- }
-
intmain()
- {
- intscore[5]={88,87,95,90,84};
- intjudge_type[5]={1,1,2,1,2};
- intav_score=cal_score(score,judge_type,5);
- printf("avscoreis%d\n",av_score);
- system("pause");
- return0;
- }
#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[]
- #include<stdio.h>
-
#include<string.h>
-
#include<math.h>
-
voidlarge2small(intnum[],intn)
- {
- inti,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;
- }
- }
- }
-
voidsort(intinput[],intn,intoutput[])
- {
- inti;
- intj;
- 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;
- }
- }
- elseif(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;
- }
- }
- }
-
intmain()
- {
- intinput[6]={3,6,1,9,7,8};
- intoutput[6]={0};
- inti;
- sort(input,6,output);
- for(i=0;i<6;i++)
- printf("%d",output[i]);
- system("pause");
- return0;
- }
#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[],长度为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<stdio.h>
-
#include<string.h>
-
voidscheduler(inttask[],intn,intsystem_task[],intuser_task[])
- {
- inti,j,k,tmp;
- intr=0,s=0;
- int*task_order=(int*)malloc(n*sizeof(int));
- for(i=0;i<n;i++)
- task_order[i]=task[i];
- for(j=0;j<n-1;j++)
- for(k=j+1;k<n;k++)
- if(task_order[j]>task_order[k])
- {
- tmp=task_order[j];
- task_order[j]=task_order[k];
- task_order[k]=tmp;
- }
- for(i=0;i<n;i++)
- {
- if(task_order[i]<50)
- {
- for(j=0;j<n;j++)
- if(task_order[i]==task[j])
- system_task[r++]=j;
- }
- if(task_order[i]>=50&&task_order[i]<255)
- {
- for(j=0;j<n;j++)
- if(task_order[i]==task[j])
- user_task[s++]=j;
- }
- }
- system_task[r]=-1;
- user_task[s]=-1;
- free(task_order);
- }
-
intmain()
- {
- inttask[]={0,30,155,1,80,300,170,40,99};
- intsystem_task[9]={0};
- intuser_task[9]={0};
- inti;
- scheduler(task,9,system_task,user_task);
- for(i=0;i<9;i++)
- printf("%d",system_task[i]);
- printf("\n");
- for(i=0;i<9;i++)
- printf("%d",user_task[i]);
- system("pause");
- return0;
- }
分享到:
相关推荐
比较全面的华为机试题 大家可以看看 对于参加华为机试C方面的同学很有用
2014年华为机试题,本人参加机试碰到的题目。很有代表性!
华为机试题源码合集
华为机试题,多多联系,找华为的实习应该是没问题的
华为射频天线笔试机试题(2021年),图片整理成word,极度清晰,绝对真实。
华为机试题 王子逃脱
华为机试题校园招聘的大全,经过大量人员整理得到的,非常不容易,题非常难得啊,转载的
有志于去华为的大牛们,可以看一下,华为机试题总结!!!
2022华为Java机试题.docx
2017最全华为机试题C/C++(含答案源码),包含111道上机考试题,欢迎下载,觉得资源好请好评。
个人整理的华为历年机试题目 包含一些以前的选择题 编程题 编程题目有些我全部做过 并且把个人的代码附上 有些代码是别人写的 可以对照参考
2014-2012各地机试题大集锦华为上机考试试题
华为2018机试题附自己的答案.zip
华为历年机试题大全,全部通过VS2008编译运行成功
2018年华为机试题,结束之后整理的机试试题及自己的答案,亲测结果没任何问题,需要的可以自己载下来看看
华为机试题2013第二次来东大的机试题,快来免费下载吧
华为机考试题+答案参照.pdf
据说这是2012年华为机试的一道题目,我给出两种方法,两种做法有微小的差别,主要涉及malloc的使用,对初学者应该有帮助
最新2015年华为提前批机试题,适合今年或明年想去华为的程序员!
内容包括第二,与第三题的解题思路与代码,第一题为经典模型就没放上去