现在的位置: 首页 > 综合 > 正文

调整序列

2014年02月17日 ⁄ 综合 ⁄ 共 445字 ⁄ 字号 评论关闭

调整序a1,a2,a3,a4,...b1,b2,b3,b4...成序列a1,b1,a2,b2,a3,b3...

要求:时间复杂度O(n),究竟复杂度O(1)

算法如下:

// cppTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
#include 
using namespace std;

void Process(char s[], int n)
{
	char t;
	int i = 1;

	do
	{
		if (i >= n / 2)
			i = 2 * (i - n / 2) + 1;
		else
			i = 2 * i;

		t = s[1];
		s[1] = s[i];
		s[i] = t ;
	} while (i != 1);

	cout << s << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	char t1[] = "ABCDEFG1234567";
	Process(t1, 14);

	char t2[] = "ABCDEF123456";
	Process(t2, 12);

	return 0;
}


抱歉!评论已关闭.