본문 바로가기
Portpolio/codingtest

프로그래머스 9로 나눈 나머지

by Peter Choi 2025. 2. 3.
반응형

 

https://school.programmers.co.kr/learn/courses/30/lessons/181914

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

자바

class Solution {
    public int solution(String number) {
        int sum = 0;
        
        for (char ch: number.toCharArray()) {
            sum += Character.getNumericValue(ch);
        }
        
        return sum % 9;
    }
}

 

자바에서는 Character 패키지의 스태틱 메서드인 getNumericValue()를 사용해서 풀이하는 방법을 사용했습니다.

public static int getNumericValue(int codePoint)
Returns the int value that the specified character (Unicode code point) represents. For example, the character '\u216C' (the Roman numeral fifty) will return an int with a value of 50.

 

The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.

If the character does not have a numeric value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for example, a fractional value), then -2 is returned.

 

Parameters:codePoint - the character (Unicode code point) to be converted.

 

Returns:the numeric value of the character, as a nonnegative int value; -2 if the character has a numeric value but the value can not be represented as a nonnegative int value; -1 if the character has no numeric value.Since:1.5

 

 

자바스크립트

 

반응형

댓글