Snippets

cia_rana 今週のお題:円周率に近似できる分数

Created by cia_rana
#include <iostream>
#include <math.h>
using namespace std;

struct Answers {
    int ary[12];
    constexpr Answers() : ary() {
        for(int i = 0; i < 12; i++) {
            long long d = pow(10, i);
            long long pi = 314'159'265'358 / ((long long)pow(10, 11 - i));
            for(int j = 1;; j++) {
                if((pi * j / d + 1) * d / j == pi) {
                    ary[i] = j;
                    break;
                }
            }
        }
    }
};

int main() {
    int n;
    cin >> n;
    cout << Answers().ary[n];
}
1
2
3
4
5
6
n = gets.to_i
D = 10 ** n
PI = 314_159_265_358 / (10 ** (11 - n))
p (1..Float::INFINITY).each{|i|
  break i if (PI * i / D + 1) * D / i == PI
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.