n为2到50,如何顺序输出1/n的十进制表示的字符串(写出一个循环节为止)

输出一个循环周期后就结束该数的输出1/7=0.142857n为2到50,如何顺序输出1/n的十进制表示的字符串(写出一个循环节为止)

算法思路: 1)1/n的循环周期不会超过n;2)当余数重复时,循环周期就出现了。参考下面代码:
int main(void)
{
char flag[50];
int b;
int c;
int i;

for(i = 2; i <= 50; i ++) {
    memset(flag, 0, sizeof(flag));
    printf("1/%d = 0.", i);

    c = 10;
    while(1) {
        printf("%d", c/i);
        b = c % i;
        if(!b || flag[b]) {
            break;
        }
        flag[b] = 1;
        c = b * 10;
    }
    printf("\n");
}

return 0;

}

下面是节选的测试结果:
1/43 = 0.0232558139534883720930
1/44 = 0.0227
1/45 = 0.02
1/46 = 0.02173913043478260869565
1/47 = 0.02127659574468085106382978723404255319148936170
1/48 = 0.02083
1/49 = 0.0204081632653061224489795918367346938775510
1/50 = 0.02

C语言版
(如果对你有帮助请点个赞!谢谢
#include
/*C语言版*/
void function()
{
int n;
double temp;
for(n=2;n<=50;n++)
{
temp=1.0/(n*1.0);
printf("1/%d=%lf\n",n,temp);
}
}

int main()//主方法
{
function();
return 0;
}


结果显示:
1/2=0.500000
1/3=0.333333
1/4=0.250000
1/5=0.200000
1/6=0.166667
1/7=0.142857
1/8=0.125000
1/9=0.111111
1/10=0.100000
1/11=0.090909
1/12=0.083333
1/13=0.076923
1/14=0.071429
1/15=0.066667
1/16=0.062500
1/17=0.058824
1/18=0.055556
1/19=0.052632
1/20=0.050000
1/21=0.047619
1/22=0.045455
1/23=0.043478
1/24=0.041667
1/25=0.040000
1/26=0.038462
1/27=0.037037
1/28=0.035714
1/29=0.034483
1/30=0.033333
1/31=0.032258
1/32=0.031250
1/33=0.030303
1/34=0.029412
1/35=0.028571
1/36=0.027778
1/37=0.027027
1/38=0.026316
1/39=0.025641
1/40=0.025000
1/41=0.024390
1/42=0.023810
1/43=0.023256
1/44=0.022727
1/45=0.022222
1/46=0.021739
1/47=0.021277
1/48=0.020833
1/49=0.020408
1/50=0.020000

http://blog.csdn.net/havedream_one/article/details/41862179
反推一下吧

节选自《编程之美》
设一个循环小说X=0.a1a2a3...an(b1b2...bm),括号中的是循环节,例0.333(333)
将X分成两部分,X=(a1a2..an+0.(b1b2..bm))/10n
设Y=0.(b1b2...bm)
则有Y=(b1b2...bm+0.(b1b2...bm))*10m
那么就有Y=b1b2...bm/(10m-1)
综上可知
X=(a1a2...an+Y)/10n
将Y=b1b2...bm/(10m-1)带入上式
则X=((a1a2...an)*(10m-1)+(b1b2...bm))/((10m-1)*10n)

注:10m,10n是10的m次方和10的n次方的意思

首先弄一个字符串,然后把上次的余数乘以十除以n,商作为字符串累加到上次的字符串后…

 #include <iostream>
using namespace std;
void foo(int n)
{
    int x = 1;
    int * initmod = new int[n];
    cout << "1 / " << n << " = 0.";
    int i = 0;
    while (true)
    {
        x = x * 10;
        cout << x / n;
        x = x % n;
        if (x % n == 0) break;
        for (int j = 0; j < i; j++)
            if (initmod[j] == x % n) goto exitloop;
        initmod[i++] = x % n;
    }
    cout << endl;
    return;
exitloop:
    cout << "..." << endl;
}

int main()
{
    for (int i = 2; i <= 50; i++)
    {
        foo(i);
    }
}

1 / 2 = 0.5
1 / 3 = 0.33...
1 / 4 = 0.25
1 / 5 = 0.2
1 / 6 = 0.16...
1 / 7 = 0.1428571...
1 / 8 = 0.125
1 / 9 = 0.11...
1 / 10 = 0.1
1 / 11 = 0.090...
1 / 12 = 0.083...
1 / 13 = 0.0769230...
1 / 14 = 0.0714285...
1 / 15 = 0.06...
1 / 16 = 0.0625
1 / 17 = 0.05882352941176470...
1 / 18 = 0.05...
1 / 19 = 0.0526315789473684210...
1 / 20 = 0.05
1 / 21 = 0.0476190...
1 / 22 = 0.045...
1 / 23 = 0.04347826086956521739130...
1 / 24 = 0.0416...
1 / 25 = 0.04
1 / 26 = 0.0384615...
1 / 27 = 0.0370...
1 / 28 = 0.03571428...
1 / 29 = 0.03448275862068965517241379310...
1 / 30 = 0.03...
1 / 31 = 0.0322580645161290...
1 / 32 = 0.03125
1 / 33 = 0.030...
1 / 34 = 0.02941176470588235...
1 / 35 = 0.0285714...
1 / 36 = 0.027...
1 / 37 = 0.0270...
1 / 38 = 0.0263157894736842105...
1 / 39 = 0.0256410...
1 / 40 = 0.025
1 / 41 = 0.024390...
1 / 42 = 0.0238095...
1 / 43 = 0.0232558139534883720930...
1 / 44 = 0.0227...
1 / 45 = 0.02...
1 / 46 = 0.02173913043478260869565...
1 / 47 = 0.02127659574468085106382978723404255319148936170...
1 / 48 = 0.02083...
1 / 49 = 0.0204081632653061224489795918367346938775510...
1 / 50 = 0.02

在线编译
http://codepad.org/D99WD6ih

大数除法,余数为1或者0 终止
余数为1 结果是循环节
余数为0,则可以整除
因为最大数是50 估计不用大数除法也可以算出

#include

using namespace std;
template
T cyclenum(T n){
if(n<1)return 1;
if(n==1){return 1;}
T p[50]={0};
T r[50]={0};
T t=1,c=0,m;
cout<<0<<".";
do{
t*=10;
m = r[c] = t%n;
p[c]=t/n;

for(T i=0;i<c;i++){
    if( r[c]==r[i]&&p[c]==p[i]){
        T j;
        for(j=0;j<i;j++)
        cout<<p[j];
        cout<<"(";
        for(;j<c;j++)
            cout<<p[j];
        cout<<")";
        return c;
    }
}
///cout<<p[c];
t%=n;
++c;

}while(m>0);
for(T i=0;i<c;i++)
cout<<p[i];
return c;
}

int main()
{
for(int i=2;i<51;i++){
cout <<"1/"<<i<<" = ";
cyclenum(i);
cout <<endl;
}
cout << "Hello world!" << endl;
return 0;
}

1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
1/11 = 0.(09)
1/12 = 0.08(3)
1/13 = 0.(076923)
1/14 = 0.0(714285)
1/15 = 0.0(6)
1/16 = 0.0625
1/17 = 0.(0588235294117647)
1/18 = 0.0(5)
1/19 = 0.(052631578947368421)
1/20 = 0.05
1/21 = 0.(047619)
1/22 = 0.0(45)
1/23 = 0.(0434782608695652173913)
1/24 = 0.041(6)
1/25 = 0.04
1/26 = 0.0(384615)
1/27 = 0.(037)
1/28 = 0.03(571428)
1/29 = 0.(0344827586206896551724137931)
1/30 = 0.0(3)
1/31 = 0.(032258064516129)
1/32 = 0.03125
1/33 = 0.(03)
1/34 = 0.0(2941176470588235)
1/35 = 0.0(285714)
1/36 = 0.02(7)
1/37 = 0.(027)
1/38 = 0.0(263157894736842105)
1/39 = 0.(025641)
1/40 = 0.025
1/41 = 0.(02439)
1/42 = 0.0(238095)
1/43 = 0.(023255813953488372093)
1/44 = 0.02(27)
1/45 = 0.0(2)
1/46 = 0.0(2173913043478260869565)
1/47 = 0.(0212765957446808510638297872340425531914893617)
1/48 = 0.0208(3)
1/49 = 0.(020408163265306122448979591836734693877551)
1/50 = 0.02
Hello world!

赞同c语言开发那个,简单明了 很轻松实现你的目的。

根据,观察,1/n ,算上循环小数第一个循环节的最后一位,共有 不超过 分母n位小数;

所有1/n 中,其中循环小数,只计算循环节,小数位数,不超过n位