//구글콘솔 광고 추가가

이문제는 두 점을 이용해서 기울기를 구하는 방법를 알고 있어야 한다. 당연하게도 평행이 되려면 기울기가 같으면 됨.

기울기가 m , y절편이 n인 직선의 방정식 >> y = mx +n
직선의 두점 (x1, y1), (x2, y2)를 지날때,
기울기 = (y값의 증가량) / (x값의 증가량) == (y1 -y2) / (x1- x2)

평행 문제

내 코드
using System;

public class Solution { //기울기 구하기
    public int solution(int[,] dots) {
        if((double)(dots[0,0] - dots[1,0]) / (dots[0,1] - dots[1,1]) == (double)(dots[2,0] - dots[3,0]) / (dots[2,1] - dots[3,1]))
        {
            return 1;
        }
        if((double)(dots[0,0] - dots[2,0]) / (dots[0,1] - dots[2,1]) == (double)(dots[1,0] - dots[3,0]) / (dots[1,1] - dots[3,1]))
        {
            return 1;
        }
        if((double)(dots[0,0] - dots[3,0]) / (dots[0,1] - dots[3,1]) == (double)(dots[1,0] - dots[2,0]) / (dots[1,1] - dots[2,1]))
        {
            return 1;
        }
        return 0;
    }
}
728x90
옹알이 (1) 문제

내 코드
using System;

public class Solution {
    public int solution(string[] babbling) {
        int answer = 0;
        string[] ableTalk = {"aya", "ye", "woo", "ma"};
        for (int i = 0; i < babbling.Length; i++)
        {
            for (int j = 0; j < ableTalk.Length; j++)
            {
                babbling[i] = babbling[i].Replace(ableTalk[j], "H");
            }

            babbling[i] = babbling[i].Replace('H'.ToString(),"");
            if (babbling[i] == "") answer++;
        }        
        return answer;
    }
}
728x90
무작위로 K개의 수 뽑기 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int[] solution(int[] arr, int k) {
        var answer = new List<int>(arr).Distinct().ToList();
        var answerArr = new List<int>();
        for(int i =0; i < k; i++)
        {
            if(i < answer.Count)
                answerArr.Add(answer[i]);
            else
                answerArr.Add(-1);
        }
        return answerArr.ToArray();
    }
}

 

728x90
전국 대회 선발 고사 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int solution(int[] rank, bool[] attendance) {
        int answer = 0;
        Dictionary<int, int> dic = new Dictionary<int, int>();
        for (int i = 0; i < rank.Length; i++)
        {
            if (attendance[i])
                dic.Add(rank[i], i);
        }
        List<int> list = dic.Keys.ToList();
        list.Sort();
        answer = dic[list[0]] * 10000 + dic[list[1]] * 100 + dic[list[2]];
        return answer;
    }
}
728x90
수열과 구간 쿼리 2 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int[] solution(int[] arr, int[,] queries) {
        var answer = new List<int>();
        for (int i = 0; i < queries.GetLength(0); i++)
        {
            int min = arr.Max()+1;
            for (int j = queries[i, 0]; j <= queries[i, 1]; j++)
            {
                if (arr[j] > queries[i, 2] && arr[j]< min)
                    min = arr[j];
            }
            if (min == arr.Max() + 1) min = -1;
            answer.Add(min);
        }
        return answer.ToArray();
    }
}
728x90
배열 조각하기 문제

 

내 코드
using System;
using System.Collections.Generic;
public class Solution {
    public int[] solution(int[] arr, int[] query) {
        List<int> answer = new List<int>(arr);
        for (int i = 0; i < query.Length; i++)
        {
            //012345 //412 >> 4 //01234 // >>1 //1234 // >>2 //123
            if (i % 2 == 0)
            {
                for (int j = answer.Count-1; j > query[i]; j--)
                {
                    answer.RemoveAt(j);
                }
            }
            else
            {
                for (int j = 0; j < query[i]; j++)
                {
                    answer.RemoveAt(0);
                }
            }
            arr = answer.ToArray();
        }
        return answer.ToArray();
    }
}

 

728x90
정수를 나선형으로 배치하기 문제

 

내 코드
using System;
//+1
public class Solution {
    public int[,] solution(int n) {
        int[,] answer = new int[n,n];
        string [,] answer2 = new string[n,1];
        int num = 1;
        for (int x = 0; x < n; x++)
        {
            for (int y = 0; y < n; y++)
            {
                answer[x,y] = -1;
            }
        }

        for (int i = 0; i < n; i++)
        {
            //왼 >> 오
            for (int r = i; r < n - i; r++)
            {
                if (answer[i,r] == -1)
                    answer[i,r] = num++;
            }
            //위 >> 아래
            for (int d = 1; d < n - i; d++)
            {
                if (answer[d,n - i - 1] == -1)
                    answer[d,n - i - 1] = num++;
            }
            //오 >> 왼
            for (int l = n - i - 1; 0 <= l; --l)
            {
                if (answer[n - i - 1,l] == -1)
                    answer[n - i - 1,l] = num++;
            }
            //아래 >> 위
            for (int u = n - i - 1; 0 < u; --u)
            {
                if (answer[u - 1,i] == -1)
                    answer[u - 1,i] = num++;
            }
        }

        for(int x =0; x < n; x++)
        {
            for(int y =0; y < n; y++)
            {
                if (y != 0) answer2[x, 0] += ",";
                
                answer2[x, 0] += + answer[x, y];
            }
        }
        return answer;
    }
}

 

728x90

+ Recent posts