코딩테스트
코딩 테스트 : 다음에 올 숫자, 연속된 수의 합, 옹알이
Cadi
2025. 1. 14. 11:02
코딩테스트 : 다음에 올 숫자
등차수열 혹은 등비수열 common이 매개변수로 주어질 때, 마지막 원소 다음으로 올 숫자를 return 하도록 solution 함수를 완성해보세요.
using System;
public class Solution {
public int solution(int[] common)
{
int answer = 0;
if (common[2]-common[1] == common[1]-common[0])
{
return common[common.Length-1] + common[2] - common[1] ;
}
else
{
int a =common[2]/common[1];
return common[common.Length-1] * a ;
}
return answer;
}
}
이번엔 처음으로 IDE 쓰지 않고 처음으로 풀어봤다.
논리는 어려운 것이 없었다 !
코딩테스트 : 연속된 수의 합
연속된 세 개의 정수를 더해 12가 되는 경우는 3, 4, 5입니다. 두 정수 num과 total이 주어집니다. 연속된 수 num개를 더한 값이 total이 될 때, 정수 배열을 오름차순으로 담아 return하도록 solution함수를 완성해보세요.
using System;
public class Solution {
public int[] solution(int num, int total)
{
int[] answer = new int[num];
if (total%num == 0)
{
int odd = total/num;
odd = odd - (num-1)/2;
for (int i = 0; i < num; i++ )
{
answer[i] = odd;
odd++;
}
}
else
{
int even = total/num;
even = even - (num-2)/2;
for (int i = 0; i < num; i++ )
{
answer[i] = even;
even++;
}
}
return answer;
}
}
점점 한 번에 클리어 되는 문제가 많아졌다.
다만 이 경우, 코가 겹치는 부분이 분명히 있어 이를 깔끔하게 줄일 수 있을 것 같다.
다른 사람의 흥미로운 풀이
생각대로 위 코드의 논리에서 크게 벗어나지 않고, 정리를 잘 하신 분들이 많았다.
코딩테스트 : 옹알이
머쓱이는 태어난 지 6개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음을 최대 한 번씩 사용해 조합한(이어 붙인) 발음밖에 하지 못합니다. 문자열 배열 babbling이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.
using System;
public class Solution {
public int solution(string[] babbling)
{
int answer = 0;
for (int i = 0; i < babbling.Length; i ++)
{
babbling[i] = Replace(babbling[i]);
if (babbling[i].Length < 1)
{
answer++;
}
}
return answer;
}
public string Replace(string str)
{
//원래는 지우려 했음, 근데 지워서 말이 되는 경우가 있을수 있으므로 1로 바꿈
str = str.Replace("aya","1");
str = str.Replace("ye","1");
str = str.Replace("woo","1");
str = str.Replace("ma","1");
str = str.Replace("1","");
return str;
}
}
다른 사람의 흥미로운 풀이
생각보다 잘..푼 듯 .. ?