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)
<span style="font-size:18px;">#include <iostream>
//#include <>
using namespace std;
int cal_score(int score[], int judge_type[], int n)
{
int zhuan_score = 0;
int zhuan_num = 0;
int da_score = 0;
int da_num = 0;
for(int i=0;i<n;i++)//先求专家组合大众组各自的平均分
{if (judge_type[i]==1)//专家组
{zhuan_score = zhuan_score + score[i];
zhuan_num++;
}
else
{
if (judge_type[i]==2)
{
da_score = da_score + score[i];
da_num++;
}
else
{cout<<"error!"<<endl;
break;
}
}
}
if (zhuan_score!=0)
{
zhuan_score = zhuan_score / zhuan_num;
if(da_num==0)//大众组没有人
{
return zhuan_score;
}
else
{
da_score = da_score / da_num;
return zhuan_score*0.6 + da_score *0.4;
}
}
else
{
cout<<"有误,专家组没有人!";
}
}
void main()
{
int score[3]={12,13,15};
int judge_type[3]={1,1,2};
printf("%d",cal_score(score, judge_type, 3) );
} </span>
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 new_sort(int input[[, int n, int output[])
#include <iostream>
//#include <>
using namespace std;
void new_sort(int input[], int n, int output[])
{
//先对输入的数组进行排序
//冒泡法排序
int t = 0;
for(int i = 0; i < n ; i++)
{
for(int j = 0; j < n-i; j++)
{
if (input[j]<input[j+1])
{
t = input[j];
input[j] = input[j+1];
input[j+1] = t;
}
}
}
//对于奇数长度
int m = 0;
int m0 = n%2;
//if(m0!=0)
//{
// m = n/2;//奇数的情况
//}
//else
//{
// m = n/2;//偶数的情况
//}
m = n/2;//奇数和偶数的情况m值都是一样的
output[m] = input[0];
int kp =1;
for (int k = 1; k<n/2; k++)
{
output[m-k] = input[kp];
kp++;
output[m+k] = input[kp];
kp++;
}
if (n%2)//奇数
{
//kp =
output[0] = input[kp];
kp++;
output[n-1] = input[kp];
}
else
{
output[0] = input[kp];
}
}
void main()
{
int input1[] = {3, 6, 1, 9, 7};
int output1[5];
new_sort(input1, 5, output1);
for(int k=0;k<5;k++)
printf("%d ",output1[k]);
cout<<"\n"<<"对于长度为偶数的情况"<<endl;
int input2[] = {3, 6, 1, 9, 7,8};
int output2[6];
new_sort(input2, 6, output2);
for(int k1=0;k1<6;k1++)
printf("%d ",output2[k1]);
}