三星的两道编程题2019-10-30

#include
#include
#include
#include
#include
#include
using namespace std;
vector sparseInput(string A, const int n){
    vector res;
    res.reserve(2 * (n + 2));
    string result;
    int counter = 0;
    //将A赋值给一个stringstream流
    stringstream input(A);
    while (input >> result){
        //string转int
        res.push_back(stoi(result.c_str()));
    }
    return res;
}
struct point
{
    int x;
    int y;
};
vector parsePoints(vector data){
    vector::iterator it = data.begin();
    vector points;
    point p;
    for (it; it < data.end(); it++){
        p.x = *it;
        it++;
        p.y = *it;
        points.push_back(p);
    }
    points.shrink_to_fit();
    return points;
}
vector> enumDistance(vector data, vector points){
    vector::iterator itStart = points.begin(), itDistinct = points.begin();
    int row = 0, col = 0, d;
    vector> distinct;
    for (itStart; itStart < points.end(); itStart++){
        vector vec;
        for (itDistinct; itDistinct < points.end(); itDistinct++){
            d = abs((*itStart).x - (*itDistinct).x) + abs((*itStart).y - (*itDistinct).y);
            vec.push_back(d);
            cout << d << ' ';
        }
        cout << endl;
        itDistinct = points.begin();
        vec.shrink_to_fit();
        distinct.push_back(vec);
    }
    distinct.shrink_to_fit();
    return distinct;
}

void findOptimalPath(vector> dist, vector points){
    vector keys;
    int counter = 0;
    for (int i = 0; i < points.size(); i++)
        keys.push_back(i);
    keys.shrink_to_fit();
    while (next_permutation(keys.begin(), keys.end()))
    {
        counter++;
        vector::iterator it = keys.begin();
        for (it; it < keys.end(); it++){
            cout << '(' << (points[*it]).x << ',' << (points[*it]).y << ')' << ' ';
        }
        cout << endl;
    }
    cout << counter;//五个元素时counter为119,此处不算初始序列
}
int main(){
    string firstLine = "end";
    //读入一个字符串,并以回车结束
    string secondLine;
    vector> dist;
    while (1){
        cin >> firstLine;
        if (firstLine == "end")
            break;
        getchar();
        getline(cin, secondLine, '\n');
        vector resSecond = sparseInput(secondLine, stoi(firstLine.c_str()));
        int home[2] = { resSecond[0], resSecond[1] };
        int office[2] = { resSecond[2], resSecond[3] };
        resSecond.erase(resSecond.begin(), resSecond.begin() + 4);
        resSecond.shrink_to_fit();
        vector points = parsePoints(resSecond);
        dist = enumDistance(resSecond, points);
        findOptimalPath(dist, points);
        firstLine = "end";
    }
    system("pause");
    return 0;
}

第二道

#include
#include
#include
#include
using namespace std;
vector sparseInput(string A){
    vector res;
    string result;
    //将A赋值给一个stringstream流
    stringstream input(A);
    while (input >> result){
        //string转int
        res.push_back(atoi(result.c_str()));
    }
    return res;
}
int findParent(vector data, int cur){
    vector::iterator it = data.end() - 1;
    int counter = 0;
    for(it; it >= data.begin(); it--){
        if(counter % 2 == 0){
            if (*it == cur)
                return *(it - 1);
        }
        counter++;
        if (it == data.begin()){
            break;
        }
    }
    return -1;
}
vector enumParents(vector data, int v){
    vector parents;
    int p = findParent(data, v);
    if (p != -1){
        while (p != -1){
            parents.push_back(p);
            p = findParent(data, p);

        }
        return parents;
    }
    parents.push_back(v);
    return parents;
    
}
int find(vector second, int cur){
    int counter = second.size() - 1;
    while (counter + 1){
        if (second[counter] == cur){
            return cur;
        }
        counter--;
    }
    return -1;
}
int findLatestCommonParent(vector first, vector second){
    vector::iterator it = first.begin();
    int p;
    for (it; it < first.end(); it++){
        p = find(second, *it);
        if (p != -1)
            break;
    }
    return p;
    
}
int main(){
    string firstLine = "end";
    //读入一个字符串,并以回车结束
    string secondLine;
    while (1){
        getline(cin, firstLine, '\n');
        if (firstLine == "end")
            break;
        vector resFirst = sparseInput(firstLine);
        getline(cin, secondLine, '\n');
        vector resSecond = sparseInput(secondLine);
        int latestCommonParent;
        vector parentsFirst = enumParents(resSecond, resFirst[2]);
        vector parentsSecond = enumParents(resSecond, resFirst[3]);
        latestCommonParent = findLatestCommonParent(parentsFirst, parentsSecond);
        cout << latestCommonParent << endl;
        firstLine = "end";
    }
    system("pause");
    return 0;
}

你可能感兴趣的:(三星的两道编程题2019-10-30)