📜  两组的笛卡尔积

📅  最后修改于: 2021-06-25 13:20:00             🧑  作者: Mango

设A和B为两个集合,笛卡尔积A×B是A和B中所有有序元素对的集合
A×B = {{x,y}:x∈A,y∈B}

给定两个有限的非空集,编写一个程序来打印笛卡尔乘积。
例子 :

Input : A = {1, 2}, B = {3, 4}
Output : A × B = {{1, 3}, {1, 4}, {2, 3}, {2, 4}}

Input  : A = {1, 2, 3} B = {4, 5, 6}
Output : A × B = {{1, 4}, {1, 5}, {1, 6}, {2, 4}, 
         {2, 5}, {2, 6}, {3, 4}, {3, 5}, {3, 6}}
CPP
// C++ Program to find the Cartesian Product of Two Sets
#include 
 
void findCart(int arr1[], int arr2[], int n, int n1)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n1; j++)
            printf("{%d, %d}, ", arr1[i], arr2[j]);
}
 
int main()
{
    int arr1[] = { 1, 2, 3 }; // first set
    int arr2[] = { 4, 5, 6 }; // second set
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    findCart(arr1, arr2, n1, n2);
    return 0;
}


Java
// Java Program to find the
// Cartesian Product of Two Sets
import java.io.*;
import java.util.*;
 
class GFG {
 
    static void findCart(int arr1[], int arr2[],
                                    int n, int n1)
    {
        for (int i = 0; i < n; i++)
          for (int j = 0; j < n1; j++)
            System.out.print("{"+ arr1[i]+", "
                             + arr2[j]+"}, ");
    }
    // Driver code
    public static void main (String[] args) {
         
        // first set
        int arr1[] = { 1, 2, 3 };
         
        // second set
        int arr2[] = { 4, 5, 6 };
         
        int n1 = arr1.length;
        int n2 = arr2.length;
        findCart(arr1, arr2, n1, n2);
    }
}
 
 
// This code is contributed by Nikita Tiwari.


Python3
# Python3 Program to find the
# Cartesian Product of Two Sets
 
def findCart(arr1, arr2, n, n1):
 
    for i in range(0,n):
        for j in range(0,n1):
            print("{",arr1[i],", ",arr2[j],"}, ",sep="",end="")
 
# Driver code
arr1 = [ 1, 2, 3 ] # first set
arr2 = [ 4, 5, 6 ] # second set
 
n1 = len(arr1) # sizeof(arr1[0])
n2 = len(arr2) # sizeof(arr2[0]);
 
findCart(arr1, arr2, n1, n2);
 
# This code is contributed
# by Smitha Dinesh Semwal


C#
// C# Program to find the
// Cartesian Product of Two Sets
using System;
 
class GFG {
 
    static void findCart(int []arr1, int []arr2,
                                    int n, int n1)
    {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n1; j++)
                Console.Write("{" + arr1[i] + ", "
                                + arr2[j] + "}, ");
    }
     
    // Driver code
    public static void Main () {
         
        // first set
        int []arr1 = { 1, 2, 3 };
         
        // second set
        int []arr2 = { 4, 5, 6 };
         
        int n1 = arr1.Length;
        int n2 = arr2.Length;
         
        findCart(arr1, arr2, n1, n2);
    }
}
 
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

{{1, 4}, {1, 5}, {1, 6}, {2, 4}, {2, 5}, {2, 6}, {3, 4}, {3, 5}, {3, 6}}

实际示例:
1)纸牌集是四个元素集到一组13个元素的笛卡尔积。
2)二维坐标系是两组实数的笛卡尔积。
参考:
https://zh.wikipedia.org/wiki/笛卡尔产品