Posted on 2009-09-27 09:39
Hero 阅读(164)
评论(0) 编辑 收藏 引用 所属分类:
代码如诗--ACM
求小数的循环节长度
1 // B .CPP_VS Accepted 224 ms 4035 kb
2
3 #include <stdio.h>
4 #include <iostream>
5 using namespace std ;
6
7 const int size = 1100000 ;
8 int flag[size] ;
9
10 int ina, inb ;
11
12 int main()
13 {
14 while( cin >> ina >> inb )
15 {
16 if( ina % inb == 0 )
17 {
18 printf( "0 0\n" ) ;
19 continue ;
20 }
21
22 memset( flag, 0, sizeof(flag) ) ;
23
24 ina = ina % inb ;
25 int outa = 0 ; int outb = 0 ;
26 int x = 0 ;
27
28 while( ina < inb )
29 {
30 flag[ina] = 1 ;
31 ina = ina * 10 ;
32 outa = outa + 1 ;
33 //flag[ina] = 1 ;
34 }
35 outa = outa - 1 ;
36
37 while( true )
38 {
39 if( flag[x] ) break ;
40
41 flag[x] = 1 ;
42 x = ina % inb ;
43 //flag[x] = 1 ;
44 outa = outa + 1 ;
45 ina = x * 10 ;
46 }
47
48 memset( flag, 0, sizeof(flag) ) ;
49 flag[x] = 1 ;
50
51 ina = x * 10 ;
52 x = 0 ;
53 while( true )
54 {
55 if( flag[x] ) break ;
56
57 flag[x] = 1 ;
58 x = ina % inb ;
59 //flag[x] = 1 ;
60 outb ++ ;
61 ina = x * 10 ;
62 }
63
64 printf( "%d %d\n", outa-outb, outb ) ;
65 }
66 return 0 ;
67 }