#include<iostream>
#define maxsize 100
void seqlistR1(int seqlist[], int length)
{
int left = 0;
int right = length - 1;
for (left, right; left < right; left++, right--)
{
int temp = seqlist[left];
seqlist[left] = seqlist[right];
seqlist[right] = temp;
}
for (int a=0,int b=length-4; a <b; a++, b--)
{
int temp = seqlist[a];
seqlist[a] = seqlist[b];
seqlist[b] = temp;
}
}
void Printf(int seqlist[],int length)
{
for (int i = 0; i < length; i++)
{
std::cout << seqlist[i]<<" ";
}
}
int main()
{
int seqlist[maxsize] = { 1,2,3,4,5,6,7,8 };
int length = 8;
seqlistR1(seqlist, length);
Printf(seqlist,length);
return 0;
}
我想用上面这个代码去实现:将1 2 3 4 5 6 7 8,改为4 5 6 7 8 3 2 1
我在seqlistR1函数里面用了两个for循环,可是代码运行有问题。
报的错为
而当我把函数中的第二个循环单独写成一个函数时,代码且可以运行了。
另一个代码如下:
#include<iostream>
#define maxsize 100
void seqlistR1(int seqlist[], int length)
{
int left = 0;
int right = length - 1;
for (left, right; left < right; left++, right--)
{
int temp = seqlist[left];
seqlist[left] = seqlist[right];
seqlist[right] = temp;
}
}
void seqlistR2(int seqlist[], int length)
{
int left = 0;
int right = length - 4;
for (left, right; left < right; left++, right--)
{
int temp = seqlist[left];
seqlist[left] = seqlist[right];
seqlist[right] = temp;
}
}
void Printf(int seqlist[], int length)
{
for (int i = 0; i < length; i++)
{
std::cout << seqlist[i] << " ";
}
}
int main()
{
int seqlist[maxsize] = { 1,2,3,4,5,6,7,8 };
int length = 8;
seqlistR1(seqlist, length);
seqlistR2(seqlist, length);
Printf(seqlist, length);
return 0;
}
我想知道为什么会出现这种情况?