51Node 1035----最长的循环节

51Node  1035----最长的循环节

正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数。
 
 
1/6= 0.1(6) 循环节长度为1
1/7= 0.(142857) 循环节长度为6
1/9= 0.(1)  循环节长度为1
Input
输入n(10 <= n <= 1000)
Output
输出<=n的数中倒数循环节长度最长的那个数
Input示例
10
Output示例
7

代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
int len[1005];
int a[1005];
int calc(int xx)
{
    int tot=0,pos=0;
    int x[10],y[10];
    int tmp1,tmp2=1,flag=0;
    while(1)
    {
        tmp1=(tmp2*10)/xx;
        tmp2=(tmp2*10)%xx;
        if(tmp1==0&&tmp2==0)
            return 0;
        if(tmp1)
            flag=1;
        if(flag) pos++;
        if(flag&&tot<8)
        {
            x[tot]=tmp1;
            y[tot++]=tmp2;
        }
        for(int i=0;i<tot-1;i++)
        {
            if(tmp1==x[i]&&tmp2==y[i])
            {
                return pos-i-1;
            }
        }
    }
}
void init()
{
    for(int i=3;i<1005;i++)
    {
        len[i]=calc(i);
    }
    int tmp=-1;
    int posi=-1;
    for(int i=3;i<1005;i++)
    {
        if(len[i]>tmp)
        {
            tmp=len[i];
            posi=i;
        }
        a[i]=posi;
    }
}
int main()
{
    int n;
    init();
    while(scanf("%d",&n)!=EOF) 
    {
        cout<<a[n]<<endl;
    }
    return 0;
}

 

posted @ 2016-07-02 18:33  茶飘香~  阅读(218)  评论(0编辑  收藏  举报