add: 220307-CN [cpp]
This commit is contained in:
parent
abb3305568
commit
6cfc7529fd
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2203)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2203 220306.cpp)
|
||||
ADD_EXECUTABLE(2203 220307-CN.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue