Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216270
  • 博文数量: 68
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-08 09:53
文章分类
文章存档

2012年(29)

2011年(3)

2010年(18)

2009年(18)

我的朋友

分类: C/C++

2012-01-26 13:57:11

问题:

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

1/20.5
1/30.(3)
1/40.25
1/50.2
1/60.1(6)
1/70.(142857)
1/80.125
1/90.(1)
1/100.1

Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.


答案:983
#include
#include


int main(void)
{
    int n, i, len, maxlen, maxn;
    maxlen = 0;
    for( n=2 ; n<=1000 ; n++ ){
        int rest = 1;
        int r0;
        for( i=0 ; i            rest = (rest*10)%n;
        r0 = rest;
        len = 0;
        do {
            rest = (rest*10)%n;
            len++;
        } while( rest!=r0 );
        if( len>maxlen ){
            maxn = n;
            maxlen = len;
        }
    }
    printf("The longest is %d\n", maxn);
    return 0;
}


阅读(714) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~