题目来源:http://blog.csdn.net/liuqiqi677/article/details/6755498#cpp,感谢liuqiqi677
//第一题
#include <iostream>
using namespace std;
int cal_score(int score[], int judge_type[], int n) ;
int main()
{
int score[]={1,2,3,4,5,6};
int judge_type[]={1,1,1,2,2,2};
cout<<cal_score(score,judge_type,6)<<endl;
return 0;
}
int cal_score(int score[], int judge_type[], int n)
{
int sumexpert=0;
int expertcount=0;
int sumcustom=0;
int customcount=0;
for (int i=0;i<n;++i)
{
if (judge_type[i]==1)
{
sumexpert+=score[i];
++expertcount;
}
else if (judge_type[i]==2)
{
sumcustom+=score[i];
++customcount;
}
}
if (customcount!=0)
{
return (int)((sumexpert/expertcount)*0.6+(sumcustom/customcount)*0.4);
}
else
return sumexpert/expertcount;
}
//第二题 void sort(int input[[, int n, int output[])
#include <iostream>
using namespace std;
void sort(int input[], int n, int output[]);
int main()
{
int array1[]={3, 6, 1, 9, 7, 8};
int array2[6];
sort(array1,6,array2);
for (int i=0;i<6;++i)
{
cout<<array2[i]<<" ";
}
return 0;
}
void sort(int input[], int n, int output[])
{
int tmp;
for (int i=0;i<n-1;i++) //冒泡
{
for (int j=0;j<n-i-1;j++)
{
if (input[j]<input[j+1])
{
tmp=input[j];
input[j]=input[j+1];
input[j+1]=tmp;
}
}
}
//
int index=n/2;
int increase=1;
int inindex=1;
output[index]=input[0];
while (index+increase<=n-1)
{
output[index-increase]=input[inindex++];
output[index+increase]=input[inindex++];
++increase;
}
if (n%2==0)
{
output[0]=input[inindex];
}
}
//第三题以后贴出,
结构体+冒泡排序
//第三题
#include <iostream>
using namespace std;
void scheduler(int task[], int n, int system_task[], int user_task[]);
void printtask(int *task,int num);//打印函数
typedef struct taskNode
{
int num;//任务号
int priority;//任务的优先级
}taskNode;
int main()
{
int taskarray[] = {0, 30, 155, 1, 80, 300, 170, 40, 99};
int n=sizeof(taskarray)/sizeof(int);//队列长度
int *system_task=new int[n];
int *user_task=new int[n];
scheduler(taskarray,n,system_task,user_task);
printtask(system_task,n);
printtask(user_task,n);
delete []system_task;
delete []user_task;
system_task=NULL;
user_task=NULL;
return 0;
}
void scheduler(int task[], int n, int system_task[], int user_task[])
{
taskNode *mytask = new taskNode[n];//为任务队列分派空间
for (int i=0;i<n;++i)//为每一个节点赋值
{
mytask[i].num=i;
mytask[i].priority=task[i];
}
//冒泡对上面的结构体数组,优先级数 从小到大进行排序
taskNode tmp;
for (i=0;i<n-1;++i)
{
for (int j=0;j<n-i-1;++j)
{
if (mytask[j].priority > mytask[j+1].priority)
{
tmp=mytask[j+1];
mytask[j+1]=mytask[j];
mytask[j]=tmp;
}
}
}
//把系统任务和用户任务,分开
int sysindex=0;
int userindex=0;
for (i=0;i<n;++i)
{
if (mytask[i].priority >= 50 && mytask[i].priority <= 255)//用户任务
{
system_task[sysindex++]=mytask[i].num;
}
else if (mytask[i].priority < 50)//系统任务
{
user_task[userindex++]=mytask[i].num;
}
}
system_task[sysindex]=-1;//表示结束
user_task[userindex]=-1;
delete []mytask;
mytask=NULL;
}
void printtask(int *task,int num)
{
for (int i=0;i<num;++i)
{
cout<<task[i]<<" ";
if (task[i]==-1)
break;
}
cout<<endl;
}