Algorithm에 해당하는 글 7

  1. 백준 94612023.08.20
  2. 백준 11492023.08.20
  3. 백준 118662023.08.19
  4. 백준 127892023.08.19
  5. 백준 27982023.08.18
  6. 백준 28392023.08.18
  7. 시작한 계기2023.08.18

백준 9461

Algorithm|2023. 8. 20. 20:04

이건 대놓고 수열이라 오히려 점화식이 금방 보였다.

DP 치고 쉬워서 이거 푼다음 자신감 붙어서 여러개 도전했다가 싹 머리 깨졌다.

DP 어렵다. 많이 해봐야 할 듯.

 

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

 

9461번: 파도반 수열

오른쪽 그림과 같이 삼각형이 나선 모양으로 놓여져 있다. 첫 삼각형은 정삼각형으로 변의 길이는 1이다. 그 다음에는 다음과 같은 과정으로 정삼각형을 계속 추가한다. 나선에서 가장 긴 변의

www.acmicpc.net

using System;

namespace Padoban
{
    class b9461
    {
        static void solution(int N)
        {
            ulong[] P = new ulong[N];

            if(N < 3)
            {
                Console.WriteLine("1");
                return;
            }
            P[0] = 1; P[1] = 1; P[2] = 1;
            for (int i = 3; i < N; i++)
            {
                P[i] = P[i-2] + P[i-3];
            }
            Console.WriteLine(P[N-1]);
        }
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            for(int i = 0; i < T; i++)
            {
                int N = int.Parse(Console.ReadLine());
                solution(N);
            }
        }
    }
}

'Algorithm' 카테고리의 다른 글

백준 1149  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 12789  (0) 2023.08.19
백준 2798  (0) 2023.08.18
백준 2839  (0) 2023.08.18

댓글()

백준 1149

Algorithm|2023. 8. 20. 17:02

예전에 DP를 제일 어려워했던 기억이 있어서 바로 DP 갈겨봤다.

아직 잘 못풀겠다. 그동안 연습을 안했으니 당연히 그대로 풀기 어려운거라 생각한다.

점화식을 찾아내서 그걸 코드로 바꾸는 부분에서 많이 헤멘다.

누군가의 점화식을 보고 풀었다.

그리고 동적 배열로 할당하니 

system.nullreferenceexception: object reference not set to an instance of an object.

라는 오류가 떠서 [][] 이모양 말고 [,] 으로 바꿔서 했다.

 

 

언젠간 잘하게 되겠지.

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

 

1149번: RGB거리

첫째 줄에 집의 수 N(2 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 각 집을 빨강, 초록, 파랑으로 칠하는 비용이 1번 집부터 한 줄에 하나씩 주어진다. 집을 칠하는 비용은 1,000보다 작거나

www.acmicpc.net

namespace RGB_Street
{
    class b1149
    {
        static void solution(int N, int[,] cost)
        {
            for (int i = 1; i < N; i++)
            {
                cost[i,0] += Math.Min(cost[i - 1,1], cost[i - 1,2]);
                cost[i,1] += Math.Min(cost[i - 1,0], cost[i - 1,2]);
                cost[i,2] += Math.Min(cost[i - 1,0], cost[i - 1,1]);
            }
            int min = Math.Min(cost[N-1,0], cost[N-1,1]);
            min = Math.Min(cost[N-1,2], min);

            Console.WriteLine(min);
        }
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            int[,] cost = new int[N,3];
            for (int i = 0; i < N; i++)
            {
                string[] tmp = Console.ReadLine().Split(' ');
                for (int j = 0; j < 3; j++)
                {
                    cost[i,j] = int.Parse(tmp[j]);
                }
            }
            solution(N, cost);
        }
    }
}

'Algorithm' 카테고리의 다른 글

백준 9461  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 12789  (0) 2023.08.19
백준 2798  (0) 2023.08.18
백준 2839  (0) 2023.08.18

댓글()

백준 11866

