华为2017.7.26机试

时间:2022-11-05 15:36:04

做了一下华为机试,代码可能还有些问题,先传上去,第三题会有些问题。第一题

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

int main()
{
    int p1, p2, p3;
    cin >> p1>>p2>>p3;
    string str;
    cin >> str;
    vector<char>res;
    for (int i = 0; i < str.size(); i++)
    {
        
        if (str[i] != '-')
        {
            res.push_back(str[i]);
            
        }
        else
        {
            
            if (0<i<str.size()-1&&abs(str[i + 1] - str[i - 1]) > 25)
            {
                res.push_back(str[i]);
            }
            if (0 < i < str.size() - 1 && abs(str[i + 1] - str[i - 1] <= 0))
            {
                res.push_back(str[i]);
            }
            int flag = 0;
            for (int j = str[i - 1] + 1; j < str[i + 1]; j++)
            {
                
                char addNums;
                switch (p1)
                {
                case(1) :
                    addNums = j;
                    break;
                case(2) :
                    addNums = toupper(j);
                    break;
                case(3) :
                    addNums = '*';
                    break;

                }
                for (int k = 0; k < p2; k++)
                {
                    res.push_back(addNums);
                    flag++;
                }
                
            }
            if (p3 == 2)
            {
                int m;
                m = res.size() - flag;
                reverse(res.begin() + m , res.begin() + m+flag);
                
            }
                


        }
        
            

    }
    for (int i = 0; i < res.size(); i++)
    {
            cout << res[i];
    }
    cout << endl;
    system("pause");
    return 0;
        
    
}

第二题

#include<iostream>
#include<string>
#include<stack>
#include<sstream>
#include<vector>
#include<stdlib.h>
using namespace std;
int helper(string &res)
{
    reverse(res.begin(), res.end());
    res = res.substr(1, res.size() - 2);
    istringstream  input(res);
    string result;
    vector<string> temp;
    while (input >> result)
    {
        
        temp.push_back(result);
    }
    
    if (temp[0] == "add")
        return stoi(temp[1]) + stoi(temp[2]);
    if (temp[0] == "sub")
        return stoi(temp[1]) - stoi(temp[2]);
    if (temp[0] == "mul")
        return stoi(temp[1]) * stoi(temp[2]);
    if (temp[0] == "div")
    {
        if (temp[2] != "0")
            return stoi(temp[1]) / stoi(temp[2]);
        else
            return false;
    }
        
}
int operation(string &nums)
{
    stack<char> st;
    int tt=0;
    int len = nums.size();
    for (int i = 0; i < len; i++)
    {
        st.push(nums[i]);
        if (st.top() == ')')
        {
            
            //break;
            string temp = "";
            while (st.top() != '(')
            {
                
                temp += st.top();
                st.pop();
                
            }
            temp += st.top();
            st.pop();
            tt = helper(temp);
            string ttstr = to_string(tt);
            for (int i = 0; i < ttstr.size(); i++)
            {
                st.push(ttstr[i]);
            }
            
            
            
            
                
        
        }


    }
    return tt;
}
int main()
{
    //string input = "(sub (mul (mul 2 4) 4) (div 9 3))";
    string input;
    getline(cin,input);
    if (input.size() == 0) throw "Error";
    int output;
    output = operation(input);
    cout << output << endl;
    system("pause");
    return 0;
}

第三题

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
struct PeopleIntroction
{
	string name;
	string phone;
};
vector<string> peopleTemp(string &p)
{
	istringstream input1(p);
	vector<string> temp;
	string result1;
	while (input1 >> result1)
	{
		temp.push_back(result1);
	}
	return temp;
}
int main()
{
	int nums;
	cin >> nums;
	const int n = 200;
	cin.ignore();
	PeopleIntroction people[n];
	for (int i = 0; i < nums; i++)
	{
		getline(cin, people[i].name);
		getline(cin, people[i].phone);
	}
	string want_to_peo;
	getline(cin,want_to_peo);
	istringstream input(want_to_peo);
	vector<string> res;
	string result;
	while (input >> result)
	{
		res.push_back(result);
	}
	if (res.size() == 2)
	{


		for (int i = 0; i < nums; i++)
		{
			vector<string> temp;
			temp = peopleTemp(people[i].name);

			if (res[0] == temp[0] && res[1] == temp[1])
			{

				cout << people[i].name << "\t" << people[i].phone;

			}
			temp.clear();

			
		//按照首字母组合
		for (int i = 0; i < nums; i++)
		{
			vector<string> temp;
			temp = peopleTemp(people[i].name);

			if (res[0][0] == temp[0][0] && res[1][0] == temp[1][0])
			{

				cout << people[i].name << "\t" << people[i].phone;

			}
			cout << endl;
			temp.clear();
		}
	}
	if (res.size() == 1)
	{
		for (int i = 0; i < nums; i++)
		{
			vector<string> temp;
			temp = peopleTemp(people[i].name);

			if (res[0] == temp[0] || res[0] == temp[1])
			{

				cout << people[i].name << "\t" << people[i].phone;

			}

			cout << endl;
			temp.clear();
		}
	}
	cout << endl;
	system("pause");
	return 0;
}