Skip to content

Commit

Permalink
docs: 演示拆分位数后使用位数
Browse files Browse the repository at this point in the history
  • Loading branch information
FeignClaims authored Nov 1, 2024
1 parent a608568 commit 0d218b3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions faq/exercise/split_digits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@
- 如果你需要保留 :cpp:`value` 不变呢? :cpp:`for (int i = value; i != 0; i /= 10)`.
- 如果输入的 :cpp:`value` 是 :cpp:`0` 呢? 那就需要你另外考虑了.

例如, 我们可以对整数的各个位数进行求和:

.. code-block:: cpp
:linenos:
#include <iostream>
using namespace std;
int main() {
int value;
cin >> value;
int sum_of_digits = 0;
for (; value != 0; value /= 10) {
int digit = value % 10;
sum_of_digits += digit;
}
}
========================================================================================================================
不知道为什么新手会想到的复杂方法
========================================================================================================================
Expand Down

0 comments on commit 0d218b3

Please sign in to comment.