1/n循环节⻓度

求1/i的循环节长度的最大值

#include <iostream>
#include<cstring>
using namespace std;
const int MAXN=1005;
int res[MAXN];
int main()
{
    memset(res, 0, sizeof res);
    int i,j,n;
    for(int temp=1;temp<=1000;temp++){
        i=temp;
        while(!i%2)  i>>=1;   //一个小小的优化
        while(!i%5)  i/=5;
        n=1;
        for(j=1;j<=1000;j++){
            n*=10;
            n%=i;
            if(n==1){
                res[temp]=j;
                break;
            }
        }
    }
    int max_re;
     while (cin >> n) {
        max_re = 1;
        for (i = 1; i <= n; i++) {
            if (res[i] > res[max_re]) {
               max_re = i; }
         
        }
        cout << max_re << endl;}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41421433/article/details/82942764