Programmer:)
[ 프로그래머스 / C# ] 로또의 최고 순위와 최저 순위(for/foreach) 본문
반응형
문제
https://programmers.co.kr/learn/courses/30/lessons/77484
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
성능 비교해보려고 for, foreach 로 풀어봤다.
using System;
public class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int sameValue = 0;
int zeroValue = 0;
int[] rank = new int[7]{6,6,5,4,3,2,1};
foreach(var i in lottos)
{
if(i == 0)
{
zeroValue++;
continue;
}
foreach(var j in win_nums)
{
if(j == i)
{
sameValue++;
break;
}
}
}
int max = sameValue + zeroValue;
int min = sameValue;
int[] answer = new int[2]{rank[max], rank[min]};
return answer;
}
}
using System;
public class Solution {
public int[] solution(int[] lottos, int[] win_nums)
{
int sameValue = 0;
int zeroValue = 0;
int[] rank = new int[7]{6,6,5,4,3,2,1};
for(int i=0; i <lottos.Length; i++)
{
if(lottos[i] == 0)
{
zeroValue++;
continue;
}
for(int j=0; j<win_nums.Length;j++)
{
if(lottos[i] == win_nums[j])
{
sameValue++;
break;
}
}
}
int min = sameValue;
int max = min + zeroValue;
int[] answer = new int[2]{rank[max], rank[min]};
return answer;
}
}
for문이 살짝 더 빠른..
반응형
'프로그래머스 정복 > C#' 카테고리의 다른 글
[ 프로그래머스 / C# ] 과일 장수 (0) | 2022.11.16 |
---|---|
[ 프로그래머스 / C# ] K번째 수 (0) | 2022.03.29 |
[ 프로그래머스 / C# ] 음양 더하기 (0) | 2021.10.25 |
[ 프로그래머스 / C# ] 없는숫자 더하기(Except) (0) | 2021.10.22 |
[ 프로그래머스 / C# ] 숫자 문자열과 영단어(2021 카카오 채용 연계 인턴쉽)(Dictionary) (0) | 2021.10.22 |
Comments