C ++:最快速的方法来检查是否所有的数组元素都是平等的 [英] C++: Fastest method to check if all array elements are equal
本文介绍了C ++:最快速的方法来检查是否所有的数组元素都是平等的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是检查是否一个数组(preferable整数数组)的所有元素都是平等的最快方法。直到现在我一直在使用下面的code:
布尔检查(int数组[],INT N)
{
布尔标志= 0; 的for(int i = 0; I< N - 1;我++)
{
如果(阵列[I]!=阵列[I + 1])
标志= 1;
} 返回标志;
}
解决方案
INT检查(const int的一个[],INT N)
{
而( - N大于0和放大器;&放大器;一个[N] == A [0]);
返回N = 0!;
}
What is the fastest method to check if all elements of an array(preferable integer array) are equal. Till now I have been using the following code:
bool check(int array[], int n)
{
bool flag = 0;
for(int i = 0; i < n - 1; i++)
{
if(array[i] != array[i + 1])
flag = 1;
}
return flag;
}
解决方案
int check(const int a[], int n)
{
while(--n>0 && a[n]==a[0]);
return n!=0;
}
这篇关于C ++:最快速的方法来检查是否所有的数组元素都是平等的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文