글
프로그래머스 C# 평행(GetLength, Distinct, float-소수, Count, JAVA)
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public int solution(int[,] dots)
{
List<float> slopes = new List<float>();
int length = dots.GetLength(0);
for (int i = 0; i < length; i++)
{
for (int j = i + 1; j < length; j++)
{
slopes.Add((dots[i, 1] - dots[j, 1]) / (float)(dots[i, 0] - dots[j, 0]));
/// 경우의 수가 length * (length -1) / 2
}
}
return slopes.Distinct().Count() == length * (length -1) / 2 ? 0 : 1;
}
}
/// Distinct로 기울기를 집어 넣은 게 중복으로 사라지게 되면, 기울기가 같은 게 있으면 1을 출력 (평행) 그렇지 않으면 0을 출력
자바
class Solution {
public int solution(int[][] dots) {
int x1 = dots[3][0];
int y1 = dots[3][1];
for (int i = 0; i < 3; i++)
{
int x2 = dots[i][0];
int y2 = dots[i][1];
int x3 = dots[(i+1)%3][0];
int y3 = dots[(i+1)%3][1];
int x4 = dots[(i+2)%3][0];
int y4 = dots[(i+2)%3][1];
if(x1 == x2 || x3 == x4)
{
if(x1 == x2 && x3 == x4)
{
return 1;
}
continue;
}
if((y2-y1)/(double)(x2-x1)==(y4-y3)/(double)(x4-x3))
{
return 1;
}
}
return 0;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 C# 올바른 괄호(시간 최소화, 괄호 알고리즘, JAVA) (0) | 2023.01.21 |
---|---|
프로그래머스 C# 가운데 글자(string 몇 번째 문자 가져오기, JAVA) (0) | 2023.01.21 |
프로그래머스 C# 크기가 작은 부분 문자열(long.Parse, JAVA parseLong, substring) (0) | 2023.01.21 |
프로그래머스 C# 연속된 수의 합(Array.Fill, JAVA) (0) | 2023.01.20 |
프로그래머스 C# 옹알이(1) (문자열 Replace의 활용, JAVA) (0) | 2023.01.20 |