Permute array to order of selection in Josephus problem

你说的曾经没有我的故事 提交于 2021-01-07 01:26:25

问题


How can we write code to permute an array to the order of selection in the Josephus problem?

Given a sequence of items (such as an array of char *) and a skip distance d, we skip d items from the start, take that item to be the first in the output sequence, then skip d further items, take the resulting item to be the second in the output, and continue in this way. When we reach the end of the sequence, we wrap to the beginning, continuing to count. Only items remaining in the array are counted for the skip distance; removed items no longer count.

For example, given:

char *arr[] = {"first", "second", "third", "fourth"};
skip=6;

we would count 6 items: "first", "second", "third", "fourth", (wrapping around) "first", "second". So "second" becomes the first output item. Then we count six again: "third", "fourth", "first", "third", "fourth", "first", and we take "first" as the next output element. The we count six again: "third", "fourth", "third", "fourth", "third", "fourth", and we take "fourth" as the third output element. Counting six again repeats "third" six times, and "third" becomes the last output element.

So the result is "second", "first", "fourth", "third".

So far I have managed to write this:

void rroulette(char* arr,int skip)
{
    if(skip>LENGTH)
    {
        skip=skip%LENGTH;
    }
    for(int i=0;i<=LENGTH;i++)//allowing index to pass the array len limit so it'll go back to 0
    {
        int counter=skip;
        if(i==LENGTH)//array index is at the (final+1) spot so it goes back to 0
            i=0;
    }
}

来源:https://stackoverflow.com/questions/65341163/permute-array-to-order-of-selection-in-josephus-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!