開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Xcode - Apple
- Windows 10 Pro (OS)
- Visual Studio (コンパイラー)
- Visual Studio Code (Text Editor)
- C++17 (プログラミング言語)
Modern C++チャレンジ ―C++17プログラミング力を鍛える100問 (Marius Bancila(著)、島 敏博(監修)、黒川 利明(翻訳)、オライリージャパン)の1章(数学の問題)、問題1(3または5で割り切れる正の整数の総和)の解答を求めてみる。
コード
#include <iostream>
int main() {
int total = 0;
for(size_t i = 0; i < 100; i++)
{
if (i % 3 == 0 || i % 5 == 0) {
total += i;
}
}
std::cout << "総和: ";
std::cout << total << std::endl;
}
入出力結果(VS 2017 用 x64 Native Tools コマンド プロンプト、Terminal)
Active code page: 65001 C:\Users\...>cl sample1.cpp Microsoft(R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x64 Copyright (C) Microsoft Corporation. All rights reserved. sample1.cpp C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: C++ 例外処理を使っていますが、アンワインド セマンティクスは有効にはなりません。/EHsc を指定してください。 Microsoft (R) Incremental Linker Version 14.16.27027.1 Copyright (C) Microsoft Corporation. All rights reserved. /out:sample1.exe sample1.obj C:\Users\...>sample1.exe 総和: 2318 C:\Users\...>
0 コメント:
コメントを投稿