luowanqian
3/25/2018 - 12:21 PM

Huawei2018

华为2018校园实习招聘编程题

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

struct ELEMENT_STRU {
    unsigned int uiElementLength;
    unsigned int uiElementValue;
};

void Decode(unsigned int uiInputLen, unsigned char aInputByte[],
            unsigned int uiElementNum, ELEMENT_STRU astElement[])
{
    unsigned int uiElementValue, uiElementLength, pow2;
    int start, end;
    start = 0;
    for (int i=0; i<uiElementNum; i++) {
        uiElementLength = astElement[i].uiElementLength;
        uiElementValue = 0;
        pow2 = 1;
        end = start + uiElementLength;
        for (int j=end-1; j>=start; j--) {
            if (aInputByte[j] == '1')
                uiElementValue = uiElementValue + pow2;
            pow2 = 2 * pow2;
        }
        astElement[i].uiElementValue = uiElementValue;
        start = end;
    }
}

int main()
{
    string str;
    unsigned int uiInputLen, num, uiElementNum,flag;
    unsigned int numberBytes;
    int start, end;
    unsigned char *aInputByte = NULL;
    ELEMENT_STRU *astElement = NULL;
    while (cin >> uiInputLen) {
        aInputByte = (unsigned char *)malloc(sizeof(unsigned char) * 32 * uiInputLen);
        start = 0;
        end = 0;
        for (int i=1; i<=uiInputLen; i++) {
            cin >> str;
            if (str.size() == 3)        // 0xX
                numberBytes = 4;
            else if (str.size() == 4)   // 0xXX
                numberBytes = 8;
            else if (str.size() == 5)   // 0xXXX
                numberBytes = 12;
            else if (str.size() == 6)   // 0xXXXX
                numberBytes = 16;

            end = start + numberBytes;

            flag = 1;
            num = stoi(str, 0, 16);
            for (int j=end-1; j>=start; j--) {
                if (flag & num)
                    aInputByte[j] = '1';
                else
                    aInputByte[j] = '0';
                flag = flag << 1;
            }

            start = end;
        }

        cin >> uiElementNum;
        astElement = (ELEMENT_STRU*)malloc(sizeof(ELEMENT_STRU)*uiElementNum);
        for (int i=0; i<uiElementNum; i++) {
            cin >> num;
            astElement[i].uiElementLength = num;
        }

        Decode(uiInputLen, aInputByte, uiElementNum, astElement);

        for (int i=0; i<uiElementNum; i++)
            cout << astElement[i].uiElementValue << endl;

        free(aInputByte);
        free(astElement);
    }
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    while (cin >> str) {
        bool numeric;
        int long_start, long_end, long_len;
        int tmp_start, tmp_end;

        long_start = long_end = -1;
        long_len = 0;
        tmp_start = tmp_end = 0;

        numeric = false;
        int i;
        for (i=0; i<str.size(); i++) {
            if (str[i] >= '0' && str[i] <= '9') {
                if (numeric == false)
                    tmp_start = i;
                numeric = true;
            } else {
                if (numeric == true) {
                    tmp_end = i;
                    if ((tmp_end - tmp_start) >= long_len) {
                        long_start = tmp_start;
                        long_end = tmp_end;
                        long_len = tmp_end - tmp_start;
                    }
                }
                numeric = false;
            }
        }
        if (i == str.size() && numeric == true) {
            tmp_end = i;
            if ((tmp_end - tmp_start) >= long_len) {
                long_start = tmp_start;
                long_end = tmp_end;
                long_len = tmp_end - tmp_start;
            }
        }

        if (long_len > 0) {
            for (i=long_start; i<long_end; i++)
                cout << str[i];
            cout << "," << long_len << endl;
        } else {
            cout << ",0" << endl;
        }
    }

    return 0;
}