华为2017.7.26机试

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

#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;
}

  

时间: 2024-08-07 21:19:10

华为2017.7.26机试的相关文章

华为OJ机试训练(一)

题目1 -- 通过输入英文句子.将每一个单词反过来,标点符号顺序不变.非26个字母且非标点符号的情况就可以标识单词结束. 标点符号包含,.!? 比如输入:Hello, I need an apple. 输出: /** * 华为机试训练1: 通过输入英文句子,将每一个单词反过来.标点符号顺序不变.非26个字母且非标点符号的情况就可以标识单词结束. 标点符号包含,.!? * Hello, I need an apple. * * @author snail * */ public class Mai

华为机试(A)

二叉树遍历        答题时间: 00 小时 03 分 11 秒 描述:  二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树: 中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树: 后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根. 给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯一确定后序遍历). 题目类别:  树  难度:  中级  运行时间限制: 无限制 内存限

华为机试(B)

输入: 字符串只包含小写英文字母, 不考虑非法输入,输入的字符串长度小于等于20个字节. 输出: 删除字符串中出现次数最少的字符后的字符串. 样例输入: abcdd 样例输出: dd #include<iostream> #include<string> #include<iomanip> #include<vector> using namespace std; int main() { string input; getline(cin,input);

华为机试在线训练(4)

华为机试在线训练:字符串分隔 题目描述 ?连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组:?长度不是8整数倍的字符串请在后面补数字0,空字符串不处理. 输入描述: 连续输入字符串(输入2次,每个字符串长度小于100) 输出描述: 输出到长度为8的新字符串数组 输入例子: abc123456789 输出例子: abc000001234567890000000 代码: 1 import java.util.Arrays; 2 import java.util.Scanner; 3

华为 2015 机试 输出:数字后面的连续出现的(2个或多个)相同字符(数字或者字符),删去一个,非数字后面的不要删除,例如,对应输出为:33aabb55pin。

1 package 华为机试; 2 //C++ 输入:由数字和字母组成的字符串,例如:333aaabb55ppin 3 //输出:数字后面的连续出现的(2个或多个)相同字符(数字或者字符),删去一个,非数字后面的不要删除,例如,对应输出为:33aabb55pin. 4 5 //这句话的核心就是在字符串删除一些字符,感觉处理很复杂,删除哪些字符呢?我们观察发现, 本字符串中删除了一个3,一个a,一个p,满足的规则是啥呢? 333中删除最后一个3,3aa删除了一个a,5pp中删除一个p, 6 //规

[华为机试真题]69.姓名的夫妻相

题目 在中国,形容夫妻恩爱的词汇中,大家用的比较多的就是"夫妻相".所谓"夫妻相",就是两个人看上去比较般配,长相.身材等某些方面有一定的相似度. 本题则另辟蹊径,从人的姓名维度,以字母重复个数来寻找最具"夫妻相"的人. 题目中预先给定一组女士的姓名拼音.输入男士的姓名拼音(拼音中间可以有空格,字母全部小写),依预先给定姓名拼音的先后遍历所有姓名,输出字母重复数最多的女士姓名. 规则1:如果字母重复数最多的女士有多位相同,则以最先匹配的女士做为最

[华为机试真题]66.单词搜索

题目 代码 /*--------------------------------------- * 日期:2015-07-06 * 作者:SJF0115 * 题目:WordSearch * 来源:华为机试真题 -----------------------------------------*/ #include <iostream> #include <string> #include <vector> #include <stack> #include

华为机试正式版(西安c/c++/java),今天下午去机试的题目,新鲜出炉了!

以下题目都是回忆的,题目都很简单, 大家有些基础就可以参加!(语言可以是c/c++,也可以是java的) 题目一(60分): 字符串操作, 将小写转换成大写, 将大写转化为小写, 数字的不做转换 例如, 输入:aBcD12 输出:AbCd12 题目二(100分): 将输入的字符串按照规定重新排序,如果字符串长度为奇数, 则中间的字符保持不变, 中间字符左侧降序排列, 右侧字符按照升序排列, 如果字符串长度为偶数,则左半侧字符降序排列,右半侧字符则按照升序排列 例如, 输入:ab5de 输出:ba

华为机试ACM(字符组合问题)

今晚做了华为的机试,3道ACM题,最后一道是实现从M个不同字符中任取N个字符的所有组合. eg: input:ABC 2 output:AB AC BC 第一个输入为字符串,第二个输入为组合的字符个数,当N=0或者N>M时,输出“ERROR”. 思路:可以用递归的算法解决,例如ABC中2个字符的所有组合,先选取第一个的A,那包含A的2个字符的所有组合就是从后面剩余的BC中取1个字符的所有组合,然后选取第二个的B,那包含B的2个字符的所有组合就是从后面剩余的C中取1个字符的组合,即只有C,到选取第