程序代码:
bool foo( size_t row, size_t col, double* arr, size_t& r, size_t& c ) { for( c=1; c<col && arr[c]<=0; ++c ); if( c >= col ) return false; r = 0; double minval; for( size_t i=1; i<row; ++i ) { if( arr[i*col+c] > 0 ) { double val = arr[i+col+0] / arr[i*col+c]; if( r==0 || val<minval ) { r = i; minval = val; } } } return r!=0; } #include <iostream> using namespace std; int main( void ) { double arr[] = { 0,-1,3,-2, 7,3,-1,2, 12,-2,4,0, 10,-4,3,8 }; size_t r, c; if( foo(4,4,arr,r,c) ) cout << r << ' ' << c << endl; }