diff --git a/src/problem1.js b/src/problem1.js index 9a14f2c..799c7a5 100644 --- a/src/problem1.js +++ b/src/problem1.js @@ -1,6 +1,42 @@ function problem1(pobi, crong) { var answer; - return answer; + var pobi_max, crong_max ; + + if (pobi[0] % 2 == 0 || pobi[1] % 2 == 1 || crong[0] % 2 == 0 || crong[1] % 2 == 1 || pobi[1] - pobi[0] != 1 || crong[1] - crong[0] != 1) { + return -1; + } + + pobi_1 = pobi[0].toString(); pobi_2 = pobi[1].toString(); + crong_1 = crong[0].toString(); crong_2 = crong[1].toString(); + pobi_max = Math.max(Math.max(plus(pobi_1), plus(pobi_2)), Math.max(multi(pobi_1), multi(pobi_2))) + crong_max = Math.max(Math.max(plus(crong_1), plus(crong_2)), Math.max(multi(crong_1), multi(crong_2))) + + if (pobi_max > crong_max) { + return 1; + } else if (pobi_max < crong_max) { + return 2; + } else { + return 0; + } +} + +function plus(now_num) { + var res = 0; + + for (i = 0;i < now_num.length;i++) { + res += parseInt(now_num.substring(i, i + 1)); + } + + return res; } +function multi(now_num) { + var res = 1; + + for (i = 0;i < now_num.length;i++) { + res *= parseInt(now_num.substring(i, i + 1)); + } + + return res; +} module.exports = problem1; diff --git a/src/problem2.js b/src/problem2.js index cebd07c..4e5e1b2 100644 --- a/src/problem2.js +++ b/src/problem2.js @@ -1,6 +1,22 @@ function problem2(cryptogram) { - var answer; - return answer; + var pre_str = cryptogram ; + var now_str ; + while (1) { + now_str = search_str(pre_str) + if (now_str == pre_str || now_str.length == 0) { + return now_str ; + } + pre_str = now_str; + } +} + +function search_str(now_str) { + for (var i = 0;i < now_str.length - 1;i++) { + if (now_str[i] == now_str[i + 1]) { + return now_str.slice(0, i) + now_str.slice(i + 2); + } + } + return now_str; } module.exports = problem2; diff --git a/src/problem3.js b/src/problem3.js index 1baed28..597a038 100644 --- a/src/problem3.js +++ b/src/problem3.js @@ -1,6 +1,16 @@ function problem3(number) { - var answer; - return answer; + var res = 0; + for (var i = 1;i <= number;i++) { + now_str = i.toString(); + res += search_369(now_str, '3'); + res += search_369(now_str, '6'); + res += search_369(now_str, '9'); + } + return res; +} + +function search_369(str, num) { + return str.split(num).length - 1 ; } module.exports = problem3; diff --git a/src/problem4.js b/src/problem4.js index ee1d3bd..e033e2c 100644 --- a/src/problem4.js +++ b/src/problem4.js @@ -1,6 +1,19 @@ function problem4(word) { - var answer; - return answer; + var res_str = ''; + var now_ascii = 0; + for (i = 0;i < word.length;i++) { + if (word[i] == ' ') { + res_str += ' '; + } else { + now_ascii = word.charCodeAt(i); + if (now_ascii < 91) { + res_str += String.fromCharCode(90 - (now_ascii - 65)); + } else { + res_str += String.fromCharCode(122 - (now_ascii - 97)); + } + } + } + return res_str ; } module.exports = problem4; diff --git a/src/problem5.js b/src/problem5.js index 9368e87..3a1bbe9 100644 --- a/src/problem5.js +++ b/src/problem5.js @@ -1,6 +1,15 @@ function problem5(money) { - var answer; - return answer; + var res_list = [0, 0, 0, 0, 0, 0, 0, 0, 0] ; + var money_list = {0 : 50000, 1 : 10000, 2 : 5000, 3 : 1000, 4 : 500, 5 : 100, 6 : 50, 7 : 10, 8 : 1} ; + var now_won = 0 ; + + for (i = 0;i < 9;i++) { + now_won = money_list[i] ; + res_list[i] = Math.floor(money / now_won) ; + money %= now_won ; + } + + return res_list ; } module.exports = problem5; diff --git a/src/problem6.js b/src/problem6.js index 3f842b5..b329634 100644 --- a/src/problem6.js +++ b/src/problem6.js @@ -1,6 +1,33 @@ function problem6(forms) { - var answer; - return answer; + var dupli_list = new Array(forms.length).fill(0) ; + var res_list = [] ; + for (var i = 0;i < forms.length;i++) { + for (var j = i + 1;j < forms.length;j++) { + if (dupli_search(forms[i][1], forms[j][1]) == true) { + if (dupli_list[i] == 0) { + res_list.push(forms[i][0]); + dupli_list[i] = 1; + } + if (dupli_list[j] == 0) { + res_list.push(forms[j][0]); + dupli_list[j] = 1; + } + } + } + } + res_list.sort() + return res_list; +} + +function dupli_search(str1, str2) { + for (var i = 0;i < str1.length - 1;i++) { + for (var j = 0;j < str2.length - 1;j++) { + if (str1.slice(i, i + 2) == str2.slice(j, j + 2)) { + return true ; + } + } + } + return false ; } module.exports = problem6; diff --git a/src/problem7.js b/src/problem7.js index ee1bb9d..f590035 100644 --- a/src/problem7.js +++ b/src/problem7.js @@ -1,6 +1,53 @@ function problem7(user, friends, visitors) { - var answer; - return answer; + let user_friends = []; + let score_dict = {}; + + for (let i = 0; i < friends.length; i++) { + let f0 = friends[i][0]; + let f1 = friends[i][1]; + if (f0 === user) { + user_friends.push(f1); + } else if (f1 === user) { + user_friends.push(f0); + } + } + + for (let i = 0; i < friends.length; i++) { + let f0 = friends[i][0]; + let f1 = friends[i][1]; + if (f0 === user || f1 === user) {continue;} + + if (user_friends.includes(f0)) { + if (score_dict[f1]) { + score_dict[f1] -= 10; + } else { + score_dict[f1] = -10; + } + } else if (user_friends.includes(f1)) { + if (score_dict[f0]) { + score_dict[f0] -= 10; + } else { + score_dict[f0] = -10; + } + } + } + + for (let i = 0; i < visitors.length; i++) { + let now_visitors = visitors[i]; + if (!user_friends.includes(now_visitors)) { + if (score_dict[now_visitors]) { + score_dict[now_visitors] -= 1; + } else { + score_dict[now_visitors] = -1; + } + } + } + + let res_list = Object.entries(score_dict) + .sort((a, b) => a[1] - b[1]) + .map(item => item[0]) ; + + return res_list; } module.exports = problem7;