SMALL

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

 

28135번: Since 1973

$N$이 주어진다. $(1 \leq N \leq 1\, 000\, 000)$

www.acmicpc.net


  • 문제


  • 문제풀이

문자열 탐색이기 때문에 하나는 KMP알고리즘을 사용하고 하나는 C#에 내장된 contain함수를 쓰겠다.


  • 코드 1
using System;
using System.Reflection.Metadata.Ecma335;

class GFG
{
    public static int count = 0;
    void KMPSearch(string pat, string txt)
    {
        int M = pat.Length;
        int N = txt.Length;
        bool print = false;
        int[] lps = new int[M];
        int j = 0;
        computeLPSArray(pat, M, lps);

        int i = 0;
        while (i < N)
        {
            if (pat[j] == txt[i])
            {
                j++;
                i++;
            }
            if (j == M)
            {
                j = lps[j - 1];
                print = true;
                break;
            }

            // mismatch after j matches
            else if (i < N && pat[j] != txt[i])
            {
                if (j != 0)
                    j = lps[j - 1];
                else
                    i = i + 1;
            }
        }
        if (print)
        {
            count++;
        }
        
    }

    void computeLPSArray(string pat, int M, int[] lps)
    {
        int len = 0;
        int i = 1;
        lps[0] = 0;
        while (i < M)
        {
            if (pat[i] == pat[len])
            {
                len++;
                lps[i] = len;
                i++;
            }
            else
            {
                if (len != 0)
                {
                    len = lps[len - 1];
                }
                else
                {
                    lps[i] = len;
                    i++;
                }
            }
        }
    }


    public static void Main()
    {
        string txt = Console.ReadLine();
        for (int i=0;i<int.Parse(txt); i++)
        {
            new GFG().KMPSearch("50", i.ToString());
            count++;
        }
        Console.WriteLine(count);
    }
}
  • 코드 2
using System;

class MainClass
{
    public static void Main(string[] args)
    {
        string input = Console.ReadLine();
        int count = 0;
        for(int i = 0; i < int.Parse(input); i++)
        {
            count++;
            if (i.ToString().Contains("50"))
            {
                count++;
            }
        }
        Console.WriteLine(count);
    }
}

  • 후기

문자열 탐색은 언제나 쉬운듯 어렵다

LIST
SMALL

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

 

25192번: 인사성 밝은 곰곰이

첫번째 새로운 사람이 들어온 뒤  pjshwa, chansol, chogahui05은 모두 곰곰티콘으로 인사했다. 두번째 새로운 사람이 들어온 뒤  pjshwa와 chansol은 다시 곰곰티콘으로 인사했다.

www.acmicpc.net


  • 문제


  • 문제풀이

HashSet을 설정해서 key값을 통해 기존에 있던 아이디인지 확인이 가능하다.


  • 코드 1
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;


class Program
{
    
    static void Main(string[] args)
    {
        int n = int.Parse(ReadLine());
        HashSet<string> ans = new HashSet<string>();
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            string str = ReadLine();
            if (str == "ENTER")
            {
                cnt += ans.Count();
                ans.Clear();
            }
            else
            {
                if (!ans.Contains(str))
                {
                    ans.Add(str);
                    
                }
            }
        }
        cnt += ans.Count();
        WriteLine(cnt);

    }
    
}

  • 후기

HashSet은 처음 써본다

LIST
SMALL

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

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net


  • 문제


  • 문제풀이

A부터 Z까지 순서대로 나열해서 max를 카운팅해준다.


  • 코드 1
using System;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string word = Console.ReadLine().ToUpper();
            int[] count = new int[26];
            
            foreach (char c in word)
            {
                count[c - 'A']++;
            }
            
            int maxcnt = 0;
            char maxChar = 'A';
            bool two = false;
            
            for (int i = 0; i < 26; i++)
            {
                if (count[i] > maxcnt)
                {
                    maxcnt = count[i];
                    maxChar = (char)('A' + i);
                    two = false;
                }
                else if (count[i] == maxcnt)
                {
                    two = true;
                }
            }
            
            Console.WriteLine(two ? "?" : maxChar.ToString());
        }
    }
}

  • 후기

문자열 탐색때도 비슷한방법을 썼던것 같다.

LIST
SMALL

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

 

25206번: 너의 평점은

인하대학교 컴퓨터공학과를 졸업하기 위해서는, 전공평점이 3.3 이상이거나 졸업고사를 통과해야 한다. 그런데 아뿔싸, 치훈이는 깜빡하고 졸업고사를 응시하지 않았다는 사실을 깨달았다! 치

