Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Jun 16, 2022
1 parent d2b1bf0 commit 7ae6df2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 012.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public:

if (a != b)
{
m_parents[a] = b;
m_parents[b] = a;
}
}

Expand Down
42 changes: 41 additions & 1 deletion 067.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 067 - Base 8 to 9 (★2)

## 解答
## 解答 1

```cpp
#include <iostream>
Expand Down Expand Up @@ -69,3 +69,43 @@ int main()
std::cout << N << '\n';
}
```
## 解答 2 (`std::stoll()` と `std::to_chars()` を使う)
C++ 標準ライブラリの `std::stoll()` と `std::to_chars()` を使うと、短いコードで解答できます。
```cpp
#include <iostream>
#include <string> // std::stoll()
#include <charconv> // std::to_chars()
#include <algorithm> // std::replace()
int main()
{
// 8 進法の整数 N
std::string N;
std::cin >> N;
// K 回の操作
int K;
std::cin >> K;
// 9 進数を格納するのに十分なバッファ
char base9[32]{};
for (int i = 0; i < K; ++i)
{
// 8 進数を 10 進数に
const long long base10 = std::stoll(N, nullptr, 8);
// 10 進数を 9 進数に
N.assign(base9, std::to_chars(std::begin(base9), std::end(base9), base10, 9).ptr);
// 文字列中の '8' を '5' に置き換える
// 例: "18588" -> "15555"
std::replace(N.begin(), N.end(), '8', '5');
}
// 解答を出力
std::cout << N << '\n';
}
```

0 comments on commit 7ae6df2

Please sign in to comment.