반응형
1. 포비와 크롱이 페이지 번호가 1부터 시작되는 400 페이지의 책을 주웠다. 책을 살펴보니 왼쪽 페이지는 홀수, 오른쪽 페이지는 짝수 번호이고 모든 페이지에는 번호가 적혀있었다. 책이 마음에 든 포비와 크롱은 페이지 번호 게임을 통해 게임에서 이긴 사람이 책을 갖기로 한다. 페이지 번호 게임의 규칙은 아래와 같다.
- 책을 임의로 펼친다.
- 왼쪽 페이지 번호의 각 자리 숫자를 모두 더하거나, 모두 곱해 가장 큰 수를 구한다.
- 오른쪽 페이지 번호의 각 자리 숫자를 모두 더하거나, 모두 곱해 가장 큰 수를 구한다.
- 2~3 과정에서 가장 큰 수를 본인의 점수로 한다.
- 점수를 비교해 가장 높은 사람이 게임의 승자가 된다.
- 시작 면이나 마지막 면이 나오도록 책을 펼치지 않는다.
포비와 크롱이 펼친 페이지가 들어있는 리스트/배열 pobi와 crong이 주어질 때, 포비가 이긴다면 1, 크롱이 이긴다면 2, 무승부는 0, 예외사항은 -1로 return 하도록 solution 메서드를 완성하라.
제한사항
- pobi와 crong의 길이는 2이다.
- pobi와 crong에는 [왼쪽 페이지 번호, 오른쪽 페이지 번호]가 순서대로 들어있다.
실행 결과 예시
pobicrongresult
[97, 98] | [197, 198] | 0 |
[131, 132] | [211, 212] | 1 |
[99, 102] | [211, 212] | -1 |
package onboarding;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
class Problem1 {
public static int solution(List<Integer> pobi, List<Integer> crong) {
// must decide which calculation produces greater value (multiplication vs. addition)
// must extract each digit
int pobiNumber = finalNumber(pobi);
int crongNumber = finalNumber(crong);
int answer;
if(pobiNumber==crongNumber) answer = 0;
else if(pobiNumber==-1 || crongNumber==-1) answer = -1;
else{
answer = (pobiNumber > crongNumber) ? 1 : 2;
}
return answer;
}
// must determine how many digits the number consists of
public static int digitNumber(int givenNum){
if(givenNum <= 1 || givenNum >=400) return -1;
int count = 0;
while(givenNum>0){
givenNum /= 10;
count++;
}
return count;
}
// then store each digit into different variables & determine between multiplication vs. addition
// return whichever one is greater
public static int greaterNumber(int num){
int digitNum = digitNumber(num);
int onesDigit = -1;
int tensDigit = -1;
int hundredsDigit = -1;
int addition = -1;
int multiplication = -1;
switch (digitNum){
case 1:
return 1;
case 2:
onesDigit = num % 10;
tensDigit = num / 10 % 10;
addition = onesDigit + tensDigit;
multiplication = onesDigit * tensDigit;
return Math.max(addition, multiplication);
case 3:
onesDigit = num % 10;
tensDigit = num / 10 % 10;
hundredsDigit = num/100;
addition = onesDigit + tensDigit + hundredsDigit;
multiplication = onesDigit * tensDigit * hundredsDigit;
return Math.max(addition, multiplication);
default:
return -1;
}
}
public static int finalNumber(List<Integer> list){
// if the list is empty, OR even page - odd page != 1, OR first number is not odd, OR second number is not even
// OR page number is either 1 or 400
if(list==null || list.get(1)-list.get(0)!=1 || list.get(0)%2!=1 || list.get(1)%2!=0 || greaterNumber(list.get(0))==-1 || greaterNumber(list.get(1))==-1){
return -1;
}
return Math.max( greaterNumber(list.get(0)), greaterNumber(list.get(1)) );
}
@Test
public static void case1(){
List<Integer> list1 = List.of(97,98);
List<Integer> list2 = List.of(197,198);
System.out.println(solution(list1, list2));
}
@Test
public static void case2(){
List<Integer> list1 = List.of(131,132);
List<Integer> list2 = List.of(211,212);
System.out.println(solution(list1, list2));
}
@Test
public static void case3(){
List<Integer> list1 = List.of(99,100);
List<Integer> list2 = List.of(397,398);
System.out.println(solution(list1, list2));
}
public static void main(String[] args) {
case1();
case2();
case3();
}
}
2. 암호문을 좋아하는 괴짜 개발자 브라운이 이번에는 중복 문자를 이용한 새로운 암호를 만들었다. 예를 들어 "browoanoommnaon"이라는 암호문은 다음과 같은 순서로 해독할 수 있다.
- "browoanoommnaon"
- "browoannaon"
- "browoaaon"
- "browoon"
- "brown"
임의의 문자열 cryptogram이 매개변수로 주어질 때, 연속하는 중복 문자들을 삭제한 결과를 return 하도록 solution 메서드를 완성하라.
제한사항
- cryptogram은 길이가 1 이상 1000 이하인 문자열이다.
- cryptogram은 알파벳 소문자로만 이루어져 있다.
실행 결과 예시
cryptogramresult
"browoanoommnaon" | "brown" |
"zyelleyz" | "" |
package onboarding;
import java.util.ArrayList;
import java.util.List;
public class Problem2 {
public static String solution(String cryptogram) {
List<Character> chars = new ArrayList<>();
for(char c: cryptogram.toCharArray()){
chars.add(c);
}
remove(chars);
StringBuilder sb = new StringBuilder();
for(Character c: chars){
sb.append(c);
}
String answer = sb.toString();
return answer;
}
// public static void main(String[] args) {
// System.out.println("Start ------");
// System.out.println(solution("appale"));
// System.out.println("End ------");
// }
public static void remove(List<Character> charArr){
List<Character>newArr = charArr;
for(int i=0; i<charArr.size()-1; i++){
if(charArr.get(i).equals(charArr.get(i+1))){
charArr.remove(i);
charArr.remove(i);
remove(newArr);
}
}
}
}
3. 배달이가 좋아하는 369게임을 하고자 한다. 놀이법은 1부터 숫자를 하나씩 대면서, 3, 6, 9가 들어가는 숫자는 숫자를 말하는 대신 3, 6, 9의 개수만큼 손뼉을 쳐야 한다.
숫자 number가 매개변수로 주어질 때, 1부터 number까지 손뼉을 몇 번 쳐야 하는지 횟수를 return 하도록 solution 메서드를 완성하라.
제한사항
- number는 1 이상 10,000 이하인 자연수이다.
실행 결과 예시
numberresult
13 | 4 |
33 | 14 |
package onboarding;
public class Problem3 {
public static int solution(int number) {
if(number <1 || number >10000) return -1;
if(number<3) return 0;
int answer = 0;
for(int i=3; i<=number; i++){
answer += factorsOfThree(i);
}
return answer;
}
public static int factorsOfThree(int number){
int count = 0;
while(number>=1){
int onesDigit = number % 10;
if(onesDigit%3==0 && onesDigit!=0) count++;
number /= 10;
}
return count;
}
public static void main(String[] args) {
System.out.println(solution(40));
}
}
반응형
'테크스토리' 카테고리의 다른 글
인공지능 AI 우리 삶에 미치는 영향? (0) | 2022.12.18 |
---|---|
자바 제네릭(Generic) 공부 후기 (0) | 2022.10.22 |
Interface & Abstract Class 첼린지 후기 (0) | 2022.10.21 |
LinkedList 자신있으면 도전해봐~ (LinkedList Challenge 후기) (2) | 2022.10.11 |
*LinkedList* 어디까지 알고있니? (feat. Iterator & ConcurrentModificationException) (2) | 2022.10.08 |
댓글