扫描阵列特定次数,然后找出元素

Scan through the array a particular amount of times then cout the element

本文关键字:然后 元素 阵列 扫描      更新时间:2023-10-16

我想遍历一个数组特定次数(k(,看看它在哪里停止,例如,我1,2,3,4输出值1k = 5因为当它到达数组的末尾时,它会重新开始并继续计数,直到达到k = 5;我希望这是有道理的,这就是我目前所拥有的:

int numbers[] = {1,2,3,4};
int count =0;
int K =5;
while (count !=k)
{
//dont know what to do here
}

请解释一下逻辑,我无法得到这个。

为什么要遍历数组?只需按数组的大小Kmodnumbers.

int numbers[] = {1,2,3,4};
int K = 5;
int arraySize = sizeof(numbers) / sizeof(numbers[0]);
int count = K % arraySize;
return numbers[count];

由于您的数组大小为 4,如果您想知道当您在数组中循环 k 次时您将在哪个元素中,请:

int c = numbers[k % 4];

在您的情况下above c = 1;您无需循环。

相关文章: