반응형
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
반응형
'IT > 알고리즘' 카테고리의 다른 글
19★] Leetcode 1436. Destination City (0) | 2020.06.30 |
---|---|
18] Leetcode 1475. Final Prices With a Special Discount in a Shop (0) | 2020.06.29 |
16] Leetcode 1252. Cells with Odd Values in a Matrix (0) | 2020.06.27 |
15] Leetcode 1464. Maximum Product of Two Elements in an Array (0) | 2020.06.25 |
14] Leetcode 1221. Split a String in Balanced Strings (0) | 2020.06.25 |
댓글