Java 2个阵列中的所有可能组合

Java 2个阵列中的所有可能组合,java,c#,nested-loops,Java,C#,Nested Loops,我有两个阵列: A {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} B {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 我想让用户能够输入一个x数字,然后程序应该打印出所有可能的=x的乘法。 =x的乘法应该由数组A的2个数字1和数组B的另一个数字组成。这些数字不能相同 我一直在搜索,我认为唯一可以工作的就是一个嵌套循环。 我正在用C语言做这个小项目,但我不在乎它是不是用Java语言,我也懂Java。 提前谢谢你的帮助 int num_user;

我有两个阵列:

  A {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 
  B {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
我想让用户能够输入一个x数字,然后程序应该打印出所有可能的=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

一个简单的嵌套循环可以解决这个问题

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);
        }
    }
}
观看现场演示
出于测试目的,此代码接受设置为8的用户输入,并将循环第一个数组项,然后循环第二个数组。使用此嵌套循环逻辑,第一个数组中的每个项与第二个数组中的每个项相乘。希望这对您有所帮助。

我选择双循环,就像其他几个答案一样

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);
如果您出于某种特殊原因想要避免双循环,可以不使用它。在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);
            }
        }
    }
我建议在避免笛卡尔连接的同时使用Linq,如果A和B很大怎么办

  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));
试验

结果

  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 

我正在用C语言做这个小项目,你的代码在哪里?是的。嵌套循环可以工作。你为什么不试着写一本呢?如果你正在学习编程,如果你自己编写代码,你会学到更多/更好。或者至少尝试一下……我认为展示嵌套循环方法和一些示例可以更好地解释问题,因为它不清楚数组是否总是数字0到9,或者如果它们是其他东西,程序是否也需要工作?我理解正确吗?“数字不能相同。”所以对于x=25,程序应该什么都不打印?5*5=25,但5与5相同。Ole用户输入一个数字,程序将在每个数组中查找两个数字:a和b。a*b=用户输入。a和b不能是同一个数字//有人能帮我吗?当我按enter键时,它总是会提交评论,但我只想留下一个空间!啊,@oscar6662,你已经编辑了这个问题。谢谢你这么做,我相信它越来越清晰了。如果问题是C控制台输入,那么我可能不是合适的帮助者。将检查任何即将进行的编辑。顺便说一句,我测试并阅读了您的代码,但没有得到int FIRSTFORT:a的作用{啊,这就是Java所谓的增强for循环。它遍历数组a,并依次将数组中的每个元素分配给firstFactor。我相信这与C中的foreach循环非常相似。请看,页面底部附近的示例。笛卡尔连接-从a中的x开始,从b中的y开始-应该小心使用。如果初始数组很大,比如说,a=Enumerable.Range010000.ToArray;b=Enumerable.Range010000.ToArray?Dennis_E谢谢你的帮助这很简单,我明白了!谢谢你的帮助这很有帮助,但对我来说有点太难了:谢谢你先生的帮助!还有时间!谢谢你的帮助!
  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