add: 220307-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-03-07 07:51:15 +08:00
parent abb3305568
commit 6cfc7529fd
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 24 additions and 1 deletions

23
cpp/2203/220307-CN.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <string>
/**
* 504. Base 7
* Given an integer num, return a string of its base 7 representation.
*/
class Solution {
public:
static std::string convertToBase7(int num) {
bool isNeg = num < 0;
std::string ret;
num = num < 0 ? -num : num;
while (num) {
ret.push_back('0' + num % 7);
num /= 7;
}
if (isNeg)
ret.push_back('-');
std::reverse(ret.begin(), ret.end());
return ret.empty() ? "0" : ret;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2203)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2203 220306.cpp) ADD_EXECUTABLE(2203 220307-CN.cpp)