728x90
SMALL

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] array, int[,] commands) {
        int[] answer = new int[commands.GetLength(0)];
        List<int> list = new List<int>();
        int k = 0;
        
        for(int i=0;i<commands.GetLength(0);i++)
        {
            for(int j=commands[i,0]-1;j<commands[i,1];j++)
            {
                list.Add(array[j]);

                // 자르기
            }


            list.Sort(); // 소팅하기
            answer[i] = int.Parse(list[commands[i,2]-1+k].ToString());
            // i에서 부터 k번째 수를 집어넣는다.            


            for(int p = 0;p<list.Count;p++)
            {
                list[p] = 0;
            }

// 소팅한 게 반영이 안되게 하려면 / for문을 돌리기 위해서는 리스트를 다 0으로 초기화해야함. 이러면 마이너스가 있을 때 문제 생길 듯. Abs로하고 소팅하면 되려나
            k += commands[i,1]-commands[i,0]+1;
        }
        return answer;
    }
}

 

 

 

///

 

 

 

using System;
using System.Collections.Generic;
public class Solution {
    public int[] solution(int[] array, int[,] commands) {
        int a2 = commands.Length / 3;
        int[] answer = new int[a2];
        List<int> list = new List<int>();

            for (int b = 0; b < a2; b++)
            {
               list.Clear();
                int i = commands[b, 0];
                int j = commands[b, 1];
                int k = commands[b, 2];
                
                 for (int a = i - 1; a < j; a++)
                {
                    list.Add(array[a]);
                }
                
                list.Sort();
                answer[b] = list[k-1];
            }
        return answer;
    }
}
728x90

설정

트랙백

댓글

728x90
SMALL

using System;

public class Solution {
    public int solution(int n) {
        int answer = 0;
        int ans = 0;
        int a = 0;
        int k = 1;
        string samjin = "";
        
        while(n > 0)
        {
            ans = n % 3;
            samjin += ans.ToString();
            n = n / 3;
            a++;
        }
        
        for(int i = a-1;i>=0;i--)
        {
            answer += k * int.Parse(samjin[i].ToString());
            k = 3 * k; 
        }
        
        return answer;
    }
}

 

//하기는 했는데 변수가 너무 남발되는 거 같다. 처음부터 거꾸로 3진법을 출력하게 했으니 원래대로 3진법이 나오게 하는 방법도 찾아봐야 할 거 같다.

////

using System;

public class Solution {
    public int solution(int n) {
        int answer = 0;
        
    	while(n>0)
    	{
        	answer = answer * 3;
        	answer += n%3;
        	n/=3;
    	}
        
    return answer;
    }
}
728x90

'프로그래밍' 카테고리의 다른 글

C# 소수 만들기(3중 for문)  (0) 2023.01.26
코테 공부 순서  (0) 2023.01.26
C# 타겟 넘버(DFS)  (0) 2023.01.26
C# 개인정보 수집 유효기간  (0) 2023.01.26
C# 기사단원의 무기(약수)  (0) 2023.01.26

설정

트랙백

댓글

728x90
SMALL

using System;