Algorithm|2023. 8. 19. 18:54

스택도 했으니 큐도 한번 해봐야 하지 않을까? 해서 풀어본 문제.
확실히 스택이든 큐든 옛날에 C로 직접 구현했을땐 골때렸는데 라이브러리로 퉁치는게 쉽고 빠르다.

문제가 무슨말을 하는지 잘 이해안되서 그림판에다 해보니 바로 이해가 갔던..

 

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

 

11866번: 요세푸스 문제 0

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

www.acmicpc.net

using System;
using System.Collections;
using System.Collections.Generic;

namespace yosepus
{
    class b11866
    {
        static void solution(int N, int K)
        {
            Queue<int> SQ = new Queue<int>(N);
            int flag = N;
            int set = 1;
            Console.Write("<");
            for (int i = 1; i <= N; i++)
            {
                SQ.Enqueue(i);
            }
            while (SQ.Count() != 0)
            {
                int k = SQ.Dequeue();
                if(set != K)
                {
                    SQ.Enqueue(k);
                    set++;
                }
                else
                {
                    if(SQ.Count == 0)
                    {
                        Console.WriteLine("{0}>", k);
                        break;
                    }
                    Console.Write("{0}, ", k);
                    set = 1;
                }
               
                flag--;
                //SQ.TrimToSize();
            }
            //Console.Write(">");
        }
        static void Main(string[] args)
        {

            int N = 0, K = 0;
            string[] tmp = Console.ReadLine().Split(' ');

            N = int.Parse(tmp[0]);
            K = int.Parse(tmp[1]);


            solution(N, K);
            //Console.WriteLine("Input N: {0}, K: {1}", N, K);
        }
    }
}

'Algorithm' 카테고리의 다른 글

백준 9461  (0) 2023.08.20
백준 1149  (0) 2023.08.20
백준 12789  (0) 2023.08.19
백준 2798  (0) 2023.08.18
백준 2839  (0) 2023.08.18

댓글()

백준 12789

Algorithm|2023. 8. 19. 17:52

그래도 스택 자료구조는 한번 해봐야 하지 않을까? 해서 풀어본 문제

정답률이 꽤 낮아 풀어봤다.

아마 사이드에 빠졌다가 원래 있던 줄로 다시 돌아갈 수 없는데,

돌아갈 수 있다고 착각해서 틀린 나같은 사람이 많아서 정답률이 낮은것 같다.

 

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

 

12789번: 도키도키 간식드리미

인하대학교 학생회에서는 중간, 기말고사 때마다 시험 공부에 지친 학우들을 위해 간식을 나눠주는 간식 드리미 행사를 실시한다. 승환이는 시험 기간이 될 때마다 간식을 받을 생각에 두근두

www.acmicpc.net

using System;
using System.Collections.Generic;
using System.Net;

namespace Snack_Line
{
    class b12789
    {
        static string solution(Stack<int> num)
        {
            Stack<int> side = new Stack<int>();
            int check = 1;

            while(true)
            {
                if (num.Contains(check)){
                    int tmp = 0;
                    while (check != (tmp = num.Pop()))
                    {
                        side.Push(tmp);
                    }
                    //Console.WriteLine(tmp);
                }
                else if (side.Contains(check))
                {
                    while (true)
                    {
                        int tmp = side.Pop();
                        //Console.WriteLine(tmp);
                        if (check != tmp)
                        {
                            return "Sad";
                        }
                        //if(side.Count == 0)
                        //{
                        //    break;
                        //}
                        break;
                    }
                }
                else
                {
                    return "Sad";
                }
                if ((num.Count == 0) && (side.Count == 0))
                {
                    return "Nice";
                }
                check++;
                //break;
            }
        }

        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            string[] tmp = Console.ReadLine().Split(' ');
           
            Stack<int> number = new Stack<int>();

            for(int i = N-1; i >= 0; i--)
            {
                number.Push(int.Parse(tmp[i]));
            }
           
