-
1157. 단어 공부Baekjoon 2023. 4. 9. 19:52
문제
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
입력
첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
Mississipi출력
첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
?에러
더보기let fs = require("fs"); const readFileSyncAdd = "1157.txt"; const input = fs.readFileSync(readFileSyncAdd).toString().toUpperCase(); const words = input.split(''); let count = 0; let res = 0; for(let i = 65; i <= 90; i++){ let now_count = 0; let now_alpa = String.fromCharCode(i); for(let j = 0; j < words.length; j++){ if (words[j] == now_alpa) { now_count++; } } if (now_count > count) { res = i; } else if (now_count == count) { res = 63; } count = now_count; } console.log(String.fromCharCode(res));난 두번 틀렸는데, 처음에 수정했을 때 내가 생각한 틀린 이유는
어떤 것을 입력했을 때, 그걸 무조건 받는 것이 문제라고 생각했다.
입력된 것이 알파벳인지 아닌지 검사하고 알파벳이라면 받는 코드를 추가했다.
let fs = require("fs"); const readFileSyncAdd = "1157.txt"; const input = fs.readFileSync(readFileSyncAdd).toString().toUpperCase(); const words = input.split('').map(v=>{ let temp = v.charCodeAt(0); if (temp >= 65 && temp <= 91) return String.fromCharCode(temp); }); let count = 0; let res = 0; for(let i = 65; i <= 90; i++){ let now_count = 0; let now_alpa = String.fromCharCode(i); for(let j = 0; j < words.length; j++){ if (words[j] == now_alpa) { now_count++; } } if (now_count > count) { res = i; } else if (now_count == count) { res = 63; } count = now_count; } console.log(String.fromCharCode(res));그랬는데도 틀렸다. 그래서 여기저기 콘솔로그 찍으면서 원인을 찾아봤다. 입력도 바꿔보고.
그랬더니 뭘 입력해도 출력이 ?가 나오는 것이 아닌가?
이유는 a부터 z까지 돌면서 비교하는데, 개수가 0인 알파벳이 많을 것 아닌가.
개수가 0인 알파벳도 개수가 같다고 판단해서, 개수가 같으면 ?을 출력하니까 ?가 나왔던 것.
그래서 최최최종으로 수정해봤다.
let fs = require("fs"); const readFileSyncAdd = "1157.txt"; const input = fs.readFileSync(readFileSyncAdd).toString().toUpperCase(); const words = input.split('').map(v=>{ let temp = v.charCodeAt(0); if (temp >= 65 && temp <= 91) return String.fromCharCode(temp); }); let count = 0; let res = 0; for(let i = 65; i <= 90; i++){ let now_count = 0; let now_alpa = String.fromCharCode(i); for(let j = 0; j < words.length; j++){ if (words[j] == now_alpa) { now_count++; } } if (now_count > count) { res = i; count = now_count; } else if (now_count == count) { res = 63; } } console.log(String.fromCharCode(res));count에 now_count를 넣는 것은 now_count > count 일때만 실행되어야한다.
계속 실행되면 위에서 말했듯, 개수가 0일때 문제가 생기기 때문에...
(now_count > count는 어쨋든 count가 최소 1이상 이라는거니까.)
결과는 맞았습니다!! 신난다
제출 코드
let fs = require("fs"); const readFileSyncAdd = "1157.txt"; const input = fs.readFileSync(readFileSyncAdd).toString().toUpperCase(); const words = input.split('').map(v=>{ let temp = v.charCodeAt(0); if (temp >= 65 && temp <= 91) return String.fromCharCode(temp); }); let count = 0; let res = 0; for(let i = 65; i <= 90; i++){ let now_count = 0; let now_alpa = String.fromCharCode(i); for(let j = 0; j < words.length; j++){ if (words[j] == now_alpa) { now_count++; } } if (now_count > count) { res = i; count = now_count; } else if (now_count == count) { res = 63; } } console.log(String.fromCharCode(res));'Baekjoon' 카테고리의 다른 글
11655. ROT13 (0) 2023.05.02 10845. 큐 (0) 2023.05.02 10820. 문자열 분석 (0) 2023.05.02 9093. 단어 뒤집기 (0) 2023.04.09 10828. 스택 (0) 2023.04.09