본문 바로가기
IT/알고리즘

17] Leetcode 1323. Maximum 69 Number

by 깻잎쌈 2020. 6. 29.
반응형

6과 9로 이뤄진 숫자가 주어졌을 때 

단 하나의 6을 9로 바꿨을 때의 최댓값을 리턴하는 문제

 

Maximum 69 Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

주어진 숫자에서 6이 있는 가장 큰 자릿수를 구해서 

그 자릿수에 3을 더 해준다 

pow함수 사용 

class Solution {
public:
    int maximum69Number (int num) {
        int max6= -1; // 6이 있는 최대 자릿수
        int ans = num;
        
        for(int cnt = 0; num!=0; cnt++){
             if(num%10 == 6)
                max6 = cnt;           
            
            num /=10;
        }               
        
        return ans+ 3*pow(10, max6); 
    }
};

http://www.cplusplus.com/reference/cmath/pow/

 

pow - C++ Reference

1234567891011 /* pow example */ #include /* printf */ #include /* pow */ int main () { printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) ); printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) ); printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) ); return 0; }

www.cplusplus.com

 

반응형

댓글