下面是源程序(编译通过)。有3个变量;我认为是O(n)的
------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace netiq
{
class Program
{
const int n = 10;
static int f(int k)
{
return (k % 2 > 0) ? ((k - 1) / 2 + n) : k / 2;
}
static int g(int k)
{
return (k
}
static string toString(int[] a)
{
string str = a[0].ToString();
for (int i = 1; i
{
str += " " + a[i];
}
return str;
}
static void Main(string[] args)
{
string str;
int[] a = new int[n * 2];
for (int i = 0; i
for (int k = 0; k
Console.WriteLine(toString(a));
// run algorithm
for (int k = 0; k
{
if (g(k) == a[k]) { continue; }
int i = k, j;
while (f(i) != k)
{
j = a[f(i)];
a[f(i)] = i;
i = j;
}
a[k] = i;
}
Console.WriteLine(toString(a));
return;
}
}
}
我觉得我解释不清楚
所有跟帖:
•
主要是难以判断一个位置有没有排好。
-乱弹-
♂
(355 bytes)
()
07/30/2009 postreply
17:30:25
•
你可以run一下这个程序,对任何n都适用
-说了就走-
♂
(357 bytes)
()
07/30/2009 postreply
20:47:50
•
你一直在假设a[k]=k。没有这个假设你的算法不成立
-康MM-
♀
(0 bytes)
()
07/31/2009 postreply
07:14:51
•
那么请问你的假设是什么?
-说了就走-
♂
(216 bytes)
()
07/31/2009 postreply
18:16:46
•
还是问冬瓜太郎吧,他说可以就可以
-康MM-
♀
(177 bytes)
()
08/02/2009 postreply
10:25:52
•
你说得很有道理
-说了就走-
♂
(2675 bytes)
()
08/02/2009 postreply
13:50:02
•
我还是觉得我们在说两件不同的事
-康MM-
♀
(0 bytes)
()
08/06/2009 postreply
17:49:24