FIFO 列表(移动元素)[C++]

发布于 2024-08-19 19:34:18 字数 816 浏览 9 评论 0原文

各位晚上好!

我正在尝试解决一个相当简单的问题,但是..好吧,看来我不能。 :)

我的想法是,我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

度的依靠╰つ 2024-08-26 19:34:18

我不确定我是否完全理解你的问题。但看起来您实际上想要旋转数组的内容。

将数组内容向左旋转 k 次。您可以执行以下操作:

  • 反转前 ​​K 个元素。
  • 反转剩余的 NK 元素。
  • 反转整个数组。

例子:

N = 5,K = 3,数组 = [1 2 3 4 5]

  • 步骤 1:反转前 3 个元素:
    [3 2 1 4 5]
  • 步骤2:将剩余的2反转
    元素:[3 2 1 5 4]
  • 步骤 3:反转整个数组:[4
    5 1 2 3]

C++ 函数执行相同操作:

void move (int a[100], int n, int k) {
        int t[100];
        int i,j;
        for (i=k-1,j=0; i>=0; i--,j++) t[j]=a[i];
        for (i=n-1; i>=k; i--,j++) t[j]=a[i];
        for (i=n-1,j=0; i>=0; i--,j++) a[j]=t[i];
}

在常量空间中执行此操作的更好方法是就地执行反转:

void arr_rev(int a[100], int start, int end) {
        int temp;

        for(;start<end;start++,end--) {
                temp = a[start];
                a[start] = a[end];
                a[end] = temp;
        }
}

void move2 (int a[100], int n, int k) {
        arr_rev(a,0,k-1);
        arr_rev(a,k,n-1);
        arr_rev(a,0,n-1);
}

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:

  • Reverse the first K elements.
  • Reverse the remaining N-K elements.
  • Reverse the entire array.

Example:

N = 5, K = 3, and array = [1 2 3 4 5]

  • step 1: reverse the first 3 elements:
    [3 2 1 4 5]
  • step 2: reverse the remaining 2
    elements: [3 2 1 5 4]
  • step 3: reverse the entire array: [4
    5 1 2 3]

C++ function to do the same:

void move (int a[100], int n, int k) {
        int t[100];
        int i,j;
        for (i=k-1,j=0; i>=0; i--,j++) t[j]=a[i];
        for (i=n-1; i>=k; i--,j++) t[j]=a[i];
        for (i=n-1,j=0; i>=0; i--,j++) a[j]=t[i];
}

A better way to do it in constant space is to do the reversal in-place:

void arr_rev(int a[100], int start, int end) {
        int temp;

        for(;start<end;start++,end--) {
                temp = a[start];
                a[start] = a[end];
                a[end] = temp;
        }
}

void move2 (int a[100], int n, int k) {
        arr_rev(a,0,k-1);
        arr_rev(a,k,n-1);
        arr_rev(a,0,n-1);
}
忆离笙 2024-08-26 19:34:18

由于您已将其标记为 C++,因此我假设这就是您正在使用的。在这种情况下,您几乎肯定应该使用 std::deque 而不是数组来存储数据。此外,队列通常有“前”和“后”,因此“左”并没有多大意义。

假设(只是为了论证)你想要的是从队列后面取出 k 个元素并将它们移动到前面,你可以这样做:

typedef std::deque<your_type> data;

void push_to_front(data &d, int k) { 
    if (k > d.size())
        return;
    for (int i=0; i<k; i++) {
        data::value_type v = d.pop_back();
        d.erase(d.back());
        d.push_front(v);
    }
}      

如果你想向另一个方向旋转,那几乎是交换前面和后面的角色的问题。

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:

typedef std::deque<your_type> data;

void push_to_front(data &d, int k) { 
    if (k > d.size())
        return;
    for (int i=0; i<k; i++) {
        data::value_type v = d.pop_back();
        d.erase(d.back());
        d.push_front(v);
    }
}      

If you want to rotate in the other direction, it's pretty much a matter of swapping the roles of the front and back.

左秋 2024-08-26 19:34:18

看起来你想要左旋转?那应该不是很困难。只需将前 k 元素出列,将剩余的 nk 元素向左移动(可能将它们放在临时数组的开头),然后添加第一个 < code>k 按顺序放在最后。

以这种方式修改代码可能如下所示:

void move (int a[100], unsigned n, unsigned k) {
        int t[100];
        unsigned i;
        for (i=k; i<=n-1; i++) t[i-k]=a[i];
        for (int x=0; x<=k-1; x++) t[i++-k]=a[x];
}

由于这仍然很难看,您可以重写它以使逻辑更清晰,但我将把它作为练习。

这还假设您不被允许使用 STL 数据结构;如果情况并非如此,请参阅杰里·科芬的回答。

It looks like you want a left-rotate? That shouldn't be very difficult. Just dequeue the first k elements, shift the remaining n-k elements to the left (possibly by putting them at the beginning of a temporary array), and then add in the first k at the end in order.

To modify your code in this manner might look like this:

void move (int a[100], unsigned n, unsigned k) {
        int t[100];
        unsigned i;
        for (i=k; i<=n-1; i++) t[i-k]=a[i];
        for (int x=0; x<=k-1; x++) t[i++-k]=a[x];
}

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.

醉殇 2024-08-26 19:34:18

如果您的意思是“将第 k 个元素移动到队列的前面”,那么这是一种方法:

void move( int *a, unsigned n, unsigned k )
{ 
    int t; // To store the temporary for the k'th element 

    t = a[ k ];

    // Shift all the elements to the right by one.
    for( unsigned i = k; i > 0; --i )
        a[ i ] = a[ i - 1 ];

    // Put the k'th element at the left of the queue.
    a[ 0 ] = t;
}

If you mean "move the kth element to the front of the queue", then this is one way to do it:

void move( int *a, unsigned n, unsigned k )
{ 
    int t; // To store the temporary for the k'th element 

    t = a[ k ];

    // Shift all the elements to the right by one.
    for( unsigned i = k; i > 0; --i )
        a[ i ] = a[ i - 1 ];

    // Put the k'th element at the left of the queue.
    a[ 0 ] = t;
}
就是爱搞怪 2024-08-26 19:34:18
#include <iostream>
#include <list>
template <typename T>
void Rotate(std::list<T>& list, int k){
    for(int i=0; i<k; ++i){
        T tmp(list.back());
        list.pop_back();
        list.push_front(tmp);
    }
}
int main(){
    std::list<int> ints;
    ints.push_back(1); ints.push_back(2);
    ints.push_back(3); ints.push_back(4);
    Rotate(ints,2);
    for(std::list<int>::const_iterator i = ints.begin(); i!=ints.end(); ++i)
        std::cout << *i << std::endl;
    return 0;
}

将输出:

3
4
1
2
#include <iostream>
#include <list>
template <typename T>
void Rotate(std::list<T>& list, int k){
    for(int i=0; i<k; ++i){
        T tmp(list.back());
        list.pop_back();
        list.push_front(tmp);
    }
}
int main(){
    std::list<int> ints;
    ints.push_back(1); ints.push_back(2);
    ints.push_back(3); ints.push_back(4);
    Rotate(ints,2);
    for(std::list<int>::const_iterator i = ints.begin(); i!=ints.end(); ++i)
        std::cout << *i << std::endl;
    return 0;
}

Will output:

3
4
1
2
旧人 2024-08-26 19:34:18

这个问题很老了,但现在看来最简单的解决方案是依赖 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文