华为2019软件类笔试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jirryzhang/article/details/81560255

第一题

忘记题目了...复盘不了

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


int main()
{
    
    cout<<"Hello Alipay!"<<endl;

    return 0;
}

第二题:0-1背包问题

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

void split(std::string str,std::string pattern,vector<int>& result)
{
    std::string::size_type pos;

    str+=pattern;
    int size=str.size();

    for(int i=0; i<size; i++){
        pos=str.find(pattern,i);
        if(pos<size){
            std::string s=str.substr(i,pos-i);
            int tt=std::stoi(s);
            result.push_back(tt);
            i=pos+pattern.size()-1;
        }
    }
}

int solve(int N,int V,vector<int>& value,vector<int>& weight)
{
    vector<vector<int>> f(N+1,vector<int>(V+1,0));
    int result=0;
    for(auto i = 1; i <= N; i++)
    {
        for(auto j = 1; j <= V; j++)
        {
            if(j < weight[i])
            {
                f[i][j] = f[i - 1][j];
            }
            else
            {
                int x = f[i - 1][j];
                int y = f[i - 1][j - weight[i]] + value[i];
                f[i][j] = x < y ? y : x;
            }
            if(f[i][j]>result)
                result=f[i][j];
        }
    }

    return result;
}

int main()
{
    string vstr,wstr;
    int c;
    while(cin>>vstr>>wstr) {
        vector<int> v(1, 0);
        vector<int> w(1, 0);
        split(vstr,",",v);
        split(wstr,",",w);
        cin >> c;
        cout << solve(5, c, v, w) << " ";
    }
}

第三题:typedef语句解析

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

string trim(const string& str)
{
    string::size_type pos = str.find_first_not_of(' ');
    if (pos == string::npos)
    {
        return str;
    }
    string::size_type pos2 = str.find_last_not_of(' ');
    if (pos2 != string::npos)
    {
        return str.substr(pos, pos2 - pos + 1);
    }
    return str.substr(pos);
}

std::vector<std::string> split(std::string str,std::string pattern)
{
    std::string::size_type pos;
    std::vector<std::string> result;

    str+=pattern;
    int size=str.size();

    for(int i=0; i<size; i++)
    {
        pos=str.find(pattern,i);
        if(pos<size)
        {
            std::string s=trim(str.substr(i,pos-i));
            result.push_back(s);
            i=pos+pattern.size()-1;
        }
    }
    return result;
}



int main()
{
    string word,key;
    char ch;
    vector<string> text;
    map<string, string> mm;
    while (cin >> word) {
        text.push_back(word);
        if ((ch = getchar()) == '\n')
            break;
    }
    cin>>key;
    for(int i=0;i<text.size();i+=3)
    {
        if(text[i]=="typedef")
        {
            int len=text[i+2].size();
            if(text[i+2][len-1]=';'){
                text[i+2]=text[i+2].substr(0,len-1);
                mm[text[i+2]] = text[i+1];
                for(auto j=mm.begin();j!=mm.end();++j){
                    auto p=mm[text[i+2]].find(j->first);
                    if(p!=string::npos)
                        mm[text[i+2]].replace(p,1,j->second);
                }
            }
            else{
                cout<<"none ";
                return 0;
            }
            int test=0;
        }
        else {
            cout << "none ";
            return 0;
        }
    }


    cout<<mm[key]<<" ";

    return 0;
}

猜你喜欢

转载自blog.csdn.net/jirryzhang/article/details/81560255
今日推荐