我怎样才能实现堆栈?

时间:2022-05-27 08:08:54

How can I create a program to convert binary to decimal using a stack in C#?

如何创建一个程序,使用C#中的堆栈将二进制转换为十进制?

1 个解决方案

#1


4  

Here is a hint, this snippet converts a decimal integer to binary using a Stack, you just have to invert the process :-P

这是一个提示,这个片段使用Stack将十进制整数转换为二进制,你只需要反转过程:-P

        int num = 50;
        Stack<int> stack = new Stack<int>();
        while (num != 0)
        {
            stack.Push(num % 2);
            num /= 2;
        }

        while (stack.Count > 0)
        {
            Console.Write(stack.Pop());
        }

#1


4  

Here is a hint, this snippet converts a decimal integer to binary using a Stack, you just have to invert the process :-P

这是一个提示,这个片段使用Stack将十进制整数转换为二进制,你只需要反转过程:-P

        int num = 50;
        Stack<int> stack = new Stack<int>();
        while (num != 0)
        {
            stack.Push(num % 2);
            num /= 2;
        }

        while (stack.Count > 0)
        {
            Console.Write(stack.Pop());
        }