쉬운 문제라도 문제를 푸는 방법은 다양합니다.
더 명확하고 깔끔한 문제 해결 방법을 아신다면 자유롭게 말씀해주세요.
프로그래머스 120924 다음에 올 숫자
프로그래머스 저작권을 침해하지 않기 위해
문제 보시고 싶으시면 버튼을 눌러주세요.
답변 형식
common 배열을 받아 원소들의 관계를 등차수열 또는 등비수열로 판단한다.
등비 또는 등차의 크기를 구하여 마지막 원소 다음에 나올 숫자를 찾는다.
풀이 조건
최소 3개 이상의 원소가 나오기 때문에
– 1항과 2항
– 2항과 3항의 관계를
확인하면 됩니다.
배열의 크기가 999까지 가능하지만
종항의 다음 항이 int 범위 내에 있기 때문에 문제를 풀 때 형 크기에 대한 부담은 없습니다.
JAVA 정답
class Solution {
public int solution(int[] common) {
int answer = 0;
int size = common.length;
int lastnum = size-1;
if(common[1]-common[0] == common[2]-common[1])
answer = common[lastnum]+(common[1] – common[0]);
else
answer = common[lastnum]*(common[1]/common[0]);
return answer;
}
}
JAVA 정답 해설
class Solution {
public int solution(int[] common) {
int answer = 0;
int size = common.length;
/// 배열의 크기를 구한다.
int lastnum = size-1;
/// 배열의 크기가 10이라면 마지막 항은 9항이다.(배열은 0항 시작)
if(common[1]-common[0] == common[2]-common[1])
/// 1항과 0항의 뺄셈 값 차이가 2항과 1항의 뺄셈 값 차이와 같다면 등차 수열로 판단한다.
answer = common[lastnum]+(common[1] – common[0]);
/// 등차수열이라면 마지막 항에 등차를 더해서 제출한다.
else
/// 배열이 등차수열이 아니라면 등비수열이다.
answer = common[lastnum]*(common[1]/common[0]);
/// 등비수열이라면 마지막 항에 등비를 곱해서 제출한다.
return answer;
}
}
JAVA 간결한 정답
class Solution {
public int solution(int[] common) {
if(common[1]-common[0] == common[2]-common[1])
return common[common.length–1]+(common[1] – common[0]);
else return common[common.length–1]*(common[1]/common[0]);
}
}
JAVA 삼항연산자
class Solution {
public int solution(int[] common) {
return common[1]-common[0] == common[2]-common[1] ?
common[common.length–1]+(common[1] – common[0]) :
common[common.length–1]*(common[1]/common[0]);
}
}
Python3 정답
def solution(common):
if common[1] – common[0] == common[2] – common[1] :
return common[-1]+(common[1] – common[0])
else :
return common[-1]*(common[1]/common[0])
Python3 정답 해설
def solution(common):
if common[1] – common[0] == common[2] – common[1] :
# 1항과 0항의 뺄셈 값 차이가 2항과 1항의 뺄셈 값 차이와 같다면 등차 수열로 판단한다.
return common[-1]+(common[1] – common[0])
# 등차수열이라면 마지막 항에 등차를 더해서 제출한다.
else :
# 배열이 등차수열이 아니라면 등비수열이다.
return common[-1]*(common[1]/common[0])
# 등비수열이라면 마지막 항에 등비를 곱해서 제출한다.


The app interface is super smooth and the login process is a breeze. I really love how fast the games load. Definitely a great choice for anyone looking for a reliable platform. Check out 666jililoginapp
The payouts here are actually fair and the interface is super clean. It is my new go-to spot for gaming. Give br667bet43 a try
Just joined a few days ago and I am already loving the community and the rewards. It feels like a very welcoming place. Check out vn15uy88 today.
The variety of games here is insane! I felt like I was right in Vegas from my own couch. Highly recommend balivegasslot for anyone who loves the thrill of the slots.
The iOS version is incredibly stable and the graphics are stunning. It is a game changer for mobile players. Try hitclubios yourself.
This place has a great vibe and the winning streaks are real. I’ve tried a few others but this one feels more rewarding. Definitely visit pkwof777.
The winning streaks here are insane. I managed to hit a big jackpot last night and the withdrawal was processed instantly. High praise for krcasinowinoui
[3407]Gembet Login Philippines | Mabilis na GCash Casino Slots,Gamitin ang gembet login philippines para sa mabilis na access. I-download ang gembet app download APK at subukan ang legit na online casino payout. Sali na! visit: gembet
Finally found a platform that does not lag during peak hours. The login process is seamless and I got my bonus right away. Great vibes all around at ck777betlogin
The odds on this site are way better than what I usually find. I did a few live bets this weekend and the experience was seamless. Cheers to 1pra1betsite for the great service.