SMALL

https://www.acmicpc.net/problem/17827

 

17827번: 달팽이 리스트

첫째 줄에 노드의 개수 N(2 ≤ N ≤ 200,000), 질문의 횟수 M(1 ≤ M ≤ 200,000), N번 노드가 가리키는 노드의 번호 V(2 ≤ V ≤ N)가 공백으로 구분되어 주어진다. 둘째 줄에 N개의 정수 C1, C2, …, CN이 공백

www.acmicpc.net


  • 문제


  • 문제풀이

만약 입력받는 수 K가 N보다 작다면 K번째 배열을 출력하고 만약 N보다 큰 수가 나온다면 V전까지를 빼고 나머지를 구한뒤 다시 V를 넣고 배열을 출력해준다.


  • 코드 1
using System.Collections.Immutable;
using System.Text;
using System.Collections.Generic;
using System;
using System.Collections;
using System.Formats.Asn1;

namespace YONGYONG2
{
    internal class Program
    {
        public static Stack<char> stack = new Stack<char>();

        static void Main(string[] args)
        {

            StringBuilder sb = new StringBuilder();
            string[] input = Console.ReadLine().Split();
            int n = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            int v = int.Parse(input[2]);

            string[] input1 = Console.ReadLine().Split();
            int[] snail=new int[n];
            for(int i=0;i<n;i++)
            {
                snail[i]= int.Parse(input1[i]);
            }
            for(int i = 0; i < m; i++)
            {
                int q=int.Parse(Console.ReadLine());
                if (q < n)
                {
                    sb.Append(snail[q]+"\n");
                }
                
                else
                {
                    int snaillength = n - v + 1;
                    q -= v - 1;
                    sb.Append(snail[q % snaillength + v - 1] + "\n");
                }
                
            }
            Console.WriteLine(sb.ToString());
        }   
    }
}

  • 후기

태블릿에 노가다로 그려보면서 풀었더니 손이 아프다

LIST

'백준 > C#' 카테고리의 다른 글

[C#]백준 1874번: 스택 수열  (0) 2023.02.01
[C#]백준 2493번: 탑  (0) 2023.02.01
[C#]백준 23253번: 자료구조는 정말 최고야  (0) 2023.01.28
[C#]백준 17413번: 단어 뒤집기 2  (0) 2023.01.28
[C#]백준 15828번: Router  (2) 2023.01.28
SMALL

https://www.acmicpc.net/problem/23253

 

23253번: 자료구조는 정말 최고야

위 그림처럼 책이 쌓여 있으므로, 첫 번째 더미 - 두 번째 더미 - 첫 번째 더미 - 두 번째 더미 순으로 꺼내면 책 번호순으로 나열할 수 있다.

www.acmicpc.net


  • 문제


  • 문제풀이

처음에는 스택 안에 스택을 만들어보고자 했으나 어떻게 해야할지 몰랐다. 하지만 책을 순서대로 꺼내기 위해서는 아래의 책이 위의 책보다 작은 숫자임을 알게 되었다. 교과서의 최대 개수가 200000이기 때문에 test를 200001로 설정하여 높은 수가 낮은 수 보다 먼저 입력받으면 false로 만들어 yes와 no를 출력하게 하였다.


  • 코드 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>



int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    bool result = true;
    for (int i = 0; i < m; i++) {
        int c;
        scanf("%d", &c);
        int book[200001];
        int test1 = 200001;
            for (int k = 0; k < c; k++) {
                scanf("%d", &book[k]);
                if (book[k]>test1) {
                    result = false;
                }
                test1 = book[k];
        }
    }
    if (result) {
        printf("Yes");
    }
    else {
        printf("No");
    }
    
}

  • 후기

너무 스택으로만 풀려고 하다보니 간단한 문제임을 놓친것 같다.

LIST
SMALL

https://www.acmicpc.net/problem/23253

 

23253번: 자료구조는 정말 최고야

위 그림처럼 책이 쌓여 있으므로, 첫 번째 더미 - 두 번째 더미 - 첫 번째 더미 - 두 번째 더미 순으로 꺼내면 책 번호순으로 나열할 수 있다.

www.acmicpc.net


  • 문제


  • 문제풀이

처음에는 스택 안에 스택을 만들어보고자 했으나 어떻게 해야할지 몰랐다. 하지만 책을 순서대로 꺼내기 위해서는 아래의 책이 위의 책보다 작은 숫자임을 알게 되었다. 교과서의 최대 개수가 200000이기 때문에 test를 200001로 설정하여 높은 수가 낮은 수 보다 먼저 입력받으면 false로 만들어 yes와 no를 출력하게 하였다.


  • 코드 1(stack 사용)
namespace yongyong2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] arr = Console.ReadLine().Split();
            bool flag = true;
            Stack<int> stack = new Stack<int>();
            int N = int.Parse(arr[0]);
            int M = int.Parse(arr[1]);
            for (int i = 0; i < M; i++)
            {
                int C = int.Parse(Console.ReadLine());
                string[] input = Console.ReadLine().Split();
                int test = 200001;
                for (int j = 0; j < C; j++)
                {
                    
                    stack.Push(int.Parse(input[j]));
                    
                    if (stack.Peek() > test)
                    {
                        flag = false;
                    }
                    test = stack.Pop();
                }
            }
            if (flag)
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
        } 
    }
}