public class Solution {
    public int solution(string skill, string[] skill_trees) {
        int answer = 0;
        int treeLength = skill_trees.Length; // 스킬 트리 배열의 길이 취득

    for(int i=0;i<treeLength;i++)
    {
        bool flag = true;
        char[] skills = skill_trees[i].ToCharArray();
        int cnt = 0;
        int skillsLen = skills.Length;
        
    for(int j=0;j<skills.Length;j++)
    {
        if(cnt < skill.IndexOf(skills[j]))
        {
            flag = false;
            break;
        }
        else if(cnt == skill.IndexOf(skills[j]))
        {
            cnt++;
        }
    }
        if(flag)
        {
            answer++;
        }
    }
        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

using System;

public class Solution {
    public int solution(int[] d, int budget) {
        int answer = 0;
        Array.Sort(d);

/// 소팅하고 마이너스 
        
        for(int i = 0;i<d.Length;i++)
        {
            if(budget >= d[i])
            {
                budget = budget - d[i];
                answer++;
            }
        }
        
        return answer;
    }
}

 

// 다른 사람의 풀이를 보고 한 건데 약간 함정이 있는 게

// 이건 모든 경우에서 예산 배분이 안되는 경우가 없게 설정되어 있는 듯하다.

// 그래서 알고리즘 상으로는 budget을 계속 마이너스 시켜서 0보다 작아지면 그 때 까지 축적된 answer 값을 출력시키는 건데 만약에 예산 배분이 안되면 리턴 값이 0이 나오거나 -1이 나오거나 이런 식으로 처리해야 되는데 그런 케이스는 없는 경우인 거 같다.

728x90

설정

트랙백

댓글

728x90
SMALL

using System;

public class Solution {
    public int solution(int[,] sizes) {

        
        for(var i=0; i< sizes.GetLength(0); i++)
        {
            var tmp = 0;


            if(sizes[i, 0] < sizes[i, 1])
            {
                tmp = sizes[i, 0];
                sizes[i, 0] = sizes[i, 1];
                sizes[i, 1] = tmp;
            } 

/// 두 번째 변의 길이가 더 길면 swap 해준다.


        }
/// 배열 한쪽으로 사이즈 큰 거를 몰아줘야 한다.

         
        int max1 = 0;
        int max2 = 0;

        for (var i = 0; i < sizes.GetLength(0); i++)
        {
            if (max1 < sizes[i, 0]) max1 = sizes[i, 0];
            if (max2 < sizes[i, 1]) max2 = sizes[i, 1];
        }
        
        int answer = max1 * max2;
/// 거기서 큰 거를 양 배열에서 찾아서 곱하고 리턴하게 한다.
        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] answers) {
        int[] answer = new int[answers.Length];
        int[] one = new int[] {1,2,3,4,5};
        int[] two = new int[] {2,1,2,3,2,4,2,5};
        int[] three = new int[] {3,3,1,1,2,2,4,4,5,5};
        int[] score = new int[3];
        int max = 0;
        List<int> list = new List<int>();
        
        for(int i=0;i<answers.Length;i++)
        {
            if(one[i%5] == answers[i]) { score[0]++; }
            if(two[i%8] == answers[i]) { score[1]++; }
            if(three[i%10] == answers[i]) { score[2]++; }
        }

//문제의 정답과 나머지를 비교해서 맞으면 스코어를 플러스 한다.
        
        for (int i = 0; i < 3; i++)
        {
            if (max < score[i])
            {
                max = score[i];
            }
        }
// 스코어의 최댓값을 추출


        for (int i = 0; i < 3; i++)
        {
            if (max == score[i])
            {
                list.Add(i + 1);
            }
        }

// 최댓값이 같은 게 있으면 리스트에 추가해준다.


        return list.ToArray();
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

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

public class Solution {
    public int[] solution(int[] numbers) {
        List<int> list = new List<int>();
        
        for(int i=0;i<numbers.Length;i++)
        {
            for(int j=0;j<numbers.Length;j++)
            {
                if(i != j)
                {
                    list.Add(numbers[i]+numbers[j]);
                }
            }
        }
        
        int[] answer = new int[list.ToArray().Length];
        answer = list.Distinct().OrderBy(x => x).ToArray();

        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

public class Solution {
        public int LCM(int a,int b)
        {
            int n = a;
            int m = b;
            int temp = 0;
            
        while(m > 0)
        {
            temp = n%m;
            n = m;
            m = temp;
        }
        return (a*b)/n;
    }
        
    public int solution(int[] arr) 
    {
        int answer = LCM(arr[0],arr[1]);
        
        for(int i=2; i<arr.Length; i++)
        {
            answer = LCM(answer,arr[i]);
        }
        return answer;
    }
}

728x90

설정

트랙백

댓글