1

假设我有一个双 for 循环。

/*Just a double for loop
*/    
for(int i = 0; i<IMAX; i++){
 for(int j = 0; j<JMAX; j++){
   count++;
   recover_loop_indices(count,IMAX,JMAX); /*this is not real world code.Just to illustrate what I mean*/
 }
}

我的问题是,给定和count,是否可以恢复唯一的循环索引 i 和 j?IMAXJMAX

4

1 回答 1

3

是的,基于计数:

i = floor(count / JMAX);
j = count % JMAX;

你根本不需要IMAX。事实上,这通常是人们可以从一系列像素流中重建图像的方式,只要给定流的宽度即可。

编辑:

我假设您想在 count++之前恢复 i 和 j 的值。要在 count++ 之后恢复它,请使用 (count-1)。

于 2012-08-06T16:15:46.620 回答