코드 2 

using System.Collections.Immutable;
using System.Text;
using System.Collections.Generic;
using System;
using System.Collections;
using System.Formats.Asn1;

namespace yongyong2
{
    internal class Program
    {


        static void Main(string[] args)
        {

            StringBuilder sb = new StringBuilder();
            string[] input = Console.ReadLine().Split();
            int N = int.Parse(input[0]);
            int M = int.Parse(input[1]);
            bool result = true;
            for (int i = 0; i < M; i++)
            {
                int c = int.Parse(Console.ReadLine());
                string[] s = Console.ReadLine().Split();
                int test = 200001;
                for (int j = 0; j < c; j++)
                {
                    if (int.Parse(s[j]) > test)
                    {
                        result = false;
                    }
                    test = int.Parse(s[j]);
                }
            }
            if (result)
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }
}

  • 후기

너무 스택으로만 풀려고 하다보니 간단한 문제임을 놓친것 같다.

LIST

'백준 > C#' 카테고리의 다른 글

[C#]백준 2493번: 탑  (0) 2023.02.01
[C#]백준 17827번: 달팽이 리스트  (0) 2023.01.28
[C#]백준 17413번: 단어 뒤집기 2  (0) 2023.01.28
[C#]백준 15828번: Router  (2) 2023.01.28
[C#]백준 1966번: 프린터 큐  (0) 2023.01.28
SMALL

https://www.acmicpc.net/problem/2869

 

2869번: 달팽이는 올라가고 싶다

첫째 줄에 세 정수 A, B, V가 공백으로 구분되어서 주어진다. (1 ≤ B < A ≤ V ≤ 1,000,000,000)

www.acmicpc.net


  • 문제


  • 문제풀이

처음에는 while로 접근하려고 했으나 시간초과를 보고 당황했지만 식을 만들어 풀면 해결이 된다.

#include<stdio.h>

int main() {
    int A;
    int B;
    int V;
    int count=0;
    scanf("%d %d %d",&A,&B,&V);
    while(V>0){
        V-=A;
        if(V==0){
            count++;
            break;
        }
        V+=B;
        count++;
    }
    printf("%d",count);
}

위의 코드는 시간 초과가 걸리니 (V-B-1)/(A-B)+1이라는 식을 만들어 풀게된다. 


  • 코드 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> 
#include <iostream>

int main() {
    int A;
    int B;
    int V;
    int count = 0;
    scanf("%d %d %d", &A, &B, &V);
    int real = (V - B - 1) / (A - B) + 1;
    printf("%d", real);
}

  • 후기

처음에 굉장히 쉽다고 생각했지만 실패하고 수학적으로 풀어야함을 알게 되었다.

LIST
SMALL

https://www.acmicpc.net/problem/17413

 

17413번: 단어 뒤집기 2

문자열 S가 주어졌을 때, 이 문자열에서 단어만 뒤집으려고 한다. 먼저, 문자열 S는 아래와과 같은 규칙을 지킨다. 알파벳 소문자('a'-'z'), 숫자('0'-'9'), 공백(' '), 특수 문자('<', '>')로만 이루어져

www.acmicpc.net


  • 문제


  • 문제풀이

스택과 불을 사용한다. 문장 전체를 입력을 받은 뒤에 글자단위로 쪼개어 '<'를 만난다면 태그를 true로 바꿔주고 '>'가 나올때까지 출력을 해주고 공백을 제외하고 나머지 글자들은 스택에 넣은 뒤 '<'를 만나거나 공백을 만나게 된다면 스택에서 pop하여 순서가 반대로 출력되도록 한다.


  • 코드 1
using System.Collections.Immutable;
using System.Text;
using System.Collections.Generic;
using System;
using System.Collections;
using System.Formats.Asn1;

namespace YONGYONG2
{
    internal class Program
    {
        public static Stack<char> stack = new Stack<char>();
        
        static void Main(string[] args)
        {
            
            StringBuilder sb = new StringBuilder();
            char charAt;
            bool tag = false;
            string word = Console.ReadLine();
            for(int i = 0; i < word.Length; i++)
            {
                charAt = word[i];
                if (charAt == '<')
                {
                    tag= true;
                    while(stack.Any())
                    {
                        sb.Append(stack.Pop());
                    }
                    sb.Append(charAt);
                }
                else if (charAt == '>')
                {
                    tag = false;
                    sb.Append(charAt);
                }
                else if (tag == true)
                {
                    sb.Append(charAt);
                }
                else if (tag == true)
                {
                    sb.Append(charAt);
                }
                else if (tag == false)
                {
                    if(charAt==' ')
                    {
                        while (stack.Any())
                        {
                            sb.Append(stack.Pop());
                        }
                        sb.Append(charAt);
                    }
                    else
                    {
                        stack.Push(charAt);
                    }
                }
            }
            while(stack.Count> 0)
            {
                sb.Append(stack.Pop());
            }
            
            
           
            
            Console.WriteLine(sb.ToString());
            
        }
    }
}

  • 후기

문장 전체를 받고 글자 단위로 쪼개는 방법을 처음 알게 되었다. string으로 문장을 받고 문장의 길이만큼 반복문을 돌려 i번째 글자를 확인하는 방법을 알게 되었다.

LIST
SMALL

https://www.acmicpc.net/problem/15828

 

15828번: Router

인터넷을 사용하기 위해서는 컴퓨터에 인터넷 회선을 연결하거나 Wi-Fi를 연결해야 한다. 이렇게 연결된 네트워크를 통해 컴퓨터에는 통신이 가능하다. 마음에 드는 노래나 동영상이 있는 곳에

www.acmicpc.net


  • 문제


  • 문제풀이

문제이해가 조금 걸렸다. 큐의 크기를 N으로 제한하고 정보(상수)가 들어온다면 라우터에 넣어주고 0이면 라우터가 정보를 처리하여 큐에서 빼주는 것이다.

 

큐를 생성하고 -1을 받는다면 break를 해주고 0보다 크다면 큐에 넣고 0과 같으면 큐에서 빼준다. C#의 경우 시간초과가 나타날 수 있기 때문에 반드시 StringBuilder을 사용하여 출력을 해야한다. 사용하지 않으면 시간초과로 50점이 나오게 된다.


  • 코드 1
using System.Text;

namespace Yongyong2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            double N = int.Parse(Console.ReadLine());
            int num = 0;
            Queue<int> queue = new Queue<int>();
            while (true)
            {
                num = int.Parse(Console.ReadLine());

                if (num == -1)
                {
                    break;
                }
                if (num > 0)
                {
                    if (queue.Count() < N)
                    {
                        queue.Enqueue(num);
                    }
                }
                if (num == 0)
                {
                    queue.Dequeue();
                }
            }
            if (queue.Count() == 0)
            {
                sb.Append("empty");
            }
            else
            {
                while (queue.Any())
                {
                    sb.Append(queue.Peek() + " ");
                    queue.Dequeue();
                }
            }
            Console.WriteLine(sb.ToString());
        }
    }
}

  • 후기

문제 이해가 코드쓰는 것 보다 더 오래걸렸다.

LIST
SMALL

https://www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net


  • 문제


  • 문제풀이

Queue를 두개를 생성해주고 하나는 입력을 받아주고 하나는 입력을 정렬한뒤 넣어준다. 자연스럽게 두번째 큐에는 최댓값부터 순서대로 큐가 생성이 된다. 즉 중요도의 순서대로 큐가 들어가기 때문에 첫번째와 두번째 큐의 수를 비교해가면서 같으면 순서를 출력해주는 방식을 사용한다.


  • 코드 1
using System.Collections.Immutable;
using System.Text;
using System.Collections.Generic;
using System;
using System.Collections;

namespace YongYong2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int N=int.Parse(Console.ReadLine());
            StringBuilder sb = new StringBuilder();
           
            
            int count;
            for(int i=0; i < N;i++)
            {
                string[] arr = Console.ReadLine().Split();
                int M = int.Parse(arr[0]);
                int num = int.Parse(arr[1]);
                int[] arr_input = (Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse));
                Queue<int> queue = new Queue<int>(arr_input);
                Array.Sort(arr_input);
                Array.Reverse(arr_input);
                Queue<int> queue_max = new Queue<int>(arr_input);
                
                count = 1;
                while (true)
                {
                    if (queue.Peek() == queue_max.Peek())
                    {
                        
                        if (num == 0)
                        {
                            sb.Append(count+"\n");
                            break;
                        }
                        queue.Dequeue();
                        queue_max.Dequeue();
                        count++;
                        num--;
                    }
                    else
                    {
                        queue.Enqueue(queue.Dequeue());
                        if (num == 0)
                        {
                            num = queue.Count()-1;
                            
                        }
                        else
                        {
                            num--;
                        }
                    }
                }
                
            }
            Console.WriteLine(sb);

        }
    }
}

  • 후기

큐를 두개를 비교해서 카드돌리기 처럼 Enqueue와 Dequeue를 사용하였다.

LIST

'백준 > C#' 카테고리의 다른 글

[C#]백준 17413번: 단어 뒤집기 2  (0) 2023.01.28
[C#]백준 15828번: Router  (2) 2023.01.28
[C#]백준 11866번: 요세푸스 문제 0  (0) 2023.01.28
[C#]백준 2164번: 카드2  (0) 2023.01.28
[C#]백준 9012번: 괄호  (0) 2023.01.14
SMALL

https://www.acmicpc.net/problem/11866

 

11866번: 요세푸스 문제 0

첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)

www.acmicpc.net


  • 문제


  • 문제풀이

카운트를 올려가면서 K와 카운트가 같아지면 Dequeue를 통해 빼주고 나머지는 다시 Enqueue로 넣어준다.


  • 코드 1
using System.Collections.Generic;
using System;
using System.Collections;
using System.Text;
namespace yongyong2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Queue<int> queue = new Queue<int>();
            StringBuilder sb= new StringBuilder();
            string[] input = Console.ReadLine().Split();
            int N = int.Parse(input[0]);
            int K = int.Parse(input[1]);
            for (int i = 1; i <= N; i++)
            {
                queue.Enqueue(i);
                
            }
            
            sb.Append("<");
            int count = 0;
            while (queue.Count>1)
            {
                count++;
                if (count == K)
                {
                    sb.Append(string.Format("{0}, ", queue.Dequeue()));
                    count = 0;
                }
                else
                {
                    queue.Enqueue(queue.Dequeue());
                }
            }
            sb.Append(string.Format("{0}>", queue.Dequeue()));
            Console.WriteLine(sb.ToString());
        }
    }
}

  • 후기

Enqueue와 Dequeue를 이해하기 좋은 문제이다.

LIST

'백준 > C#' 카테고리의 다른 글

[C#]백준 15828번: Router  (2) 2023.01.28
[C#]백준 1966번: 프린터 큐  (0) 2023.01.28
[C#]백준 2164번: 카드2  (0) 2023.01.28
[C#]백준 9012번: 괄호  (0) 2023.01.14
[C#]백준 10773번: 제로  (0) 2023.01.05

+ Recent posts