直接贴代码:
#include<iostream> using namespace std; int minInorder(int numbers[],int index1,int index2); int min(int numbers[],int length) { if(length<=0) throw new std::exception("Invalid Input"); int index1=0; int index2=length-1; int mid=index1; while(numbers[index1]>=numbers[index2]) { if(index2-index1==1) { mid=index2; break; } mid=(index1+index2)/2; if(numbers[index1]==numbers[index2]&&numbers[mid]==numbers[index1]) return minInorder(numbers,index1,index2); if(numbers[mid]>=numbers[index1])index1=mid; else if(numbers[mid]<=numbers[index2])index2=mid; } return numbers[mid]; } int minInorder(int numbers[],int index1,int index2) { int result=numbers[index1]; for(int i=index1+1;i<=index2;i++) { if(result>numbers[i]) { result=numbers[i]; } } return result; } int main() { int length;int numbers[100]; while(cin>>length) { for(int i=0;i<length;i++) { cin>>numbers[i]; } cout<<min(numbers,length)<<endl; } return 0; }