问题描述
我能够实现在C.But阵列的静态数笛卡尔产品,我想建立一个code以动态输入的数量arrays.Can有人提供一些线索如何做到这一点只使用数组。如果这是不可能的阵列,请给我建议其他solution.Thank you.Here低于3阵列的笛卡尔乘积我的code。
#包括LT&;&stdio.h中GT;INT主要(无效)
{
INT数组1 [100];
INT数组2 [100];
INT ARRAY3 [100];
INT cartesian_array [100] [100];
INT M,N,O;
INT I,J;
INT p = 0时,Q = 0,R = 0;
INT X,Y,Z;
INT cartesian_arr_row_len;
INT cartesian_arr_col_len;
的printf(请输入第一个数组的大小:\\ n);
scanf函数(%d个,&安培; M); 的printf(请输入第二个数组的大小:\\ n);
scanf函数(%d个,&安培; N); 的printf(进入第三排的大小:\\ n);
scanf函数(%d个,&安培; O); 的printf(请输入第一个数组元素:\\ n);
对于(i = 0; I<米;我++)
scanf函数(%d个,&安培;数组1 [I]); 的printf(请输入第二个数组元素:);
对于(i = 0; I< N;我++)
scanf函数(%d个,&安培;数组2 [I]); 的printf(进入第三个数组元素:);
对于(i = 0; I< O;我++)
scanf函数(%d个,&安培; ARRAY3 [I]); cartesian_arr_row_len = M * N * O;
cartesian_arr_col_len = 3; X = cartesian_arr_row_len /米;
Y = cartesian_arr_row_len /(M * N);
Z = 0; 对于(i = 0; I< cartesian_arr_row_len;我++)
{
为(J = 0; J< cartesian_arr_col_len; J ++)
{
如果(J == 0)
{
cartesian_array [I] [J] = ARRAY1 [P / X];
p ++;
} 如果(J == 1)
{
cartesian_array [I] [J] =数组2 [Q / Y];
q ++;
如果(Q> = N * Y)
Q = 0;
} 如果(J == 2)
{
cartesian_array [I] [J] = ARRAY3 [R%Z]。
- [R ++;
}
} } 的printf(两个数组的乘积为:\\ n);
对于(i = 0; I< cartesian_arr_row_len;我++)
{
为(J = 0; J< cartesian_arr_col_len; J ++)
{
的printf(%d个\\ t的,cartesian_array [I] [J]);
}
的printf(\\ n);
} 返回0;
}
您可以阅读的Java code和自己翻译吗?
进口的java.util。*;类CartesianIterator< T>实现迭代器<名单,LT; T>> { 私人最终名单<名单,LT; T>> lilio;
私人INT电流= 0;
私人最终总算; 公共CartesianIterator(最终名单,LT;名单< T>>劳工组织){
lilio = LLO;
长材产品= 1L;
对于(列表< T> LIO:lilio)
产品* = lio.size();
最后=产品;
} 公共布尔规则hasNext(){
返回当前=最后!;
} 公开名单< T>下一个 () {
当前++;
得到的回报(电流 - 1,lilio);
} 公共无效删除(){
当前++;
} 私人列表< T>得到(最终诠释N,最终的名单,LT;名单< T>>丽丽){
开关(lili.size())
{
情况下0:返回新的ArrayList< T> (); //没有打破以往的回报;
默认:{
清单< T>内= lili.get(0);
清单< T> LO =新的ArrayList< T> ();
lo.add(inner.get(N%inner.size()));
lo.addAll(获得(N / inner.size(),lili.subList(1,lili.size())));
返回LO;
}
}
}
}类CartesianIterable< T>实现可迭代<名单,LT; T>> { 私人列表<名单,LT; T>> lilio; 公共CartesianIterable(列表<名单,LT; T>>劳工组织){
lilio = LLO;
} 公共迭代器<名单,LT; T>>迭代(){
返回新CartesianIterator< T> (lilio);
}
}类CartesianIteratorTest { 公共静态无效的主要(字串[] args){
清单<性格> LA = Arrays.asList(新字[] {'A','B'});
清单<性格>磅= Arrays.asList(新字[] {'B','C'});
清单<性格> LC = Arrays.asList(新字[] {'C','A'});
清单<名单,LT;性格>>有限责任公司=新的ArrayList<名单,LT;性格>> ();
llc.add(LA);
llc.add(磅);
llc.add(LC); CartesianIterable<性格> CI =新CartesianIterable<性格> (LLC);
对于(列表<性格>罗:CI)
展会(LO); LA = Arrays.asList(新字[] {'X','Y','Z'});
磅= Arrays.asList(新字[] {'B'});
LC = Arrays.asList(新字[] {'C'});
有限责任公司=新的ArrayList<名单,LT;性格>> ();
llc.add(LA);
llc.add(磅);
llc.add(LC); CI =新CartesianIterable<性格> (LLC);
对于(列表<性格>罗:CI)
展会(LO);
} 公共静态无效秀(列表<性格> LO){
System.out.print(();
对于(对象o:LO)
System.out.print(O);
的System.out.println());
}
}
我不知道C中的是否存在迭代器类似的事情迭代
最有可能是没有帮助你。
如果我们有3组(1,2,3)(4,5)(6,7,8),我们有18 = 3x2x3结果我们预期。我们可以,比如迭代器7的位置计算结果如下:
7%3 = 1 = GT; (1,2,3)[1] = 2(数模第一组大小)
7/3 = 2(INT科)(数量DIV 1组的大小)
2%2 = 0 => (4,5)[0] = 4(休息模第2组的大小)
2/2 = 0
0%3 = 0 => (7,8,9)= GT; 7IDX G1 G2 G3
0 1 4 6
1 2 4 6
2 3 4 6
3 1 5 6
4 2 5 6
5 3 5 6
6 1 4 7
7 2 4 7
8 3 4 7
9 1 5 7
10 2 5 7
11 3 5 7
12 1 4 8
13 2 4 8
14 3 4 8
15 1 5 8
16 2 5 8
17 3 5 8
I am able to achieve cartesian product of static number of arrays in C.But I would like to build a code dynamically taking the number of input arrays.Can someone shed some light how to do this "only using arrays".If it's not possible with arrays please suggest me other solution.Thank you.Here is my code below for cartesian product of 3 arrays.
#include<stdio.h>
int main(void)
{
int array1[100];
int array2[100];
int array3[100];
int cartesian_array[100][100];
int m,n,o;
int i,j;
int p=0,q=0,r=0;
int x,y,z;
int cartesian_arr_row_len;
int cartesian_arr_col_len;
printf("Enter the size of first array:\n");
scanf("%d",&m);
printf("Enter the size of second array:\n");
scanf("%d",&n);
printf("Enter the size of third array:\n");
scanf("%d",&o);
printf("Enter the first array elements:\n");
for(i=0;i<m;i++)
scanf("%d",&array1[i]);
printf("Enter the second array elements:");
for(i=0;i<n;i++)
scanf("%d",&array2[i]);
printf("Enter the third array elements:");
for(i=0;i<o;i++)
scanf("%d",&array3[i]);
cartesian_arr_row_len=m*n*o;
cartesian_arr_col_len=3;
x=cartesian_arr_row_len/m;
y=cartesian_arr_row_len/(m*n);
z=o;
for(i=0;i<cartesian_arr_row_len;i++)
{
for(j=0;j<cartesian_arr_col_len;j++)
{
if(j==0)
{
cartesian_array[i][j]=array1[p/x];
p++;
}
if(j==1)
{
cartesian_array[i][j]=array2[q/y];
q++;
if(q>=n*y)
q=0;
}
if(j==2)
{
cartesian_array[i][j]=array3[r%z];
r++;
}
}
}
printf("The Cartesian Product of two arrays is:\n");
for(i=0;i<cartesian_arr_row_len;i++)
{
for(j=0;j<cartesian_arr_col_len;j++)
{
printf("%d\t",cartesian_array[i][j]);
}
printf("\n");
}
return 0;
}
Can you read Java code, and translate it yourself?
import java.util.*;
class CartesianIterator <T> implements Iterator <List <T>> {
private final List <List <T>> lilio;
private int current = 0;
private final long last;
public CartesianIterator (final List <List <T>> llo) {
lilio = llo;
long product = 1L;
for (List <T> lio: lilio)
product *= lio.size ();
last = product;
}
public boolean hasNext () {
return current != last;
}
public List <T> next () {
++current;
return get (current - 1, lilio);
}
public void remove () {
++current;
}
private List<T> get (final int n, final List <List <T>> lili) {
switch (lili.size ())
{
case 0: return new ArrayList <T> (); // no break past return;
default: {
List <T> inner = lili.get (0);
List <T> lo = new ArrayList <T> ();
lo.add (inner.get (n % inner.size ()));
lo.addAll (get (n / inner.size (), lili.subList (1, lili.size ())));
return lo;
}
}
}
}
class CartesianIterable <T> implements Iterable <List <T>> {
private List <List <T>> lilio;
public CartesianIterable (List <List <T>> llo) {
lilio = llo;
}
public Iterator <List <T>> iterator () {
return new CartesianIterator <T> (lilio);
}
}
class CartesianIteratorTest {
public static void main (String[] args) {
List <Character> la = Arrays.asList (new Character [] {'a', 'b'});
List <Character> lb = Arrays.asList (new Character [] {'b', 'c'});
List <Character> lc = Arrays.asList (new Character [] {'c', 'a'});
List <List <Character>> llc = new ArrayList <List <Character>> ();
llc.add (la);
llc.add (lb);
llc.add (lc);
CartesianIterable <Character> ci = new CartesianIterable <Character> (llc);
for (List<Character> lo: ci)
show (lo);
la = Arrays.asList (new Character [] {'x', 'y', 'z'});
lb = Arrays.asList (new Character [] {'b'});
lc = Arrays.asList (new Character [] {'c'});
llc = new ArrayList <List <Character>> ();
llc.add (la);
llc.add (lb);
llc.add (lc);
ci = new CartesianIterable <Character> (llc);
for (List<Character> lo: ci)
show (lo);
}
public static void show (List <Character> lo) {
System.out.print ("(");
for (Object o: lo)
System.out.print (o);
System.out.println (")");
}
}
I don't know whether iterator-similar thing exists in C. The iterable
is most probably of no help for you.
The idea of the code is, to have a counter, which creates an index over all elements of the resultset, and calculate the element, bound to that value.
If we have 3 groups (1,2,3)(4,5)(6,7,8), we have 18=3x2x3 results we expect. We can, for Iterator position 7 for example calculate the result as follows:
7 % 3 = 1 => (1,2,3)[1] = 2 (number modulo 1st group size)
7 / 3 = 2 (int division) (number div 1st group size)
2 % 2 = 0 => (4,5)[0] = 4 (rest modulo 2nd group size)
2 / 2 = 0
0 % 3 = 0 => (7,8,9) => 7
idx g1 g2 g3
0 1 4 6
1 2 4 6
2 3 4 6
3 1 5 6
4 2 5 6
5 3 5 6
6 1 4 7
7 2 4 7
8 3 4 7
9 1 5 7
10 2 5 7
11 3 5 7
12 1 4 8
13 2 4 8
14 3 4 8
15 1 5 8
16 2 5 8
17 3 5 8
这篇关于用C多个阵列的笛卡尔乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!