某同事去某公司面试, 遇到笔试题目:判断是否为递增数组, 原题是填空题, 现在我来写写完整的程序:

#include <iostream>
using namespace std;

bool isIncrease(int a[], int size)
{
	if(NULL == a || size <= 0)
	{
		exit(1);
	}

	if(1 == size)
	{
		return false;
	}

	if(a[0] > a[1])
	{
		return false;
	}

	if(2 == size)  // 这个分支千万不能少
	{
		return true;     
	}

	return isIncrease(a + 1, size -1);
}


int main()
{
	int test[][5] = 
	{
		{1, 2, 3, 4, 5},
		{1, 1, 3, 4, 5},
		{5, 1, 2, 3, 4},
		{5, 5, 5, 5, 5},
		{1, 4, 2, 3, 5},
		{1, 2, 3, 4, 4},
		{1, 2, 3, 4, 4},
		{1, 2, 2, 2, 5},
	};

	int n = sizeof(test) / sizeof(test[0]);
	int i = 0;
	for(i = 0; i < n; i++)
	{
		if(isIncrease(test[i], 5))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
	}

	return 0;
}
      初步测试了一下, ok.




本文转载:CSDN博客