목록Coding test(Python3)/Programmers (115)
NIRVANA
[level 1] 나머지가 1이 되는 수 찾기
문제 자연수 n이 매개변수로 주어집니다. n을 x로 나눈 나머지가 1이 되도록 하는 가장 작은 자연수 x를 return 하도록 solution 함수를 완성해주세요. 답이 항상 존재함은 증명될 수 있습니다. 문제 풀이 접근법 1 처음에는 다음과 같은 알고리즘으로 접근했었다. 1) n 입력 받음 2) for문에서 i를 1씩 증가 → n을 i로 나누었을 때, 나머지가 1인지 확인 3) 만약 나머지가 1이면 해당 i값을 return *i를 1부터 1씩 증가하므로 처음으로 나머지가 1이 나오는 수가 가장 작은 x일 것 하지만 결과 값이 예상대로 나오지 않았다. 원래라면 3*3 = 9이므로 n이 10일 때, 나머지가 1이되게 하는 가장 작은 자연수 x는 3이지만 위의 알고리즘에서는 3*1=3이므로 10을 3으로 나..
Coding test(Python3)/Programmers
2023. 7. 4. 15:47