备注:转载于 http://blog.csdn.net/ychtt/article/details/6774184
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 "iostream"
- using namespace std;
- int cal_score(int score[], int judge_type[], int n)
- {
- if (NULL==score||NULL==judge_type||0==n)
- return 0;
- int sum=0;
- int sum1=0,count1=0;
- int sum2=0,count2=0;
- for(int i=0;i<n;i++)
- {
- if (judge_type[i]==1)
- {
- sum1=sum1+score[i];
- count1++;
- }
- else
- {
- sum2=sum2+score[i];
- count2++;
- }
- }
- if(0==count2)
- sum=sum1/count1;
- else
- sum=(sum1/count1)*0.6+(sum2/count2)*0.4;
- return sum;
- }
- void main()
- {
- int score[3]={12,13,15};
- int judge_type[3]={1,1,2};
- printf("%d",cal_score(score, judge_type, 3) );
- }
//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 "iostream"
- using namespace std;
- void bubblesort(int data[],int n)
- {
- int temp=0;
- for(int i=0;i<n;i++ )
- {
- for (int j=i+1;j<n;j++)
- {
- if (data[i]<data[j])
- {
- temp=data[i];
- data[i]=data[j];
- data[j]=temp;
- }
- }
- }
- }
- void sort(int input[], int n, int output[])
- {
- int *sort_input=new int[n];
- for(int i=0;i<n;i++)
- {
- sort_input[i]=input[i];
- }
- bubblesort(sort_input,n);
- if (1==n%2)
- {
- int mid=n/2;
- int k=0;
- output[mid]=sort_input[k++];
- for(int j=1;j<=n/2;j++)
- {
- output[mid-j]=sort_input[k++];
- output[mid+j]=sort_input[k++];
- }
- }
- else
- {
- int mid=n/2;
- int k=0;
- output[mid]=sort_input[k++];
- for(int j=1;j<n/2;j++)
- {
- output[mid-j]=sort_input[k++];
- output[mid+j]=sort_input[k++];
- }
- output[0]=sort_input[k++];
- }
- delete sort_input;
- }
- void main()
- {
- int input1[] = {3, 6, 1, 9, 7};
- int output1[5];
- memset(output1,0,5*sizeof(int));
- int input2[] = {3, 6, 1, 9, 7, 8} ;
- int output2[6];
- memset(output2,0,6*sizeof(int));
- sort(input1, 5, output1);
- sort(input2, 6, output2);
- for(int k=0;k<5;k++)
- printf("%d ",output1[k]);
- for(k=0;k<6;k++)
- printf("%d ",output2[k]);
- }
// 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"
- using namespace std;
- void change(int *a,int *b)
- {
- int temp=*a;
- *a=*b;
- *b=temp;
- }
- void bubblesort(int data[],int n,int index[])//冒泡排序并记录排序后下标
- {
- int temp=0;
- for(int j=0;j<n;j++)
- index[j]=j;
- for(int i=0;i<n;i++ )
- {
- for (int j=i+1;j<n;j++)
- {
- if (data[i]>data[j])
- {
- change(&data[i],&data[j]);
- change(&index[i],&index[j]);
- }
- }
- }
- }
- void scheduler(int task[], int n, int system_task[], int user_task[])
- {
- int *sort_task=new int[n];
- int *index=new int[n];
- for(int i=0;i<n;i++)
- {
- sort_task[i]=task[i];
- }
- bubblesort(sort_task,n,index);
- i=0;
- while (sort_task[i]<50)
- {
- system_task[i]=index[i];
- i++;
- }
- system_task[i]=-1;
- int k=0;
- while (sort_task[i]>50&&sort_task[i]<=255)
- {
- user_task[k++]=index[i++];
- }
- user_task[k]=-1;
- delete sort_task;
- delete index;
- }
- void main()
- {
- int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99} ;
- int n=sizeof(task)/sizeof(int);
- int *system_task=new int[n];
- int *user_task=new int[n];
- scheduler(task, n, system_task, user_task);
- for(int k=0;k<n;k++)
- {
- printf("%d ",system_task[k]);
- if (system_task[k]!=-1)
- {
- printf("%d ",system_task[k]);
- }
- else
- {
- printf("%d ",system_task[k]);
- break;}
- }
- printf("\n");
- for(k=0;k<n;k++)
- {
- printf("%d ",user_task[k]);
- if (user_task[k]!=-1)
- {
- printf("%d ",user_task[k]);
- }
- else{
- printf("%d ",system_task[k]);
- break;}
- }
- }