反转数字的子序列
Reversing a subsequence of numbers
我正在尝试使用 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;
}
k 应初始化为 -
int k = i + rand()%(n-i);
我正在尝试使用 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;
}
k 应初始化为 -
int k = i + rand()%(n-i);