【问题标题】:Array of Integers - Index Combinations [closed]整数数组 - 索引组合
【发布时间】:2020-09-01 11:41:35
【问题描述】:

我有一个整数数组,例如:

int[][] matrix = new int[][] { 
    new int[2],  // array a,
    new int[2],  // array b,
    new int[3],  // array c,
    new int[4],  // array d,
    // ...       // array x
};

现在我想生成从每个数组 a、b、c、... x 中选择一个的所有可能的索引组合

这意味着在例子中:{new int[2], new int[2], new int[3]},我想得到这样的索引组合:

{a[0], b[0], c[0]}
{a[0], b[0], c[1]}
{a[0], b[0], c[2]}
{a[0], b[1], c[0]}
{a[0], b[1], c[1]}
{a[0], b[1], c[2]}
{a[1], b[0], c[0]}
{a[1], b[0], c[1]}
{a[1], b[0], c[2]}
{a[1], b[1], c[0]}
{a[1], b[1], c[1]}
{a[1], b[1], c[2]}

矩阵的长度未知,但矩阵中的每个数组至少有1个元素。

有没有人可以解决这个问题?

【问题讨论】:

  • 请您自己先试试。当您遇到特定问题时,请随时在此处提问。 SO 不是代码编写服务。
  • 我认为这不是代码编写问题,而是寻找有效算法的问题。我越想这个问题我就越困惑。据我了解,不可能使用经典排列来解决这个问题。我说的对吗?
  • @JulianHerbel:我不明白。你的数组有 3 个索引,0、1 和 2。但是你的结果数组包含 1,2 和 3。你的源数组的值根本不重要?
  • @TimSchmelter:也许我的问题有点令人困惑。我想要数组中所有可能的索引组合。看看treesong的答案。他解决了。再次感谢

标签: c# arrays


【解决方案1】:

这里是代码。

class MainClass
{
    public static void Main(string[] args)
    {
        printAll(new int[] { 2, 2, 4});
        printAll(new int[] { 3, 3, 4, 5, 6 });
        printAll(new int[] { 3, 3, 4, 5, 6, 7, 8, 9});
    }

    public static void printAll(int[] array)
    {
        int max = 1;
        for (int i = 0; i < array.Length; i++)
        {
            max *= array[i];
        }

        for (int row = 0; row < max; row++)
        {

            int[] line = new int[array.Length];

            int weight = 1;
            for (int j = 0; j < array.Length; j++)
            {
                int times = row / weight;
                // line[j] = times % array[j]; // but you want to start form 1 
                line[j] = 1 + (times % array[j]); 
                weight *= array[j];
            }

            Console.WriteLine(String.Join(",", line));
        }
    }
}

【讨论】:

  • 将其作为黑盒进行了测​​试,它对我有用。我目前不完全理解它,但我正在尝试进入它。非常感谢!我还必须了解编程中的其他数字系统。感谢您的提示!
  • 抱歉无法解释清楚。问题等于:stackoverflow.com/questions/1719594/…
【解决方案2】:

你可以试试这个:

 List<int> list = new List<int>(){1, 2, 3};
        List<int> lstResult = new List<int>();
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int z = 0; z < 3; z++)
                {
                    //Check if it exist before adding   
                    if (!lstResult.Contains(int.Parse(list[i].ToString() + list[i].ToString() + list[z].ToString())))
                    {
                        lstResult.Add(int.Parse(list[i].ToString() + list[i].ToString() + list[z].ToString()));
                    }
                }
                 //Check if it exist before adding
                if (!lstResult.Contains(int.Parse(list[i].ToString() + list[j].ToString() + list[i].ToString())))
                {
                    lstResult.Add(int.Parse(list[i].ToString() + list[j].ToString() + list[i].ToString()));
                }
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2013-07-04
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多