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

11个回答

`````` #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

#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!

lm_whales n为2，5 以外的奇素数的，循环节不超过 n-1 位 可以用 费马小定理证明
4 年多之前 回复
lm_whales n为2，5 以外的奇素数的，循环节不超过 n-1 位，别的数循环节，只能比n-1少
4 年多之前 回复
lm_whales 可能还不超过n-1
4 年多之前 回复
lm_whales 余数为0 是整除，非零需要判断是否循环小数
4 年多之前 回复
lm_whales 余数相同判断，可能跟循环节位数相差1，因为商可以有一位不同，所以要同时判断商和余数都相同的，后面就是下一个循环节了
4 年多之前 回复
lm_whales 只计算到第一个循环节
4 年多之前 回复

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

4 年多之前 回复

X=（a1a2...an+Y）/10n

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

4 年多之前 回复