English 中文(简体)
专题: 在你通过两个阵列的汽车产品时,能否计算出行位?
原标题:Math Question: Is it possible to calculate the row position as you loop through a cartesian product of two arrays?

是否有可能计算两种阵列的汽车产品中的行踪位置?

例如,如果你拥有两行和三行之一,便很容易计算出汽车产品的规模(Array1.Rows.Count* Array2.Rows.Count=6),但你可以通过各个阵列进行蒸.,并公正地利用各行的产物来计算油轮产品中的浏览量。

Array1.Row * Array2.Row
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6

是否有办法从Array1获得结果1、2、3、4、5、6。 页: 1 罗以如下方式通过:

For 1 To Array1.Rows.Count
    For 1 To Array2.Rows.Count
         some formula here to obtain: 
        Cartesian.Row = Cartesian.Row + 1
    Next Array2.Row
Next Array1.Row

感谢!

最佳回答

我不敢肯定我是否理解这个问题,但你只是要求简单地将2D-1D指数转变吗?

如果是的话,公式是(基于1的指数计算)

FOR i1 = 1..N1 DO
  FOR i2 = 1..N2 DO
     (i1,i2) => ((i1-1) * N2) + i2)

这里是 Java的雕像:

public class Carte {
    public static void main(String[] args) {
        count(1, 2);
        count(2, 1);
        count(3, 2);
    }   
    public static void count(final int N1, final int N2) {
        System.out.println(N1 + "x" + N2);
        for (int i1 = 1; i1 <= N1; i1++) {
            for (int i2 = 1; i2 <= N2; i2++) {
                System.out.format("(%d,%d)=%d%n", i1, i2, ((i1-1) * N2 + i2));
            }
        }
    }
}

印本:

1x2
(1,1)=1
(1,2)=2
2x1
(1,1)=1
(2,1)=2
3x2
(1,1)=1
(1,2)=2
(2,1)=3
(2,2)=4
(3,1)=5
(3,2)=6

See also

问题回答

暂无回答




相关问题
adding an index to sql server

I have a query that gets run often. its a dynmaic sql query because the sort by changes. SELECT userID, ROW_NUMBER(OVER created) as rownumber from users where divisionID = @divisionID and ...

Linq to SQL nvarchar problem

I have discovered a huge performance problem in Linq to SQL. When selecting from a table using strings, the parameters passed to sql server are always nvarchar, even when the sql table is a varchar. ...

TableView oval button for Index/counts

Can someone help me create an index/count button for a UITableView, like this one? iTunes http://img.skitch.com/20091107-nwyci84114dxg76wshqwgtauwn.preview.jpg Is there an Apple example, or other ...

Move or copy an entity to another kind

Is there a way to move an entity to another kind in appengine. Say you have a kind defines, and you want to keep a record of deleted entities of that kind. But you want to separate the storage of ...

热门标签