腾讯2017暑假实习笔试题-字符串编码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/charles1e/article/details/69056099
/*
腾讯2017暑假实习笔试题-字符串编码
输入:16的倍数的字符串
输出:编码后的结果

例子
输入:abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl
输出:
00000010 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
00000020 71 72 73 74 75 76 77 78 79 7a 61 62 63 64 65 66 qrstuvwxyzabcdef
00000030 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 ghijklmnopqrstuv
00000040 77 78 79 7a 61 62 63 64 65 66 67 68 69 6a 6b 6c wxyzabcdefghijkl
*/

#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;

int main()
{
    //打表
    vector<string> v(128, "");
    for (int i = 'a'; i <= 'z'; ++i)
    {
        char tmp[5];
        sprintf(tmp, "%02x", i);
        v[i] = tmp;
        //cout << v[i] << endl;
    }
    int index = 0;//先打印索引部分
    string in_str;
    cin >> in_str;
    while (in_str.size() >= 16)
    {
        string str = in_str.substr(0, 16);
        in_str = in_str.substr(16);
        char tmp[10];
        sprintf(tmp, "%08x", index);
        index += 16;
        cout << tmp << " ";//print index;

        for (auto i : str)
            cout << v[i] << " ";

        cout << str;

        cout << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/charles1e/article/details/69056099