#include<iostream>
using namespace std;
bool fn(int *a,int n)
{
if(n == 1)
return true;
else if(a[n-1] > a[n-2])
fn(a,n-1);//递归调用fn函数,直到n = 1,比较完成,返回true
else
return false;
}
void main()
{
int a[] = {12,23,34,45,56,67,78,89,99};
//int a[] = {12,23,0,45,56,67,58,89,99};
//int a[] = {666};
int n = sizeof(a)/sizeof(a[0]);
if(n > 1 && fn(a,n))
cout<<"a[]是递增数组"<<endl;
else if(n == 1)
cout<<"a[]中只有一个元素,不能判断数组是否递增"<<endl;
else
cout<<"a[]不是递增数组"<<endl;
}
递归法判断递增数组
最新推荐文章于 2022-01-19 19:06:44 发布