www.acmicpc.net


  • 문제


  • 문제풀이

swich-case문을 통해 조건을 설정하여 답을 구했다.


  • 코드 1
using System.Xml.Schema;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            double result = 0;
            double haktotal = 0;
            
            for(int i = 0; i < 20; i++)
            {
                string[] input = Console.ReadLine().Split();
                double hak = double.Parse(input[1]);
                if (input[2] != "P")
                {
                    haktotal += hak;
                }
                switch (input[2])
                {
                    case "A+":
                        result+=hak*4.5;
                        break;
                    case "A0":
                        result+=hak * 4.0;
                        break;
                    case "B+":
                        result+=hak * 3.5;
                        break;
                    case "B0":
                        result+=hak * 3.0;
                        break;
                    case "C+":
                        result+=hak *2.5;
                        break;
                    case "C0":
                        result+=hak * 2.0;
                        break;
                    case "D+":
                        result+=hak * 1.5;
                        break;
                    case "D0":
                        result+=hak * 1.0;
                        break;
                    case "F":
                        result+=hak * 0;
                        break;
                    case "P":
                        continue;
                }
            }

            Console.WriteLine(result / haktotal);
        }
    }
}

  • 후기

정말 오랜만에 swich-case를 써본것 같다.

LIST
SMALL

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

 

10988번: 팰린드롬인지 확인하기

첫째 줄에 단어가 주어진다. 단어의 길이는 1보다 크거나 같고, 100보다 작거나 같으며, 알파벳 소문자로만 이루어져 있다.

www.acmicpc.net


  • 문제


  • 문제풀이

문자를 쪼개서 거꾸로 비교해나간다.


  • 코드 1
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            bool check = true;
            for (int f = 0, l = input.Length - 1 - f; f < l; f++, l--)
            {
                if (input[f] != input[l])
                {
                    check = false;
                }
                else
                {
                    continue;
                }
            }

            if (check)
            {
                Console.WriteLine("1");
            }
            else
            {
                Console.WriteLine("0");
            }
        }
    }
}
  • 코드2
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            char[] inputarr = input.ToCharArray();
            string reverse="";
            for(int i = inputarr.Length-1; i >=0; i--)
            {
                reverse += inputarr[i];
            }
           
            if (input == reverse)
            {
                Console.WriteLine("1");
            }
            else
            {
                Console.WriteLine("0");
            }
        }
    }
}

  • 후기

C#은 자체 함수가 많아서 참 좋다

LIST

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

