C++:笔试题汇总

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);



// Complete the findNumber function below.
string findNumber(vector<int> arr, int k) {
    vector<int>::iterator pos_vector;
    for(pos_vector = arr.begin(); pos_vector < arr.end(); pos_vector++)
    {
        if(*pos_vector == k)
            return string("YES");
    }
    return string("NO");
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string arr_count_temp;
    getline(cin, arr_count_temp);

    int arr_count = stoi(ltrim(rtrim(arr_count_temp)));

    vector<int> arr(arr_count);

    for (int i = 0; i < arr_count; i++) {
        string arr_item_temp;
        getline(cin, arr_item_temp);

        int arr_item = stoi(ltrim(rtrim(arr_item_temp)));

        arr[i] = arr_item;
    }

    string k_temp;
    getline(cin, k_temp);

    int k = stoi(ltrim(rtrim(k_temp)));

    string res = findNumber(arr, k);

    fout << res << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}
typedef struct
{
    std::string unit_name;
    std::string unit_type;
    std::string in_symbol;
    int self_port_number;
    int unit_port_number;
    int value;
    bool init_flag;
} Input_Port_Value_Type;

typedef struct
{
    std::string unit_name;
    std::string unit_type;
    std::string out_symbol;
    std::string out_unit;
    int self_unit_value;
    int out_unit_port_number;
} Unit_Name_Value_Type;

std::vector<std::string> m_unit_register_sum;
std::vector<std::string> m_unit_register_negate;
std::vector<std::string> m_unit_register_max;
std::vector<std::string> m_unit_register_min;
std::vector<std::string> m_unit_register_mul;
 
std::vector<std::string> *m_unit_register_table[5] =
{&m_unit_register_sum, &m_unit_register_negate,
 &m_unit_register_max, &m_unit_register_min,
 &m_unit_register_mul};

std::vector<Input_Port_Value_Type> m_input_port_value;
std::vector<Unit_Name_Value_Type> m_unit_name_value;
 
int input_port_number = 0;
 
std::string get_unit_type_by_index(int index)
{
    std::string unit_type;
    switch(index)
    {
        case 0:
            unit_type = "sum";
            break;
 
        case 1:
            unit_type = "negate";
            break;
 
        case 2:
            unit_type = "max";
            break;
 
        case 3:
            unit_type = "min";
            break;
 
        case 4:
            unit_type = "mul";
            break;
 
        default:
            break;
    }
    return unit_type;
}
 
void unit_callback_func(std::string name, std::string type)
{
    if(type == std::string("sum"))
        m_unit_register_sum.push_back(name);
    else if(type == std::string("negate"))
        m_unit_register_negate.push_back(name);
    else if(type == std::string("max"))
        m_unit_register_max.push_back(name);
    else if(type == std::string("min"))
        m_unit_register_min.push_back(name);
    else if(type == std::string("mul"))
        m_unit_register_mul.push_back(name);
}

void input_callback_func(int port_num)
{
    input_port_number = port_num;
}
 
void connection_callback_func(std::string obj_from, std::string out_valid, std::string port_from, std::string obj_to, std::string in_valid, std::string port_to)
{
    int port_input = 0;
    bool search_result = false;
    int search_index = 0;
    std::vector<std::string>::iterator pos;
    if(obj_from == std::string("input"))
    {
        port_input = atoi(port_from.c_str());
        assert(port_input < input_port_number);

        if(obj_to == std::string("result"))
        {
            Input_Port_Value_Type no_unit_port_value;
            no_unit_port_value.unit_name = obj_to;
            m_input_port_value.push_back(no_unit_port_value);
            return;
        }

        for(search_index = 0; search_index < 5; search_index++)
        {
            if(!search_result)
            {
                for(pos = m_unit_register_table[search_index]->begin(); pos < m_unit_register_table[search_index]->end(); pos++)
                {
                    if(*pos == obj_to)
                    {
                        if(search_index == 1)
                        {
                            assert(input_port_number < 2);
                        }

                        search_result = true;
                        Input_Port_Value_Type new_port_value;
                        new_port_value.unit_name = obj_to;
                        new_port_value.unit_type = get_unit_type_by_index(search_index);
                        new_port_value.self_port_number = port_input;
                        new_port_value.unit_port_number = atoi(port_to.c_str());
                        new_port_value.init_flag = false;
 
                        m_input_port_value.push_back(new_port_value);
                        break;
                    }
                }
            }
            else
            {
                break;
            }
        }
    }
    else
    {
        for(search_index = 0; search_index < 5; search_index++)
        {
            if(!search_result)
            {
                for(pos = m_unit_register_table[search_index]->begin(); pos < m_unit_register_table[search_index]->end(); pos++)
                {
                    if(*pos == obj_from)
                    {
 
                        search_result = true;
                        Unit_Name_Value_Type new_unit_value;
                        new_unit_value.unit_name = obj_from;
                        new_unit_value.unit_type = get_unit_type_by_index(search_index);
                        new_unit_value.out_unit = obj_to;
 
                        m_unit_name_value.push_back(new_unit_value);
                        break;
                    }
                }
            }
            else
            {
                break;
            }
        }
    }
 
}
 
