開発環境
- 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章(数学の問題)、問題8(アームストロング数(ナルシシスト数))の解答を求めてみる。
コード
#include <iostream> #include <cmath> int main() { size_t upper = 1000000; std::cout << "3桁のアームストロング数(ナルシシスト数)" << std::endl; size_t count = 1; for (size_t i = 100; i < 999; i++) { for (size_t x = 1;; x++) { size_t t = 0; for (size_t j = i; j != 0; j /= 10) { t += std::pow((j % 10), x); if (t > i) { break; } } if (t <= 3 || t > i) { break; } if (t == i) { std::cout << count << "個目: " << i << std::endl; count++; break; } } } }
入出力結果(cmd(コマンドプロンプト)、Terminal)
Active code page: 65001 C:\Users\...>cl /O2 sample8.cpp && sample8.exe Microsoft(R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x64 Copyright (C) Microsoft Corporation. All rights reserved. sample8.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:sample8.exe sample8.obj 3桁のアームストロング数(ナルシシスト数) 1個目: 153 2個目: 370 3個目: 371 4個目: 407 C:\Users\...>
0 コメント:
コメントを投稿