FIFO 列表(移动元素)[C++]
各位晚上好!
我正在尝试解决一个相当简单的问题,但是..好吧,看来我不能。 :)
我的想法是,我有一个包含 n 个元素的 FIFO 列表(FIFO 队列),并给定一个值 k(k < n)。我的小程序必须将元素向左移动 k 个元素。 (例如,对于 n=4、k=3、a[]=(1, 2, 3, 4),结果为 4 1 2 3)。
但好吧,我离这个还差得很远。
这就是我到目前为止所写的:
#include <iostream>
using namespace std;
void move (int a[100], unsigned n, unsigned k) {
int t[100];
unsigned i;
for (i=0; i<=n-1; i++) t[i]=a[i];
for (i=0; i<=k-1; i++) a[i]=a[i+k-1];
for (i=k; i<=n-1; i++) a[i]=t[i+1];
}
int main () {
int a[100];
unsigned k, n, i;
cout<<"n; k= "; cin>>n>>k;
for (i=0; i<=n-1; i++) cin>>a[i];
move (a, n, k);
for (i=0; i<=n-1; i++) cout<<a[i]<<" ";
}
任何帮助将不胜感激。先感谢您。
Good evening, people!
I'm trying to solve a rather simple problem, but.. well, it seems that I can't. :)
The idea is that I have a FIFO list (FIFO queue) with n elements and it's given a value, k (k < n). My little program has to move the elements to the left with k elements. (e.g. for n=4, k=3, a[]=(1, 2, 3, 4), the result is 4 1 2 3).
But well, I get nowhere near that.
This is what I've written so far:
#include <iostream>
using namespace std;
void move (int a[100], unsigned n, unsigned k) {
int t[100];
unsigned i;
for (i=0; i<=n-1; i++) t[i]=a[i];
for (i=0; i<=k-1; i++) a[i]=a[i+k-1];
for (i=k; i<=n-1; i++) a[i]=t[i+1];
}
int main () {
int a[100];
unsigned k, n, i;
cout<<"n; k= "; cin>>n>>k;
for (i=0; i<=n-1; i++) cin>>a[i];
move (a, n, k);
for (i=0; i<=n-1; i++) cout<<a[i]<<" ";
}
Any help would be greatly appreciated. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不确定我是否完全理解你的问题。但看起来您实际上想要旋转数组的内容。
将数组内容向左旋转 k 次。您可以执行以下操作:
例子:
[3 2 1 4 5]
元素:[3 2 1 5 4]
5 1 2 3]
C++ 函数执行相同操作:
在常量空间中执行此操作的更好方法是就地执行反转:
I'm not sure if I've understood your question completely. But looks like you effectively want to rotate the contents of the array.
To rotate the array contents to the left k times. You can do the following:
Example:
[3 2 1 4 5]
elements: [3 2 1 5 4]
5 1 2 3]
C++ function to do the same:
A better way to do it in constant space is to do the reversal in-place:
由于您已将其标记为 C++,因此我假设这就是您正在使用的。在这种情况下,您几乎肯定应该使用 std::deque 而不是数组来存储数据。此外,队列通常有“前”和“后”,因此“左”并没有多大意义。
假设(只是为了论证)你想要的是从队列后面取出 k 个元素并将它们移动到前面,你可以这样做:
如果你想向另一个方向旋转,那几乎是交换前面和后面的角色的问题。
Since you've tagged this as C++, I'll assume that's what you're using. In that case, you should almost certainly be using an
std::deque
instead of an array for storing the data. In addition, a queue normally has a "front" and a "back", so "left" doesn't really mean much.Assuming (just for the sake of argument) that what you want is to take k elements from the back of the queue and move them to the front, you could do something like:
If you want to rotate in the other direction, it's pretty much a matter of swapping the roles of the front and back.
看起来你想要左旋转?那应该不是很困难。只需将前
k
元素出列,将剩余的nk
元素向左移动(可能将它们放在临时数组的开头),然后添加第一个 < code>k 按顺序放在最后。以这种方式修改代码可能如下所示:
由于这仍然很难看,您可以重写它以使逻辑更清晰,但我将把它作为练习。
这还假设您不被允许使用 STL 数据结构;如果情况并非如此,请参阅杰里·科芬的回答。
It looks like you want a left-rotate? That shouldn't be very difficult. Just dequeue the first
k
elements, shift the remainingn-k
elements to the left (possibly by putting them at the beginning of a temporary array), and then add in the firstk
at the end in order.To modify your code in this manner might look like this:
And since this is still ugly, you might rewrite it to make the logic more clear, but I will leave that as an exercise.
This also assumes you are not allowed to use STL data structures; if that is not the case, see Jerry Coffin's answer.
如果您的意思是“将第 k 个元素移动到队列的前面”,那么这是一种方法:
If you mean "move the kth element to the front of the queue", then this is one way to do it:
将输出:
Will output:
这个问题很老了,但现在看来最简单的解决方案是依赖 std::旋转。复杂度是线性的,但取决于第一个和最后一个之间的距离。因此,如果需要旋转 1 个元素,复杂度为 O(1),对于 3 个元素,复杂度为 O(3)。
The question is old, but nowadays it seems that the easiest solution is to rely on std::rotate. The complexity is linear but depends on the distance between first and last. So if you need to rotate 1 element complexity is O(1), for 3 it is O(3).