[C#]백준 1157번: 단어공부  (0) 2023.07.19
[C#]백준 25206: 너의 평점은  (0) 2023.07.19
[C#]백준 23292번: 코딩 바이오리듬  (0) 2023.07.19
[C#]백준 15652번: N과 M(4)  (0) 2023.07.19
[C#]백준 15651번: N과 M(3)  (0) 2023.07.19
SMALL

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

 

23292번: 코딩 바이오리듬

바이오리듬(biorhythm)이라는 이론을 들어본 적 있는가? 바이오리듬은 인체에 신체,감성,지성의 세가지 주기가 생년월일의 입력에 따라 어떤 패턴으로 나타나고, 이 패턴의 조합에 따라 능력이나

www.acmicpc.net


  • 문제


  • 문제풀이

날짜를 쪼개서 노가다 해주었다.


  • 코드 1
namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string birth = Console.ReadLine();
            int N = int.Parse(Console.ReadLine());
            string[] coding = new string[N];
            
            int max = 0;
            string answer="";
            for(int i = 0; i < N; i++)
            {
                coding[i] = Console.ReadLine();
            }
            Array.Reverse(coding);
            
            for (int i = 0; i < N; i++)
            {
                
                int sum = 0;
                int year = 0;
                int month = 0;
                int day = 0;
                for (int j = 0; j < 4; j++)
                {
                    char temp=(coding[i][j]);
                    
                    int digit = int.Parse(temp.ToString());
                    digit = digit - int.Parse(birth[j].ToString());
           
                    int square = digit * digit;
                   
                    year += square;
                }
                for(int j = 4; j < 6; j++)
                {
                    char temp = (coding[i][j]);
                    int digit = int.Parse(temp.ToString());
                    digit = digit - int.Parse(birth[j].ToString());
                    int square = digit * digit;
                    month += square;
                }
                for (int j = 6; j < 8; j++)
                {
                    char temp = (coding[i][j]);
                    int digit = int.Parse(temp.ToString());
                    digit = digit - int.Parse(birth[j].ToString());
                    int square = digit * digit;
                    day += square;
                }
                sum = year * month * day;
              
                if (sum > max)
                {
                    max = sum;
                    answer = coding[i];
                }
            }
            Console.WriteLine("{0}",answer);
        }
    }
}

  • 후기

클린코드로 해보고싶은데 방법이 없으려나

LIST

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

[C#]백준 25206: 너의 평점은  (0) 2023.07.19
[C#]백준 10988번: 팰린드롬인지 확인하기  (0) 2023.07.19
[C#]백준 15652번: N과 M(4)  (0) 2023.07.19
[C#]백준 15651번: N과 M(3)  (0) 2023.07.19
[C#]백준 15650번: N과 M(2)  (0) 2023.07.19
SMALL

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

 

15652번: N과 M (4)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net


  • 문제


  • 문제풀이

백트래킹 문제이다. 깊이 탐색을 활용해서 풀어준다.


  • 코드 1
using System.ComponentModel.Design;
using System.Text;

namespace ConsoleApp2
{
    internal class Program
    {
        static public StringBuilder sb = new StringBuilder();
        static public bool[] visited;
        static public int[] result;
        static void Main(string[] args)
        {
            
            
            string[] input = Console.ReadLine().Split();
            int N = int.Parse(input[0]);
            int M = int.Parse(input[1]);
            result= new int[N];
            visited= new bool[N];
            
            
            dfs(N, M, 0, 1);
            Console.WriteLine(sb.ToString());
        }
        static void dfs(int N, int M, int cnt, int num)
        {
            if (cnt == M)
            {
                for(int i=0;i<M;i++)
                {
                    sb.Append(result[i]+" ");
                }
                sb.AppendLine();
                return;
            }
            for(int i=num;i<=N;i++)
            {
                //if (!visited[i])
                //{
                //    visited[i]=true;
                    result[cnt] = i;
                    dfs(N, M, cnt+1, i);
                //    visited[i] = false;
                //}
            }
        }
    }
}

  • 후기

백트래킹에 dfs를 변형해서 할 수 있다.

LIST

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

[C#]백준 10988번: 팰린드롬인지 확인하기  (0) 2023.07.19
[C#]백준 23292번: 코딩 바이오리듬  (0) 2023.07.19
[C#]백준 15651번: N과 M(3)  (0) 2023.07.19
[C#]백준 15650번: N과 M(2)  (0) 2023.07.19
[C#]백준 15649번: N과 M(1)  (0) 2023.07.19
SMALL

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

 

15651번: N과 M (3)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net


  • 문제


  • 문제풀이

백트래킹 문제이다. 깊이 탐색을 활용해서 풀어준다.


  • 코드 1
using System.ComponentModel.Design;
using System.Text;

namespace ConsoleApp2
{
    internal class Program
    {
        static public StringBuilder sb = new StringBuilder();
        static public bool[] visited;
        static public int[] result;
        static void Main(string[] args)
        {
            
            
            string[] input = Console.ReadLine().Split();
            int N = int.Parse(input[0]);
            int M = int.Parse(input[1]);
            result= new int[N];
            visited= new bool[N];
            
            
            dfs(N, M, 0, 1);
            Console.WriteLine(sb.ToString());
        }
        static void dfs(int N, int M, int cnt, int num)
        {
            if (cnt == M)
            {
                for(int i=0;i<M;i++)
                {
                    sb.Append(result[i]+" ");
                }
                sb.AppendLine();
                return;
            }
            for(int i=num;i<=N;i++)
            {
                //if (!visited[i])
                //{
                //    visited[i]=true;
                    result[cnt] = i;
                    dfs(N, M, cnt+1, num);
                //    visited[i] = false;
                //}
            }
        }
    }
}

  • 후기

시간이 2272ms인데 더 줄일 수 없을까...?

LIST

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

[C#]백준 23292번: 코딩 바이오리듬  (0) 2023.07.19
[C#]백준 15652번: N과 M(4)  (0) 2023.07.19
[C#]백준 15650번: N과 M(2)  (0) 2023.07.19
[C#]백준 15649번: N과 M(1)  (0) 2023.07.19
[C#]백준 1541번: 잃어버린 괄호  (0) 2023.07.19

+ Recent posts