#include <iostream> using namespace std; int getInvertNumber(int arr[],int nLength); void main() { int arr[] = {1,2,3,4,5,6,7,8,9}; int brr[] = {1,3,4,6,8,9}; int sb = getInvertNumber(brr,sizeof(brr)/sizeof(brr[0])); cout<<sb<<endl; } int getInvertNumber(int arr[],int nLength) { int nLeft = 0; int nRight = nLength-1; int nMid; if(arr[nLeft]<arr[nRight]) return 0; while(nLeft<nRight) { nMid = ( nLeft + nRight ) / 2; if(arr[nMid]>arr[nLeft]) nLeft = nMid; else nRight = nMid; } return nLength -nMid-1; }
原文地址:http://blog.csdn.net/ddupd/article/details/40511943