0

例如

上)

for (int i=0;i<n;i++)

编辑后:我的最终答案是

for(int i =(n - 1); i > 1; i--)
 {
         factorial = factorial * i;             
 }  
 for (int j=n-2;j<factorial;j++)
 {

 }
4

4 回答 4

2

最简单的答案是 (int i = 0; i < Factorial(n); i++) {...

在实践中,通常 O(n!) 算法是那些通过尝试列表的所有不同排列来工作的算法,也就是说,您可以重新排序列表的所有不同方式。一个例子是找到通过地图中所有点的最短线,称为旅行商问题。您需要尝试所有不同的方法来遍历所有点,这将是 O(n!)。

IEnumerable<List<int>> nextPermutation(List<int> nodesLeft)
{
    if (nodesLeft.Count == 0)
    {
        yield return new List<int>();
    }
    else
    {
        for (int i = 0; i < nodesLeft.Count; i++)
        {
            List<int> newNodesLeft = new List<int>(nodesLeft);
            newNodesLeft.removeAt(i);

            foreach (List<int> subPermutation in nextPermutation(newNodesLeft)
            {
                subPermutation.add(nodesLeft[i]);
                yield return subPermutation;
            }
        }
    }
}

void main()
{
    foreach (List<int> permutation in nextPermutation(new List<int>(new int[]{1,2,3,4,5}))) {
        //every permutation of [1,2,3,4,5] will be generated here
        //this will take O(n!) to complete, where n is the number of nodes given (5 in this case)
    }
}
于 2012-02-05T06:50:43.033 回答
1

如果允许递归,则:

void loop(int n)
{
    if(n == 1) 
        return; // the program gets here exactly n! times

    for(int i=0; i<n; i++)
        loop(n-1);
}
于 2012-02-05T07:13:27.627 回答
0
fact = 1;
for( c = 1 ; c <= n ; c++ )
{
    fact = fact*c;
}

像这样?

于 2012-02-05T06:30:27.807 回答
0

如果我们在这里在同一页面上...我认为那看起来像..
Tau(fetch) + Tau(store) + (2Tau(fetch) + Tau(<) )*(N + 1) + (2Tau(fetch) + Tau(+) + Tau(store)) * N

于 2012-02-05T06:32:19.850 回答