t**********s 发帖数: 930 | 1 比如下面这个同时求出一个数组中最大和最小值的算法,我已算出其元素之间的比较次
数是3n/2-2,这要比分别扫描数组两次所比较次数2*(n-1)要少。
我的问题是如何让这个递归函数自己记下算出递归的次数?
我对递归函数间如何传递数据很糊涂。
vector minmax( vector V, int l, int u )
{
int m;
vector M( 2 ), M1( 2 ), M2( 2 );
if( u == l ) // one element - no comparisons needed
{
M[0] = V[l]; M[1] = V[u];
return M;
}
if( u-l == 1 ) // two elements - only one comparison
{
if( V[l] <= V[u] )
{
M[0] = V[l]; M[1] = V[u];
| g*****g 发帖数: 34805 | 2 传个引用参数进去,跟使全局变量一个效果。
【在 t**********s 的大作中提到】 : 比如下面这个同时求出一个数组中最大和最小值的算法,我已算出其元素之间的比较次 : 数是3n/2-2,这要比分别扫描数组两次所比较次数2*(n-1)要少。 : 我的问题是如何让这个递归函数自己记下算出递归的次数? : 我对递归函数间如何传递数据很糊涂。 : vector minmax( vector V, int l, int u ) : { : int m; : vector M( 2 ), M1( 2 ), M2( 2 ); : : if( u == l ) // one element - no comparisons needed
| t**********s 发帖数: 930 | 3 I got it myself. Just increase the vector size to 3 with the third one
holding the number of comparison.
【在 g*****g 的大作中提到】 : 传个引用参数进去,跟使全局变量一个效果。
|
|