反转数字的子序列

时间:2015-03-09 18:22:10

标签: c++ reverse

我试图使用reverse函数在原始数组中反转子序列来填充int类型的新数组。但是,我只是得到了相同的数字序列。有人可以指出我的错误吗?先感谢您。

double swap4()
{
    int i = rand()%(n-1);     //choose radom sub seq. start point.
    int k = rand()%(n-i);      //choose radom sub seq. end point.
    int *p;
    int *q;
    p = &Path[i];
    q = &Path[k];
    reverse(p,q);               //reverse the sequence.

    for(int j=0;j<n;j++)
    {
        neworder[j] = Path[j];          //create a new array to store the new sequnece.
    }

    reverse(p,q);                           //reverse again to regain the origional.

    cout<<" the reversed path is "<< endl;

    for(int i=0;i<n;i++)                        //print out new sequence.
    {
        cout << neworder[i]<< " ";
    }

    cout<< neworder[0] <<")"<<endl;
}

1 个答案:

答案 0 :(得分:4)

k应初始化为 -

int k = i + rand()%(n-i);