判断一个数组是否为单一完全循环数组
2012-05-22 03:36
316 查看
判断一个数组是否为单一完全循环数组
/* Determine if an array is a single complete circle by the following rules:
* a[0]=1 -> a[1]=3 -> a[4]= 2 -> ...
* If array a is of length 6, a[6] -> a[0]
*/
using namespace std;
/*
Starting from a[0] without loss of generality, it can only return
to a[0] after traversing all data items. Anytime before that if it
goes back to a[0], this array is not a single and complete circle.
*/
bool isSingleCompleteCircle(int* x, int len)
{
int index = 0;
for (int i = 0; i < len; i++)
{
index = ((index%len) + (x[index]%len)) % len;
// to prevent overflow (not all the time, but when len is not very large)
if ((index == 0) &&(i < len-1))
{
return false;
}
}
return true;
}
int main(void)
{
int a[]={1, 1, 1, 5};
int len = sizeof(a)/sizeof(a[0]);
bool result = isSingleCompleteCircle(a, len);
cout << "A single and complete circle? " << (result?"Yes":"No") << endl;
return 0;
}
/* Determine if an array is a single complete circle by the following rules:
* a[0]=1 -> a[1]=3 -> a[4]= 2 -> ...
* If array a is of length 6, a[6] -> a[0]
*/
using namespace std;
/*
Starting from a[0] without loss of generality, it can only return
to a[0] after traversing all data items. Anytime before that if it
goes back to a[0], this array is not a single and complete circle.
*/
bool isSingleCompleteCircle(int* x, int len)
{
int index = 0;
for (int i = 0; i < len; i++)
{
index = ((index%len) + (x[index]%len)) % len;
// to prevent overflow (not all the time, but when len is not very large)
if ((index == 0) &&(i < len-1))
{
return false;
}
}
return true;
}
int main(void)
{
int a[]={1, 1, 1, 5};
int len = sizeof(a)/sizeof(a[0]);
bool result = isSingleCompleteCircle(a, len);
cout << "A single and complete circle? " << (result?"Yes":"No") << endl;
return 0;
}
相关文章推荐
- 用于判断数组是否完全包含另外的一个数组的所有元素
- 不要用数组循环判断等于的方式,来判断一个对象是否在一个集合内
- 不良代码展示-不要用数组循环判断等于的方式,来判断一个对象是否在一个集合内
- 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- Javascript判断一个对象是否为数组
- 判断一个数组是否存在三个元素为某一定值
- C#中怎么判断一个数组中是否存在某个数组值
- JS判断一个数组中是否有重复值的三种方法
- js判断一个元素是否在数组中
- 判断一个单项链表是否有循环,如果有循环输出循环点
- 关于判断某一数组内容是否为一个二叉搜索树的后序遍历结果
- 每天三道冲刺工作--如何用递归算法判断一个数组是否是递增
- 判断一个变量是否在一个数组里
- 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- Java判断一个对象是否是数组
- PHP中判断一个数组是否为空?
- 判断一个字符串是否在一个字符串数组中
- 哈希(3) - 判断一个数组是否为另一个数组的子集
- java判断循环数组是否连续并获取不连续的项以及重复项
- 一个长度为N的整形数组,数组中每个元素的取值范围是[0,N-1],写一个算法判断数组中是否存在重复的数字