天天看点

递归方法判断数组是否为递增数组

#include<iostream>
using namespace std;
bool IsIncrease( int a[], int n )
{
    if( n==1 )
        return true;
    if( n==2 )
        return a[n-1] >= a[n-2];
    return IsIncrease( a,n-1) && ( a[n-1] >= a[n-2] );
    //n个元素的数组的最后一个元素是a[n-1]

}
int main()
{
    int bb[]={1,2,3,4,5,9,33};
    int cc[]={3,4,7,8,1};
    cout<<"bb[] IsIncrease  1 yes 0 no :"<<IsIncrease(bb,sizeof(bb)/sizeof(int))<<endl;
    cout<<"cc[] IsIncrease  1 yes 0 no :"<<IsIncrease(cc,sizeof(cc)/sizeof(int))<<endl;
}
/*********************
bb[] IsIncrease  1 yes 0 no :1
cc[] IsIncrease  1 yes 0 no :0

Process returned 0 (0x0)   execution time : 0.952 s
Press any key to continue.

**********************/