void value_callback_func(std::string input_name, std::string intput_port, std::string value)
{
    std::vector<Input_Port_Value_Type>::iterator pos_in;
    bool NeedCall = false;
    bool DeInitFlag = false;
 
    if(input_name == std::string("input"))
    {
        for(pos_in = m_input_port_value.begin(); pos_in < m_input_port_value.end(); pos_in++)
        {
            if(pos_in->unit_name == std::string("result"))
            {
                std::cout << atoi(value.c_str()) << std::endl;
                return;
            }

            if((pos_in->self_port_number) == atoi(intput_port.c_str()))
            {
                pos_in->value = atoi(value.c_str());
                pos_in->init_flag = true;
                NeedCall = true;
            }
 
            if(!(pos_in->init_flag))
            {
                DeInitFlag = true;
            }
        }
    }

    if((!NeedCall) || DeInitFlag)
        return;

    int value_input = 0;
    int value_result = 0;
    std::string m_unit_type;
 
    for(pos_in = m_input_port_value.begin(); pos_in < m_input_port_value.end(); pos_in++)
    {
        value_input = pos_in->value;
        if(pos_in->unit_type == std::string("sum"))
        {
            value_result += value_input;
        }
 
        if(pos_in->unit_type == std::string("negate"))
        {
            value_result = 0 - value_input;
        }
 
        if(pos_in->unit_type == std::string("max"))
        {
            if(pos_in == m_input_port_value.begin())
            {
                value_result = value_input;
            }
            else
            {
                value_result = value_result > value_input? value_result : value_input;
            }
        }
 
        if(pos_in->unit_type == std::string("min"))
        {
            if(pos_in == m_input_port_value.begin())
            {
                value_result = value_input;
            }
            else
            {
                value_result = value_result < value_input? value_result : value_input;
            }
        }
 
        if(pos_in->unit_type == std::string("mul"))
        {
            if(pos_in == m_input_port_value.begin())
            {
                value_result = value_input;
            }
            else
            {
                value_result *= value_input;
            }
        }
    }
 
    Unit_Name_Value_Type m_node_name_value;
    std::vector<Unit_Name_Value_Type>::iterator pos_in_node;
 
    for(pos_in_node = m_unit_name_value.begin(); pos_in_node < m_unit_name_value.end(); pos_in_node++)
    {
        if((pos_in_node->unit_name) == m_input_port_value[0].unit_name)
        {
            pos_in_node->self_unit_value = value_result;
            m_node_name_value = *pos_in_node;
        }
    }
 
    while(m_node_name_value.out_unit != std::string("result"))
    {
        for(pos_in_node = m_unit_name_value.begin(); pos_in_node < m_unit_name_value.end(); pos_in_node++)
        {
            if((pos_in_node->unit_name) == m_node_name_value.out_unit)
            {
                assert(pos_in_node->unit_type != std::string("negate"));
                pos_in_node->self_unit_value = 0 - m_node_name_value.self_unit_value;
                m_node_name_value = *pos_in_node;
                break;
            }
        }
 
        assert(pos_in_node != m_unit_name_value.end());
    }
 
    std::cout << m_node_name_value.self_unit_value << std::endl;
}

int main()
{
    class input_parser solution;
    solution.register_unit_callback(unit_callback_func);
    solution.register_input_callback(input_callback_func);
    solution.register_connection_callback(connection_callback_func);
    solution.register_value_callback(value_callback_func);
    solution.process();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AllenSun-1990

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值