結果
問題 | No.8111 April Fool 2024 in Pi |
ユーザー |
![]() |
提出日時 | 2025-04-03 16:00:11 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,592 bytes |
コンパイル時間 | 737 ms |
コンパイル使用メモリ | 70,368 KB |
実行使用メモリ | 7,844 KB |
最終ジャッジ日時 | 2025-04-03 16:00:13 |
合計ジャッジ時間 | 1,352 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | WA * 1 |
ソースコード
#include <iostream> #include <string> #include <cmath> #include <sstream> // リープニッツの公式で円周率を小数点以下まで計算 std::string calculatePi(int precision) { // 分母が奇数になることを利用します long double pi = 0.0; long long k = 0; for (long long i = 0; i < precision * 10; ++i) { pi += (i % 2 == 0 ? 1.0 : -1.0) / (2.0 * k + 1.0); k++; } pi *= 4.0; // リープニッツの公式では π/4 を求めるので、4 を掛けることで π を得ます。 // 小数部分を取得する文字列加工 std::ostringstream oss; oss.precision(precision + 10); // 精度を指定(誤差調整用に多め) oss << std::fixed << pi; // 整数部分("3.")を除外 std::string piStr = oss.str(); size_t dotIndex = piStr.find('.'); if (dotIndex != std::string::npos) { return piStr.substr(dotIndex + 1); } return ""; } int main() { const std::string TARGET = "20240401"; // 探す文字列 const int PRECISION = 100000; // 円周率の計算桁数 // 円周率を計算して文字列として取得 std::string piDecimal = calculatePi(PRECISION); // ターゲット文字列を検索 size_t position = piDecimal.find(TARGET); if (position != std::string::npos) { // 見つかった場合、位置を1加算して出力(1始まり) std::cout << position + 1 << std::endl; } else { // 見つからない場合は -1 を出力 std::cout << -1 << std::endl; } return 0; }