一个长度为2N的数组,前面N个是数字,后面N个是字母,类似123abc,让转化为1a2b3c

来源:互联网 发布:博悦娱乐网络检测 编辑:程序博客网 时间:2024/06/05 00:08
#include <iostream>using namespace std;#include <vector>#include <string>void swap(int& a,int& b){        int temp = a;        a = b;        b = a;}void convert(vector<string>& ele,int gap){        int len = ele.size();        int n = len / 2;        if(gap < n)        {                   for(int i = gap  ; i < n; )                {                           for(int j = 0; j < gap; ++j)                        {                                   swap(ele[i + j],ele[n + i + j - gap  ]);                         }                        i += 2*gap;                }                convert(ele,2*gap);        }}int main(){        vector<string> res;        res.push_back("1");        res.push_back("2");        res.push_back("3");        res.push_back("a");        res.push_back("b");        res.push_back("c");        convert(res,1);        for(int i = 0; i < res.size(); ++i)                cout << res[i] << endl;        return 0;}

1 0
原创粉丝点击