华为上机题汇总(三)

华为上机题汇总(三)

注:编译环境为Visual Studio 2012,答案仅供参考。

目录

第十一题

11.选秀节目打分,分为专家评委和大众评委,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 (n == 0)
    {
        // Invalid input
        return -1;
    }

    int professionSum = 0, massesSum = 0, professionNum = 0;

    for (int i = 0; i < n; i++)
    {
        if (judge_type[i] == 1)
        {
            professionSum += score[i];
            professionNum++;
        }
        else if (judge_type[i] == 2)
        {
            massesSum += score[i];
        }
        else
        {
            // Invalid input
            return -1;
        }
    }

    if (professionNum == 0)
    {
        return massesSum/n;
    }
    else if (professionNum == n)
    {
        return professionSum/n;
    }
    else
    {
        return 0.6*(professionSum/professionNum) + 0.4*(massesSum/(n-professionNum));
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    int score[] = {80,90,90};
    int type[] = {1,1,2};
    int n = 3;
    cout << cal_score(score,type,n) << endl;
    return 0;
}

第十二题

12.给定一个数组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}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void convertArray(int input[],int output[], int n){
    if (n == 0)
    {
        // error
        return;
    }

    vector<int> temp(input,input+n);
    sort(temp.begin(),temp.end(),[](const int &i1, const int &i2){
        return i1 > i2;
    });

    int startIndex = n/2;
    for (int i = 0; i < n; i++)
    {
        int index = startIndex + ((i+1)/2)*pow(-1,i);
        output[index] = temp[i];
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    int input[6] = {3, 6, 1, 9, 7, 8},output[6];
    convertArray(input,output,6);
    return 0;
}

第十三题

13.操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 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;

int *globalTask;
int cmp(const void *a, const void *b){
    return globalTask[*(int*)a] - globalTask[*(int*)b];
}

void scheduler(int task[], int n, int system_task[], int user_task[]){
    globalTask = task;
    int systemCount = 0, userCount = 0;
    for (int i = 0; i < n; i++)
    {
        if (task[i] < 50)
        {
            system_task[systemCount++] = i;
        }
        else if (task[i] <= 255)
        {
            user_task[userCount++] = i;
        }
    }
    qsort(system_task,systemCount,sizeof(int),cmp);
    qsort(user_task,userCount,sizeof(int),cmp);
    system_task[systemCount] = -1;
    user_task[userCount] = -1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99};
    int system_task[100],user_task[100];
    scheduler(task,9,system_task,user_task);
    return 0;
}

第十四题

14.字符串替换
描述:编写一个字符串替换函数,如strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。
举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,结果就变成了:
ABCDEFGHIJKLMNOPQgggUVWXYZ

#include <iostream>
#include <string>
using namespace std;

void replaceString(string &strSrc,const string &strFind, const string &strReplace){
    int length = strFind.size();
    bool flag = 1;
    while (flag)
    {
        int index = strSrc.find(strFind);
        if (index >= 0)
        {
            strSrc.replace(index,length,strReplace);
        }
        else
        {
            flag = 0;
            cout << strSrc << endl;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    string str,strFind,strRelace;
    getline(cin,str);
    getline(cin,strFind);
    getline(cin,strRelace);
    replaceString(str,strFind,strRelace);
    return 0;
}

第十五题

15.对一个数组,将数组中偶数从大到小排序,奇数从小到大排序,奇数和偶数交叉着放且输出数组第一位放奇数 若奇数和偶数不等长,则把剩下的直接放到数组中。
思路:先进行奇偶判断,得到奇数和偶数数组。然后对两数组排序,进行长度判断,最后组织数据。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void mySort(vector<int> &v){
    vector<int> oddV,evenV;
    for(auto begin = v.begin(),end = v.end();begin != end;begin++){
        if (*begin%2)
        {
            oddV.push_back(*begin);
        }
        else{
            evenV.push_back(*begin);
        }
    }
    sort(oddV.begin(),oddV.end());
    sort(evenV.begin(),evenV.end(),[](const int &a, const int &b){
        return a > b;
    });
    v.clear();
    auto oBegin = oddV.begin();
    auto eBegin = evenV.begin();
    while ((oBegin != oddV.end())&&(eBegin != evenV.end()))
    {
        v.push_back(*oBegin++);
        v.push_back(*eBegin++);
    }
    while (oBegin != oddV.end())
    {
        v.push_back(*oBegin++);
    }
    while (eBegin != evenV.end())
    {
        v.push_back(*eBegin++);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    // VS2012不支持列表初始化,在新版本编译器可以用vector<int> v ={1,2,3,4,5,6,7,8,9}
    vector<int> v;
    for (int i = 0; i < 9; i++)
    {
        v.push_back(i+1);
    }
    mySort(v);
    for (int i = 0; i < 9; i++)
    {
        cout << v[i] << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值