需要帮助安排非零和零顺序 [英] need help with arranging non - zero and zero in sequence

查看:85
本文介绍了需要帮助安排非零和零顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

序列是[5,0,1,4,3,0,0,6,1,0]。现在必须使用队列和堆栈按顺序排列顺序[5,1,4,3,6,1,0,0,0,0]。

解决方案

大!如果你考虑到它,这很简单。只需查看第一个数字并考虑您需要做什么,然后查看下一个数字并执行相同的操作。冲洗并重复,直到没有更多数字。



不,我们不打算为你做功课。所以,如果您正在寻找某人为您编写代码,那么您来错了地方。


这个伪代码怎么样(如果你坚持使用这两个容器)

重复给定序列中的每个项目
如果项目等于0,则将项目推送到堆栈
将项目排队到队列,否则
结束重复
从队列中取出所有项目
弹出堆栈中的所有项目



干杯

Andi

嗨好像冒泡排序的例子,但条件差别不大。请尝试下面的代码

  int  [] a =  new   int  [] { 5  0  1  4  3  0  0  6  1  0 }; 
int temp;
for int pass = 1 ;传递< = a.Length - 2 ;传递++)
{
for int i = 0 ; i < = a.Length - 2 ; i ++)
{
if (a [i] == 0
{
temp = a [i + 1 ];
a [i + 1 ] = a [i];
a [i] = temp;
}

}

}







string str =;

for(int i = 0; i< a.Length; i ++)// for循环打印已排序的元素

{

str + = a [i] +,;

}



你可以能够找到aaray的排序列表。


Sequence is [5,0,1,4,3,0,0,6,1,0]. Now have to use queue and stack to arrange the sequence in the order [ 5,1,4,3,6,1,0,0,0,0].

解决方案

Great! It''s very simple to do if you put any thought into it. Just look at the first number and think about what you need to do with it, then look at the next one and do the same. Rinse and repeat until there are no more numbers.

No, we''re not going to do your homework for you. So, if you were looking for someone to write your code for you, you''ve come to wrong place.


How about this pseudo code (if you insist using both containers)

repeat for each item in the given sequence
   push item to stack if item equals 0
   enqueue item to queue otherwise
end repeat
dequeue all items from the queue
pop all items from the stack


Cheers
Andi


Hi It seems like bubble sort example but the condition is little different.Try bellow code

int[] a = new int[] { 5, 0, 1, 4, 3, 0, 0, 6, 1, 0 };
int  temp;
            for (int pass = 1; pass <= a.Length - 2; pass++)
            {
                for (int i = 0; i <= a.Length - 2; i++)
                {
                    if (a[i]==0 )
                    {
                        temp = a[i + 1];
                        a[i + 1] = a[i];
                        a[i] = temp;
                    }

                }

            }




string str = "";
for (int i = 0; i < a.Length; i++) //for loop to print sorted elements
{
str += a[i] + ",";
}

in str you can able to find the sorted list of aaray.


这篇关于需要帮助安排非零和零顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