sm 기술 블로그

86. 1620(나는야 포켓몬 마스터 이다솜) 본문

문제/백준_자바스크립트

86. 1620(나는야 포켓몬 마스터 이다솜)

sm_hope 2022. 6. 19. 15:57
let input = require('fs').readFileSync(0).toString().trim().split("\n");

let tmp = input[0].split(" ").map(Number);

input.shift();

let PokemonDoGam_str = new Object();
let PokemonDoGam_num = new Object();
let Question = [];
let result = ""

for(let i =0;i<tmp[0];i++){
  PokemonDoGam_str[input[i].trim()] = (i+1).toString(); 
  PokemonDoGam_num[(i+1).toString()] = input[i].trim();
}

for(let j = tmp[0]; j < tmp[0]+tmp[1]; j++){
  Question.push(input[j].trim());
}

for(let val of Question){
    if(isNaN(val)){ //문자열이 알파벳이라면 true
      result += PokemonDoGam_str[val]+"\n";
    }
    else {
      result += PokemonDoGam_num[val]+"\n";
    }
}

console.log(result);

문제요약

다솜이는 포켓몬마스터가 되기 위해 오박사가 내는 문제를 맞춰야한다.

숫자가 들어오면 포켓몬 이름을, 포켓몬 이름이 들어오면 숫자를 말해라.

설명

문제 설명보다 잡소리가 긴 문제로 이런 병맛을 좋아하는 나에게는 취향저격이다.

빨간 선을 기준으로 위에는 포켓몬 도감 아래는 문제이다.

for(let i =0;i<tmp[0];i++){
  PokemonDoGam_str[input[i].trim()] = (i+1).toString(); 
  PokemonDoGam_num[(i+1).toString()] = input[i].trim();
}

먼저 입력 받은 값으로 숫자가 key, 알파벳(문자)가 key인 두가지 경우의 객체를 만들어주었다.

 

나머지 입력값은 문제로 Question 배열에 넣어 주었다.

(바로 쓸 수도 있지만 편의상 배열에 넣었다.)

 

for(let val of Question){
    if(isNaN(val)){ //문자열이 알파벳이라면 true
      result += PokemonDoGam_str[val]+"\n";
    }
    else {
      result += PokemonDoGam_num[val]+"\n";
    }
}

isNaN( value )은 value가 숫자 문자열인지, 알파벳문자열인지 판별해주는 내장함수이다.

만약 문자열이 알파벳이면 true를 숫자면 false를 반환한다.

 

조건문을 사용하여 알파벳, 숫자 어느것이 입력되느냐에 따라 적합한 객체를 이용할 수 있도록 해 주었다.  

'문제 > 백준_자바스크립트' 카테고리의 다른 글

88. 1764(듣보잡)  (0) 2022.06.20
87. 10816(숫자카드2)  (0) 2022.06.20
85. 14425(문자열 집합)  (0) 2022.06.19
84. 10815(숫자 카드)  (0) 2022.06.19
83. 18870 (좌표압축)  (0) 2022.06.18
Comments