아래 예제와 같이 고양이를 출력하시오.

없음.
고양이를 출력한다.

:: 성공

: 개선점 : 없음

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Print01
{
    class Program
    {
        static void Main(string[] args)
        {
            // :: Print Cat
            Console.WriteLine("\\    /\\");
            Console.WriteLine(" )  ( ')");
            Console.WriteLine("(  /  )");
            Console.WriteLine(" \\(__)|");

            // :: End Program
            //Console.ReadKey();
        }
    }
}

'C#' 카테고리의 다른 글

백준 1008 : A/B  (0) 2020.11.12
백준 10172 : 개 // 성공  (0) 2020.11.07
백준 1874 : 스택 수열 // 실패  (0) 2020.11.05
백준 10828 : 스택 // 실패  (0) 2020.11.04
백준 4949: 균형잡힌 세상 // 성공  (0) 2020.11.01
블로그 이미지

RIsN

,

스택 (stack)은 기본적인 자료구조 중 하나로, 컴퓨터 프로그램을 작성할 때 자주 이용되는 개념이다. 스택은 자료를 넣는 (push) 입구와 자료를 뽑는 (pop) 입구가 같아 제일 나중에 들어간 자료가 제일 먼저 나오는 (LIFO, Last in First out) 특성을 가지고 있다.

1부터 n까지의 수를 스택에 넣었다가 뽑아 늘어놓음으로써, 하나의 수열을 만들 수 있다. 이때, 스택에 push하는 순서는 반드시 오름차순을 지키도록 한다고 하자. 임의의 수열이 주어졌을 때 스택을 이용해 그 수열을 만들 수 있는지 없는지, 있다면 어떤 순서로 push와 pop 연산을 수행해야 하는지를 알아낼 수 있다. 이를 계산하는 프로그램을 작성하라.

첫 줄에 n (1 ≤ n ≤ 100,000)이 주어진다. 둘째 줄부터 n개의 줄에는 수열을 이루는 1이상 n이하의 정수가 하나씩 순서대로 주어진다. 
물론 같은 정수가 두 번 나오는 일은 없다.

:: 실패 이유 : 컴파일 에러

: 개선하지 않는 이유 : 뭐가 문제인지 모르겠다.

: 프로그램 자체는 문제가 말하는 대로 흘러가는데, 정작 컴파일 에러가 계속 뜬다.

: C#은 대체로 전부 문제가 많은 게 아닐까?

: 질문 되면 질문을 올려보고, 안되면 다른 언어로 도전 고려

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Stack05
{
    class Program
    {
        static void Main(string[] args)
        {
            // :: Initialise
            Stack<int> stack = new Stack<int>();
            List<bool> processChecker = new List<bool>(); 
            Queue<int> input = new Queue<int>();

            // :: Input Command
            int commandSize = Int32.Parse(Console.ReadLine()); // :: CommandSize
            for(int i = 0; i < commandSize; i++)
            {
                // :: Save Input in Queue
                input.Enqueue(Int32.Parse(Console.ReadLine()));
            }

            // :: Start Program
            for (int i = 1; i <= commandSize; i++)
            {
                // :: Push
                stack.Push(i);
                processChecker.Add(true);

                // :: If stack has remains & current stack last value same as input first value;
                while (stack.Count != 0 && stack.Peek() == input.Peek())
                {
                    // :: Pop & input Queue dequeue
                    stack.Pop();
                    processChecker.Add(false);
                    input.Dequeue();
                }
            }
            
            // :: If stack has something : error
            if(stack.Count() != 0)
            {
                Console.WriteLine("NO");
            }
            // :: else print process
            else
            {
                foreach (var itm in processChecker)
                {
                    Console.WriteLine("{0}", itm == true ? "+" : "-");
                }
            }

            // :: End Program
            // Console.ReadKey();
        }
    }
}

'C#' 카테고리의 다른 글

백준 10172 : 개 // 성공  (0) 2020.11.07
백준 10171 : 고양이 // 성공  (0) 2020.11.06
백준 10828 : 스택 // 실패  (0) 2020.11.04
백준 4949: 균형잡힌 세상 // 성공  (0) 2020.11.01
백준 9012 : 괄호 // 성공  (0) 2020.10.31
블로그 이미지

RIsN

,

백준 10828 : 스택 // 실패

C# 2020. 11. 4. 23:39

정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.

명령은 총 다섯 가지이다.

  • push X: 정수 X를 스택에 넣는 연산이다.
  • pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
  • size: 스택에 들어있는 정수의 개수를 출력한다.
  • empty: 스택이 비어있으면 1, 아니면 0을 출력한다.
  • top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 
둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 
문제에 나와있지 않은 명령이 주어지는 경우는 없다.

:: 실패 이유 : 시간초과

: 개선하지 않는 이유 : 뭐가 문제인지 모르겠다.

: 문제를 못 맞춰서 다른 사람 코드도 볼 수 없고, C# 관련 구글 검색해서 갖다 붙여도 시간초과로 실패함

: 10개 안 풀어서 질문을 올릴 수도 없음, 10개 이상 풀고나서 다음에 확인할 것.

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Stack01
{
    class Program
    {
        static void Main(string[] args)
        {
            // :: Initialise
            Stack stack = new Stack();

            // :: Check Command Size
            string input = Console.ReadLine();
            int commandSize = 0;
            try
            {
                commandSize = Int32.Parse(input);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            List<string> inputList = new List<string>();
            // :: Loop Command with Size
            for (int i = 0; i < commandSize; i++)
            {
                string temp = Console.ReadLine();
                inputList.Add(temp);
            }

            foreach (var itm in inputList)
            {
                CheckCommand(itm, stack);
            }

            // :: End Program
            // Console.ReadKey();
        }

        // :: Check Command with String
        public static void CheckCommand(string input, Stack stack)
        {
            string[] command = input.Split(' ');
            switch (command[0])
            {
                case "push":
                    try
                    {
                        int temp = Int32.Parse(command[1]);
                        stack.Push(temp);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    break;
                case "pop":
                    Console.WriteLine(stack.Pop());
                    break;
                case "size":
                    Console.WriteLine(stack.GetSize());
                    break;
                case "empty":
                    Console.WriteLine(stack.CheckEmpty());
                    break;
                case "top":
                    Console.WriteLine(stack.GetTop());
                    break;
            }
        }
    }

    public class Stack
    {
        private List<int> data;
        private int top;

        public Stack()
        {
            data = new List<int>();
            top = -1;
        }

        public void Push(int input)
        {
            data.Add(input);
            top += 1;
        }

        public int Pop()
        {
            if (top < 0)
            {
                return -1;
            }

            int temp = data[top];
            data.RemoveAt(top);
            top -= 1;

            return temp;
        }

        public int GetTop()
        {
            if (top < 0)
                return top;

            return data[top];
        }

        public int GetSize()
        {
            return data.Count();
        }

        public int CheckEmpty()
        {
            return data.Count() > 0 ? 0 : 1;
        }
    }
}

'C#' 카테고리의 다른 글

백준 10171 : 고양이 // 성공  (0) 2020.11.06
백준 1874 : 스택 수열 // 실패  (0) 2020.11.05
백준 4949: 균형잡힌 세상 // 성공  (0) 2020.11.01
백준 9012 : 괄호 // 성공  (0) 2020.10.31
백준 10773 : 제로 // 성공  (0) 2020.10.30
블로그 이미지

RIsN

,