【问题标题】:Generating all permutations by the minimum transposition number C -> C #通过最小转置数 C -> C # 生成所有排列
【发布时间】:2017-10-26 22:04:56
【问题描述】:

我想将此代码转换为 C#,但出现了问题。我不想用指针来做到这一点。谁能知道如何尽快改善它?这是通过最小数量的转置生成所有排列。这是我要转换的代码。 a link.

namespace Algorytm8
{
    class Program
    {
        private int[] P;
        private int count, n;
        public int silnia(int n) {    
            return (n == 1 || n == 0) ? 1: silnia(n-1) * n;    
        }
        public void swap(int i, int j)
        {
            int pom;
            pom = P[i];
            P[i] = P[j];
            P[j] = pom;
        }
        public int b(int m, int i)
        {
            if ((m % 2 == 0) && (m > 2))
            {
                if (i < m - 1)
                {
                    return i;
                }
                else
                {
                    return m - 2;
                }
            }
            else
            {
                return m - 1;
            }
        }    
        public void perm(int m)
        {
            int Mi;
            int[] I = P;    
            for (int i = 1; i <= m; i++)
            {
                I[i] = 1;
            }
            Mi = 1;
            while (count < silnia(m))
            {
                if (I[Mi]==Mi)
                {
                    if (I[Mi]==1 && Mi==1)
                    {
                        count++;
                        Console.Write(count);
                        for (int i = 1; i <= n; i++)
                        {
                            Console.Write(P[i]);
                        }
                        Console.Write("\n");
                    }
                    for (int i = 1; i <= Mi; i++)
                    {
                        I[i] = 1;
                    }
                    Mi++;      
                }
                if (I[Mi] < Mi)
                {    
                    int i = I[Mi];
                    swap(b(Mi, i), Mi);
                    I[Mi]++;
                    Mi = 1;                        
                }    
            }            
        }
        public void getData()
        {
            Console.WriteLine("Podaj ilość elementów: ");
            int n = Int32.Parse(Console.ReadLine());
            P = new int[n];
            for (int i = 0; i < n; i++)
            {
                P[i] = i + 1;
            }
            perm(n - 1);
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.getData();
            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 出了什么问题?
  • 坏值列举我
  • 然后拿一个调试器调试一下。

标签: c# c permutation


【解决方案1】:

我复制/粘贴了它很容易转换为 C# 的 C 程序

您遇到的一些主要问题:int[] I = P; 没有创建新数组。将 int n = Int32.Parse(Console.ReadLine()); 更改为 n = Int32.Parse(Console.ReadLine());。编译器应该给你关于初始化的警告。

swap改成swap(ref int i, ref int j),这样更像C版,更通用。

class Program
{
    private int[] P;
    private int count, n;

    public int silnia(int n)
    {
        return (n == 1 || n == 0) ? 1 : silnia(n - 1) * n;
    }

    public void swap(ref int i, ref int j)
    {
        int temp = i;
        i = j;
        j = temp;
    }

    public int B(int m, int i)
    {
        if ((m % 2 == 0) && (m > 2))
        {
            if (i < (m - 1))
                return i;
            else
                return m - 2;
        }
        else
            return m - 1;
    }

    public void PERM(int m)
    {
        int[] I = new int[m + 1];

        for (int i = 1; i <= m; i++)
            I[i] = 1;
        int Mi = 1;

        while (count < silnia(m))
        {
            if (I[Mi] == Mi)
            {
                if (I[Mi] == 1 && Mi == 1)
                {
                    count++;
                    Console.Write("{0}:\t", count);
                    for (int i = 1; i <= n; i++)
                        Console.Write("{0} ", P[i]);
                    Console.WriteLine();
                }
                for (int i = 1; i <= Mi; i++)
                    I[i] = 1;
                Mi++;
            }
            if (I[Mi] < Mi)
            {
                int i = I[Mi];
                swap(ref P[B(Mi, i)], ref P[Mi]);
                I[Mi]++;
                Mi = 1;
            }
        }
    }

    public void getData()
    {
        Console.WriteLine("Podaj ilość elementów: ");
        n = Int32.Parse(Console.ReadLine());
        P = new int[n + 1]; 
        for (int i = 1; i <= n; i++)
            P[i] = i;
        count = 0;
        PERM(n);
    }

    static void Main(string[] args)
    {
        Program prog = new Program();
        prog.getData();
        Console.ReadKey();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多