字符串替换&任务调度

来源:互联网 发布:电动站立床淘宝 编辑:程序博客网 时间:2024/04/28 02:09

题目1:编写一个字符串替换函数,函数名为StrReplace(char *strSrc,char *strFind,char *strReplace),strSrc为源字符串,strFind是待替换的字符串,strReplace为替换字符串。

例如:把“zhuzhiwen”中的“hu”替换为"guwenbing"。

#include <iostream>#include <string.h>using namespace std;char *StrReplace(char *strSrc,char *strFind,char *strReplace){    char *p=strSrc;    int lensrc=strlen(strSrc);    int lenfind=strlen(strFind);    int lenrep=strlen(strReplace);    int flag=0;    char *nstr=new char[lensrc+lenrep+1];    while(*p!='\0')    {        if(!strncmp(p,strFind,lenfind)) //在strSrc中找到相匹配的字符串        {            //char *nstr=new char[lensrc+lenrep+1];            strncpy(nstr,strSrc,flag);            strncpy(nstr+flag,strReplace,lenrep);            strncpy(nstr+flag+lenrep,strSrc+flag+1,lensrc-flag-1);            nstr[lensrc+lenrep-lenfind+1]='\0';            break;        }        else        {            p++;            flag++;        }    }    delete []strSrc;    strSrc=nstr;        //退出函数,则strSrc指向的仍为"zhuzhiwen",内存空间释放!!!!    return strSrc;}int main(){    char *scr="zhuzhiwen";    char *find="hu";    char *rep="guwenbing";    cout<<StrReplace(scr,find,rep);    return 0;}

总结:使用strncpy()函数中,不会将‘\0'一起拷贝到源字符串中,所以需要在最后加上'\0'


题目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>using namespace std;int cal_score(int score[],int judge_type[],int n){    float sc1=0,sc2=0;      //专家总分,大众总分    float asc1=0,asc2=0;    //平均分    int num1=0,num2=0;      //专家人数,大众人数    int sum=0;    for(int i=0;i<n;i++)    {        if(1==judge_type[i])        {            sc1+=score[i];            num1++;        }        else if(2==judge_type[i])        {            sc2+=score[i];            num2++;        }    }    asc1=sc1/num1;    if(num2)    {        asc2=sc2/num2;        sum=asc1*0.6+asc2*0.4;    }    else    {        sum=asc1;    }    return sum;}int 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>using namespace std;void func(int input[],int output[],int n){    int ma,temp,mid;    int arr[n];    for(int i=0;i<n;i++)    {        arr[i]=input[i];        cout<<arr[i]<<' ';    }    cout<<endl;    for(int i=0;i<n;i++)    {        ma=i;        for(int j=i+1;j<n;j++)        {            if(arr[j]>arr[ma])                ma=j;        }        temp=arr[ma];        arr[ma]=arr[i];        arr[i]=temp;        //cout<<arr[i]<<' ';    }    if(n%2) //n是奇数    {        mid=n/2;    }    else    //n是偶数    {        mid=n/2;    }    output[mid]=arr[0];    for(int i=mid-1,j=mid+1,k=0;k<n;i--,j++)    {        output[i]=arr[++k];        output[j]=arr[++k];    }}int main(){    const int n=6;    int input[n]={0,1,2,3,4,5};    int output[n];    func(input,output,n);    for(int i=0;i<n;i++)        cout<<output[i]<<' ';    return 0;}

题目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>using namespace std;void scheduler(int task[],int n,int system_task[],int user_task[]){    int coutsys=0,coutusr=0;    int msys,musr,temp;    for(int i=0;i<n;i++)    {        if(0<=task[i]&&task[i]<50)        {            system_task[coutsys++]=i;        }        else if(50<=task[i]&&task[i]<=255)        {            user_task[coutusr++]=i;        }        else            cout<<"出现非法任务"<<task[i]<<endl;    }    //coutsys-=1; //系统任务个数    //coutusr-=1; //用户任务个数    for(int i=0;i<coutsys;i++)    {        msys=i;        for(int j=i+1;j<coutsys;j++)        {            if(task[system_task[j]]<task[system_task[msys]])                msys=j;        }        temp=system_task[i];        system_task[i]=system_task[msys];        system_task[msys]=temp;    }    system_task[coutsys]=-1;    for(int i=0;i<coutusr;i++)    {        musr=i;        for(int j=i+1;j<coutusr;j++)        {            if(task[user_task[j]]<task[user_task[musr]])                musr=j;        }        temp=user_task[i];        user_task[i]=user_task[musr];        user_task[musr]=temp;    }    user_task[coutusr]=-1;}int main(){    const int n=9;    int task[n]={0,30,155,1,80,300,170,40,99};    int system_task[n];    int user_task[n];    scheduler(task,n,system_task,user_task);    int i=0,j=0;    while(system_task[i]!=-1)        cout<<system_task[i++]<<' ';    cout<<endl;    while(user_task[j]!=-1)        cout<<user_task[j++]<<' ';    cout<<endl;    return 0;}


【欢迎读者交流批评指正~】

0 0
原创粉丝点击