簡體   English   中英

2個數組中的所有可能組合

[英]All possible combinations out of 2 Arrays

我有2個數組:

  A {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 
  B {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

我想讓用戶能夠輸入斧頭數字,然后程序應該打印出所有可能的乘積= x = x的乘法應由數組A的2個數字1和數組B的另一個數字組成。這些數字不能相同。

我一直在搜索,我認為唯一可行的方法是嵌套循環。 我正在C#中做這個小項目,但是我不在乎它是否在Java中,我也理解Java。 先謝謝您的幫助。

int num_user;
        int[] x = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, };
        int[] y = new int[9];
        Console.WriteLine("Hello please input the number  you think could be the solution :) ");
        num_user = Convert.ToInt32(Console.ReadLine());
        for (int a = 0; a < x.Length; a++ )
            for (int b = 0; b < y.Length; b++)
                if num_user == a*b //and here is where I get lost
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int target = 5; //The number you want the 2 numbers to multiply to

var query =
    from x in a
    from y in b
    where y != x && x * y == target
    select new { x, y };

foreach (var pair in query) Console.WriteLine(pair);

一個簡單的嵌套循環將為此工作

for (int x = 0; x < 10; x++)
{
    for (int y = 0; y < 10; y++)
    {
        if (x != y && x*y == your_number) System.out.format("%d * %d = %d\n",x,y,your_number);
    }
}

代碼未經測試,但是可以正常工作。 您必須自己實現數組:)

using System;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        string input = string.Empty;
        int output = 0;
        do
        {
            Console.WriteLine("Enter number: ");
            input = /* Console.ReadLine(); */ "8";
        } while (!int.TryParse(input, out output));
        int[] first = new int[] {0,1,2,3,4,5,6,7,8,9};
        int[] second = new int[] {0,1,2,3,4,5,6,7,8,9};
        var list = new List<string>();
        for (int i = 0; i < first.Length; i++)
            for (int j = 0; j < second.Length; j++)
                if (first[i] * second[j] == output)
                    list.Add(first[i] + " x " + second[j] + " = " + output);
        foreach (var str in list) {
            Console.WriteLine(str);
        }
    }
}


在這里觀看現場演示

and will loop through the first array items and then loop through the second array.A multiplication is done for each item in the first by each item in the second using this nested loop logic. 此代碼接受用戶輸入 ,將循環遍歷第一個數組項,然后循環遍歷第二個數組。使用此嵌套,對第一項中的每個項與第二項中的每個項進行乘法運算。循環邏輯。

希望這對您有所幫助。

我會選擇雙循環,就像其他幾個答案一樣。

如果您想避免雙循環(出於某些特殊原因),可以不使用它。 在Java中:

    int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    // not using b

    int x = 8;

    for (int firstFactor : a) {
        if (firstFactor != 0 && x % firstFactor == 0) {
            int secondFactor = x / firstFactor;
            if (0 <= secondFactor && secondFactor <= 9 && secondFactor != firstFactor) {
                System.out.println("" + firstFactor + " * " + secondFactor);
            }
        }
    }

上面的代碼不適用於x等於0的情況,您必須特別對待這種情況(在雙循環方法中不必這樣做)。 對於x等於8的代碼,輸出:

1 * 8
2 * 4
4 * 2
8 * 1

我建議在避免笛卡爾聯接的同時使用Linq (如果AB大)?

  int[] A = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  int[] B = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

  int goal = 10;

  var result = A
    .Where(a => a != 0 && goal % a == 0) // a from A is divider of the goal 
    .Where(goal / a != a)                // a and b can't be the same 
    .Where(a => B.Contains(goal / a))    // b is in B
    .OrderBy(a => a)                     // let be nice
    .Select(a => string.Format("{0} {1}", a, goal / a));

測試

  Console.Write(string.Join(Environment.NewLine, result));

結果

  2 5
  5 2

壓力測試:

  int[] A = Enumerable.Range(0, 1000000).ToArray();
  int[] B = Enumerable.Range(0, 1000000).ToArray();

  int goal = 2016;

將在毫秒內返回

1 2016
2 1008
3 672
4 504
6 336
7 288
8 252
9 224
... 
504 4
672 3
1008 2
2016 1 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM