백준/C#

[C#]백준 17827번: 달팽이 리스트

용용코딩 2023. 1. 28. 16:14
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