開発環境
- 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章(数学の問題)、問題2(最大公約数)の解答を求めてみる。
コード
#include <iostream> size_t gcd(size_t q, size_t r) { if (r == 0) { return q; } return gcd(r, q % r); } int main() { for (size_t i = 5; i < 10; i++) { for (size_t j = 5; j < 10; j++) { std::cout << "(" << i << "," << j << ") = " << gcd(i, j) << std::endl; } } for (size_t i = 50; i < 55; i++) { for (size_t j = 100; j < 105; j++) { std::cout << "(" << i << "," << j << ") = " << gcd(i, j) << std::endl; } } }
入出力結果(cmd(コマンドプロンプト)、Terminal)
Active code page: 65001 C:\Users\...>cl sample2.cpp && sample2.exe Microsoft(R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x64 Copyright (C) Microsoft Corporation. All rights reserved. sample2.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:sample2.exe sample2.obj (5,5) = 5 (5,6) = 1 (5,7) = 1 (5,8) = 1 (5,9) = 1 (6,5) = 1 (6,6) = 6 (6,7) = 1 (6,8) = 2 (6,9) = 3 (7,5) = 1 (7,6) = 1 (7,7) = 7 (7,8) = 1 (7,9) = 1 (8,5) = 1 (8,6) = 2 (8,7) = 1 (8,8) = 8 (8,9) = 1 (9,5) = 1 (9,6) = 3 (9,7) = 1 (9,8) = 1 (9,9) = 9 (50,100) = 50 (50,101) = 1 (50,102) = 2 (50,103) = 1 (50,104) = 2 (51,100) = 1 (51,101) = 1 (51,102) = 51 (51,103) = 1 (51,104) = 1 (52,100) = 4 (52,101) = 1 (52,102) = 2 (52,103) = 1 (52,104) = 52 (53,100) = 1 (53,101) = 1 (53,102) = 1 (53,103) = 1 (53,104) = 1 (54,100) = 2 (54,101) = 1 (54,102) = 6 (54,103) = 1 (54,104) = 2 C:\Users\...>
0 コメント:
コメントを投稿