Recurring decimal with an infinite non-repeating decimals

 B input from the keyboard and denominator molecules a, determines the fraction (a proper fraction) is the infinite decimal, and gives the number of bits of the cycle began. Verify the following conclusions are correct:

(Score for b / a, a 'up to the decimal point, or from a start position of an infinite loop.)

 E.g:

 // 3/8 = 0.375 no more than seven decimal (finite decimal)

 1/3 = 0.333333 ... // start an infinite loop from a

 1/7 = 0.142857 6 142 857 142 857 @ start (7 bits) cyclic sequence occurs: 142857 

 Cyclic sequence appears 6/7 = 0.857142 6 857 142 857 142 @ start (7 bits): 857 142

(Try the magic of fractional 1/49 and 100 / 9801,100 / 9899)

prompt:

 Suppose there are 3/8 = 0.375

 Numbers on the first decimal place of business is 30/8, 30/8 is the value of the expression: 3

 Value of 8 to 30% (Remainder) is 6, then the decimal number on the second bit is the value of 60/8: 7

 8 60% value (remainder) is 4, then the number of positions is the decimal value of 40/8 to 3: 5

 

Write process encountered many problems to be resolved, so there may be some complex code is not streamlined, the author is limited. . . . .

This question inevitably there are simpler solutions, next came up again and change;

Firstly, the subject of the proposed method to obtain the fractional portion thereof, is then taken out my idea 2a digits after decimal individually determined;

But then, the problem may not be repeated in the digital loop, because he might as several consecutive numbers appear;

It is a problem for a long time, the final solution to the problem is not ideal, but it can be resolved;

Other cyclic and non-cyclic relatively easy;

code show as below:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int  main()
 5 {
 6     int b,a;
 7     cin>>b>>a;
 8     int arr[a*2+1] ,i,j,k,temp,m,n;
 9     int sign,sum = 0;
10     temp = b;
11     for(i = 1; i <= a*2; i++)        // 2a位小数 
12     {
13          temp *= 10;
14          arr[i] = temp / a;
15          temp %= a;
16 //         cout<<arr[i]; 
17          if(i>a)
18              sum += arr[i];
19     }
20     if(sum == 0)
21         cout<<"该分数为有限小数"<<endl;
22     else
23     {
24         for(i = 1;i <= 2*a;i++)
25         {
26             for(J = . 1 ; J <I; J ++)         // have equal 
27              {
 28                  IF (ARR [I] == ARR [J])        
 29                  {
 30                      Sign = 0 ;
 31 is                      m = ij of;             // number of bits by 
32                      IF (m =! . 1 )             // will not identical cycle 329 2 
33 is                      {
 34 is                          for (K = . 1 ; K <A; K ++)          // ensure subparagraph cycle without intermediate 
35                              IF (ARR [K + I] = ARR [! + J K])
36                                  Sign ++ ;
 37 [                      } 
 38 is                      the else         // must be the same loop 
39                      {
 40                          for (K = . 1 ; K <= A - J; K ++ )    
 41 is                              IF (ARR [J] = ARR [J +! K])
 42 is                                  Sign ++ ;
 43 is                      }        
 44 is                  }
 45                  IF (Sign == 0 )             // determined loop 
46 is                  {
 47                      n-= I -1 ;         // start point of the cycle 
48                      BREAK ;
 49                  }            
 50              }
 51 is              IF (Sign == 0 )
 52 is                  BREAK ;        
 53 is          }
 54 is          IF (Sign == 0 )
 55              COUT << " the infinite decimal fraction, position loop number: " << n-<< endl;
 56 is          the else 
57 is              COUT << " the score is infinitely non-repeating decimals " << endl;            
 58      }   
 59     return 0;
60 } 

Test example:

3 8

1 3

1 7

1 49

Output:

The finite decimal fraction

The score repeating infinite decimal, loop digits: 1

The score repeating infinite decimal, loop median: 6

The score repeating infinite decimal, loop median: 42

 

2020-01-1115:35:53

Guess you like

Origin www.cnblogs.com/2015-16/p/12180070.html