//题目1:编写一个字符串替换函数,函数名为StrReplace(char *strSrc, char *strFind, char *strReplace),
//strSrc为源字符串,strFind是待替换的字符串,strReplace为替换字符串。
//例如:把“zhuzhiwen”中的“hu”替换为"guwenbing"。
#include<iostream>
#include<map>
#include<vector>
using namespace std;
char* StrReplace(char* strSrc, char * strFind, char* strReplace)
{
int lengthSrc = strlen(strSrc);
int lengthFind = strlen(strFind);
int lengthReplace = strlen(strReplace);
char* strNew=new char[1000];
int p = 0;
for (int i = 0; i < lengthSrc; i++)
{
if (strSrc[i] == strFind[0])
{
for (int j = 1; j < lengthFind; j++)
{
if (strSrc[i + j] == strFind[j])
{
if (j == lengthFind - 1){
for (int k = 0; k < lengthReplace; k++)
{
strNew[p++] = strReplace[k];
}
i = i + lengthFind ;
}
}
else
{
break;
}
}
}
strNew[p++] = strSrc[i];
}
strNew[p] ='\0';
return strNew;
}
void main()
{
cout << StrReplace("zhuzhiwen", "hu","guwenbing");
}
//题目2:选秀节目打分,分为专家评委和大众评委,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>
#include<map>
#include<vector>
using namespace std;
int cal_score(int score[], int judge_type[], int n)
{
int daScore = 0, zhuanScore = 0,score1=0;
int daNum = 0, zhuanNum = 0;
for (int i = 0; i < n; i++)
{
if (judge_type[i]==1)
{
zhuanScore += score[i];
zhuanNum++;
}
else
{
daScore += score[i];
daNum++;
}
}
if (daNum == 0)
{
score1 = zhuanScore / n;
}
else{
score1 =0.6* zhuanScore / zhuanNum + 0.4*daScore/daNum;
}
return score1;
}
void main()
{
const int n = 10;
int score[n] = { 80, 85, 90, 80, 75, 95, 75, 80, 90, 95 };
int judge_type[n] = { 1, 2, 1, 1, 2, 2, 1, 1, 2, 1 };
cout << cal_score(score, judge_type, n);
//return 0;
}
//题目3:给定一个数组input[], 如果数组长度n为奇数,则将数组中最大的元素放到output[]数组最中间的位置,
//如果数组长度n为偶数,则将数组中最大的元素放到output[]数组中间两个位置偏右的那个位置上,
//然后按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
void outp(int input[], int output[], int n)
{
sort(input[0], input[n - 1]);
int inputP = n - 1;
int temp = n / 2;
int biaozhi = 0;
int zuoP = temp - 1;
int youP = temp + 1;
output[temp] = input[inputP--];
for (int i = inputP; i >= 0; i--)
{
if (biaozhi == 0)
{
output[zuoP--] = input[i];
biaozhi = 1;
}
else
{
output[youP++] = input[i];
biaozhi = 0;
}
}
}
void main()
{
const int n = 6;
int input[n] = { 0, 1, 2, 3, 4, 5 };
int output[n];
outp(input, output, n);
}
题目4:操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 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>
#include<vector>
#include <list>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
class M
{
public:
M(){
a=-1;
b=-1;
};
M(int in1,int in2){
a=in1;
b=in2;
};
int a,b;
};
bool com(M l,M r)
{
return l.a<r.a;
}
void scheduler (int task[],int n,int system_task[],int user_task[])
{
//sort(task[0],task[8]);
M* ms1=new M[n];
M* ms2=new M[n];
int p=0;
int q=0;
for (int i=0;i<n;i++)
{
if (task[i]<50)
{
ms1[p].a=task[i];
ms1[p++].b=i;
//system_task[p++]=i;
}
if (task[i]>=50&&task[i]<=255)
{
ms2[q].a=task[i];
ms2[q++].b=i;
}
}
sort(ms1,ms1+p,com);
sort(ms2,ms2+q,com);
for (int i=0;i<p;i++)
{
system_task[i]=ms1[i].b;
cout<< system_task[i];
}
for (int i=0;i<q;i++)
{
user_task[i]=ms2[i].b;
cout<< user_task[i];
}
}
void main()
{
int task[]={0,30,155,1,80,300,170,40,99};
int n=9;
int system_task[9];
int user_task[9];
scheduler( task, n, system_task, user_task);
}