Development/Program Solving

[백준 단계별로 풀어보기] 심화 1 with C#

이쥬딩 2023. 7. 11. 21:20
728x90

25083번: 새싹

 Console.WriteLine("         ,r'\"7");
      Console.WriteLine("r`-_   ,'  ,/");
      Console.WriteLine(" \\. \". L_r'");
      Console.WriteLine("   `~\\/");
      Console.WriteLine("      |");
      Console.WriteLine("      |");

3003번: 킹, 퀸, 룩, 비숍, 나이트, 폰

string[] strArray = Console.ReadLine().Split();
      int[] intArray = strArray.Select(int.Parse).ToArray();

      int[] result = { 1, 1, 2, 2, 2, 8 };

      for (var i = 0; i < result.Length; i++)
      {
        result[i] -= intArray[i];
      }

      Console.WriteLine(String.Join(" ", result));

2444번: 별 찍기 - 7

int num = int.Parse(Console.ReadLine());

      int count = 2 * num - 1;
      int whiteSpace = (count - 1) / 2;
      int star = 1;

      List<string> result = new List<string>();

      for (int i = 0; i < num; i++)
      {
        string a = null;

        for (int x = 0; x < whiteSpace; x++)
        {
          a += " ";
        }
        for (int y = 0; y < star; y++)
        {
          a += "*";
        }

        if (whiteSpace != 0 && star != count)
        {
          whiteSpace -= 1;
          star += 2;
        }


        result.Add(a);
      }



      for (int i = 0; i < num - 1; i++)
      {
        string a = null;

        whiteSpace += 1;
        star -= 2;

        for (int x = 0; x < whiteSpace; x++)
        {
          a += " ";
        }
        for (int y = 0; y < star; y++)
        {
          a += "*";
        }
        result.Add(a);
      }

      foreach (var item in result)
      {
        Console.WriteLine(item);
      }

맘에 안들어서 아래로 다시 수정함

문자열 반복 되는 stringbulder를 쓰자!

using System.Text;
class Program
  {
    static void Main(string[] args)
    {
      int num = int.Parse(Console.ReadLine());
      StringBuilder stringBuilder = new StringBuilder();

      int count = 2 * num - 1;
      int whiteSpace =a num - 1;
      int star = 1;
      bool standard = true;

      for (var i = 1; i <= count; i++)
      {
        if (num == i) standard = false;
        stringBuilder.Append(' ', whiteSpace);
        stringBuilder.Append('*', star);
        stringBuilder.Append('\n');


        if (standard == true)
        {
          whiteSpace -= 1;
          star += 2;
        }
        else if (standard == false)
        {
          whiteSpace += 1;
          star -= 2;
        }

      }

      Console.WriteLine(stringBuilder);


    }}

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

 string str = Console.ReadLine();

      string revValue = new string(str.Reverse().ToArray());
      bool result = str.Equals(revValue);


      Console.WriteLine(result ? 1 : 0);

1157번: 단어 공부

아ㅣㅏㅏ코드 맘에 안들어

더 좋은 코드 의견 있으면 댓글 부탁드려요....

char[] str = Console.ReadLine().ToCharArray();
      int max = 0;
      string result = null;

      var list = str.GroupBy(x => x.ToString().ToUpper()).Select(s => new
      {
        count = s.Count(),
        value = s.Key
      }).OrderByDescending(d => d.count).ToArray();

      max = list[0].count;
      result = list[0].value;

      for (int i = 1; i < list.Length; i++)
      {
        if (max <= list[i].count) result = "?";
      }

      Console.WriteLine(result);

2941번: 크로아티아 알파벳

Regex 정규식 Replace 

근데 Regex.replace는 string.replace보다 느리다고 함

using System.Text.RegularExpressions;
class Program
  {
    static void Main(string[] args)
    {
      string str = Console.ReadLine();
      string result = Regex.Replace(str, @"c=|c-|dz=|d-|lj|nj|s=|z=", " ");

      Console.WriteLine(result.Length);

    }
  }

1316번: 그룹 단어 체커

using System.Text.RegularExpressions; 
class Program
  {
    static void Main(string[] args)
    {
int num = int.Parse(Console.ReadLine());
      int result = 0;

      for (var i = 0; i < num; i++)
      {
        string str = Console.ReadLine();

        string pattern = @"(\w)\1*";
        Regex regExp = new Regex(pattern);
        var matches = regExp.Matches(str);

        var tab = matches.Select(x => x.Value.First()).GroupBy(x => x).Where(g => g.Count() > 1).Select(s => s.Key).ToArray();

        if (tab.Length == 0) result++;

      }


      Console.WriteLine(result);
    }}

25206번: 너의 평점은

double total_major_avg = 0;
      double total_credit = 0;

      for (int i = 0; i < 20; i++)
      {
        string[] array = Console.ReadLine().Split();

        if (array[2] == "P") continue;

        double subject_avg = 0;
        switch (array[2])
        {
          case "A+":
            subject_avg = 4.5;
            break;
          case "A0":
            subject_avg = 4;
            break;
          case "B+":
            subject_avg = 3.5;
            break;
          case "B0":
            subject_avg = 3;
            break;
          case "C+":
            subject_avg = 2.5;
            break;
          case "C0":
            subject_avg = 2;
            break;
          case "D+":
            subject_avg = 1.5;
            break;
          case "D0":
            subject_avg = 1;
            break;
          case "F":
            subject_avg = 0;
            break;
        }

        total_major_avg += double.Parse(array[1]) * subject_avg;
        total_credit += double.Parse(array[1]);
      }

      Console.WriteLine(total_major_avg / total_credit);
728x90