"깊이"가 다른 게임개발자 허민영

유저에서 게임까지, 철학에서 코딩까지, 본질을 보는 게임개발

코딩테스트

Lv0-3. 연산

허민영 2025. 1. 10. 15:24
  • 길이가 같은 두 문자열 str1과 str2가 주어집니다. 두 문자열의 각 문자가 앞에서부터 서로 번갈아가면서 한 번씩 등장하는 문자열을 만들어 return 하는 solution 함수를 완성해 주세요

  • 나의 해답
using System;

public class Solution {
    public string solution(string str1, string str2) {
        string answer = "";
        for(int i=0; i<str1.Length;i++)
        {
            answer +=str1[i];
            answer +=str2[i];
            //answer += string.Format("{0}{1}",str1[i],str2[i]);
        }        
        return answer;
    }
}

보완점 : Format을 활용하여 안정성과 완결성 높은 형태로 개선가능

 

  • 문자들이 담겨있는 배열 arr가 주어집니다. arr의 원소들을 순서대로 이어 붙인 문자열을 return 하는 solution함수를 작성해 주세요.

  • 나의 해답
using System;

public class Solution {
    public string solution(string[] arr) {
        string answer = "";
        foreach(string s in arr)
        {
           answer += s; 
           //answer = String.Join("", arr);
           //answer = string.Concat(arr);
        }
        return answer;
    }
}

 

 

  • 문자열 my_string과 정수 k가 주어질 때, my_string을 k번 반복한 문자열을 return 하는 solution 함수를 작성해 주세요.

  • 나의해답
using System;

public class Solution {
    public string solution(string my_string, int k) {
        string answer = "";
        for(int i = 0; i<k; i++){
            answer += my_string;
        }
        return answer;
    }
}

 

 

  • 연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.
    • 12 ⊕ 3 = 123
    • 3 ⊕ 12 = 312

양의 정수 a와 b가 주어졌을 때, a  b와 b  a 중 더 큰 값을 return 하는 solution 함수를 완성해 주세요.

단, a  b와 b  a가 같다면 a  b를 return 합니다.

  • 나의 해답
using System;

public class Solution
{
    public int solution(int a, int b)
    {
        int answer = (Calc(a, b) > Calc(b, a)) ? Calc(a, b) : Calc(b, a);
        return answer;
    }
    private int Calc(int i1, int i2)
    {
        int result = Convert.ToInt32(i1.ToString() + i2.ToString());
        return result;
    }
}
//참고 풀이
using System;

public class Solution {
    public int solution(int a, int b) 
    {
        int aNum = Int32.Parse($"{a}{b}");
        int bNum = Int32.Parse($"{b}{a}");
        return Math.Max(aNum, bNum);
    }
}

보완점 : 동일 계산을 2번씩 함

 

  • 연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.
    • 12 ⊕ 3 = 123
    • 3 ⊕ 12 = 312
  • 양의 정수 a와 b가 주어졌을 때, a ⊕ b와 2 * a * b 중 더 큰 값을 return하는 solution 함수를 완성해 주세요. 단, a  b와 2 * a * b가 같으면 a  b를 return 합니다.


  • 나의해답
using System;

public class Solution
{
    public int solution(int a, int b)
    {
        int abJoin = Calc(a, b);
        int answer = (abJoin > 2 * a * b) ? abJoin : 2 * a * b;
        return answer;
    }
    private int Calc(int i1, int i2)
    {
        int result = Convert.ToInt32(i1.ToString() + i2.ToString());
        return result;
    }
}

 

'코딩테스트' 카테고리의 다른 글

Lv0-6. 조건문, 반복문  (2) 2025.01.13
Lv0-5. 조건문  (1) 2025.01.11
Lv0-4. 연산,조건문  (1) 2025.01.10
Lv0-2. 출력,연산  (3) 2025.01.02
Lv0-1. 출력  (1) 2024.12.30