            Console.WriteLine(solution(number));
        }
    }
}

 

'Algorithm' 카테고리의 다른 글

백준 1149  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 2798  (0) 2023.08.18
백준 2839  (0) 2023.08.18
시작한 계기  (0) 2023.08.18

댓글()

백준 2798

Algorithm|2023. 8. 18. 20:00

블랙잭 문제이다.

알고리즘은 브루트포스 알고리즘. 그냥 싹 다 돌려보는거에 이런 거창한 이름이 붙은 이유가 뭘까?

근데 틀렸다. 예시는 잘 나오는데 반례를 잘 찾아서 다시 풀어보자.

3중 for문 같은 코드 더럽게 만드는걸 굉장히 싫어하는데 이것도 고칠 방법을 찾아보고 싶다.

 

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

 

2798번: 블랙잭

첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장

www.acmicpc.net

using System;

namespace BlackJack
{
    class b2798
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            string[] num = Console.ReadLine().Split();

            int[] num_list = new int[int.Parse(input[0])];
            for (int i = 0; i < num.Length; i++)
            {
                num_list[i] = int.Parse(num[i]);
            }

            b2798 sol = new b2798();
            int s = sol.solution(int.Parse(input[1]), num_list);
            Console.WriteLine(s);
        }
        public int solution(int target_number, int[] list)
        {
            int sol = 0;

            for(int i = 0; i < list.Length-2; i++)
            {
                for(int j = i+1; j < list.Length-1; j++)
                {
                    for(int k = i+2; k < list.Length; k++)
                    {
                        int sum = list[i] + list[j] + list[k];
                        if((sum <= target_number) && (sum > sol))
                        {
                            sol = sum;
                        }
                    }
                }
            }

            return sol;
        }
    }
}

'Algorithm' 카테고리의 다른 글

백준 1149  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 12789  (0) 2023.08.19
백준 2839  (0) 2023.08.18
시작한 계기  (0) 2023.08.18

댓글()

백준 2839

Algorithm|2023. 8. 18. 19:32

처음엔 너무 쉬운 두문제를 풀어서 두개는 건너뛰고 기록한다.

 

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

 

2839번: 설탕 배달

상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. 상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. 설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. 봉지는 3킬로그

www.acmicpc.net

알고리즘 분류는 DP와 Greedy인데 어떤 알고리즘이다 생각없이 그냥 막 푼것같다.

일단 절대 DP로 푼건 아니다.

 

using System;
using System.Collections.Generic;

namespace Sugar_Delivery
{
    class b2839
    {
        static void Main(string[] args)
        {
            int val = int.Parse(Console.ReadLine());

            Console.WriteLine(solution(val));

        }

        static int solution(int sugar)
        {
            int sol = sugar;
            int cnt = 0;

            for(int i = 0; i <= (sugar / 5); i++)
            {
                // i = big plastic bag num
                for(int j = 0; j <= (sugar - (5 * i)); j++)
                {
                    // j = small plastic bag num
                    if (((5 * i + 3 * j) == sugar) && i + j < sol)
                    {
                        sol = i + j;
                        cnt++;
                    }
                }
            }

            if (cnt == 0)
                return -1;
            else
                return sol;
        }
    }
}

'Algorithm' 카테고리의 다른 글

백준 1149  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 12789  (0) 2023.08.19
백준 2798  (0) 2023.08.18
시작한 계기  (0) 2023.08.18

댓글()

시작한 계기

Algorithm|2023. 8. 18. 19:00

알고리즘 스터디.. 가끔씩 하더라도 매일 일기쓰기보다 힘들다.

거의 2년만에 다시 한 계기도 별건 없고 이제부터 개발하는데 C#을 쓸것 같아

C#에 적응도 할 겸 다시 풀어본다..

'Algorithm' 카테고리의 다른 글

백준 1149  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 12789  (0) 2023.08.19
백준 2798  (0) 2023.08.18
백준 2839  (0) 2023.08.18

댓글()