|
发表于 2023-7-12 12:39:40
|
显示全部楼层
- /*
- Multiples of 3 and 5
- If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
- Find the sum of all the multiples of 3 or 5 below 1000.
- 题目翻译:
- 10 以下的自然数中,属于 3 或 5 的倍数的有 3, 5, 6 和 9,它们之和是 23。
- 找出 1000 以下的自然数中,属于 3 或 5 的倍数的数字之和。
- */
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int sum; // 答案总和
- for (int i = 1; i < 1000; i++) {
- if (i % 3 == 0 || i % 5 == 0) {
- sum += i;
- }
- }
- cout << sum << endl;
- return 0;
- }
复制代码 |
|