# UVa 202 Repeating Decimal

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">问题：求一个有理数的循环周期。</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">思路：余数出现分母次后，只少会有两次是相同的</span></span>

流程：①读取分子，分母；

②取模、取余、乘10，放到一个结构数组中

③每取一个余数，都要到结构中查一下是否有相同的值

④这两个值之间的值就是周期。

⑤根据格式输出周期

⑥如果超过50，使用省略号。

Trick：OJ上最后结果添加了一个空行，提交了好几次总是PresentationE，神奇。

#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

typedef struct {
int factor;
int remainder;
} Elem;

int main()
{
#ifndef UVa
FILE *fp;
freopen_s(&fp, "data.in.txt", "r", stdin);
freopen_s(&fp, "data.out.txt", "w", stdout);
#endif

Elem *eArr = new Elem[3000];
int numerator = 0;
int denominator = 0;
int cnt = 0;

while (cin >> numerator >> denominator) {
int pos1 = -1,pos2 = -1;
if (cnt++ > 0)
cout << endl;
cout << numerator << "/" << denominator << " = ";
for (int i = 0; i < 3000; ++i) {
eArr[i].factor = numerator / denominator;
numerator %= denominator;
eArr[i].remainder = numerator;
numerator *= 10;
for (int j = 0; j < i; ++j)
if (eArr[i].remainder == eArr[j].remainder) {
pos1 = j;
pos2 = i;
break;
}
else {
pos1 = -1;
pos2 = -1;
}//endif
if (pos1 != -1 && pos2 != -1)
break;
}//endfor

cout << eArr[0].factor << ".";
int index = 0;
for (index = 1; index <= pos1; ++index)
cout << eArr[index].factor;
cout << "(";

for (; index <= pos2; ++index ) {
cout << eArr[index].factor;
if (index == 50)
break;
}
if (index == 50)
cout << "...";
cout << ")" << endl;
cout << "   " << setiosflags(ios::left) << pos2 - pos1 << " = number of digits in repeating cycle" << endl;
}
cout << endl;
return 0;
}


#### UVa 202 - Repeating Decimals

2014-10-07 23:46:26

#### uva202循环小数Repeating Decimals

2015-01-23 08:43:51

#### 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)

2017-01-29 11:37:36

#### UVA 202 - Repeating Decimals（循环小数循环节）

2016-11-23 20:53:27

#### UVA202 UVALive5141 Repeating Decimals【分数】

2016-08-01 17:39:29

#### 循环小数 UVa202

2016-07-12 21:29:00

#### UVa-202

2017-02-23 17:17:59

#### 循环小数(Repeating Decimals)

2015-02-09 21:45:39

#### UVA 202 Repeating Decimals

2018-07-01 22:45:39

#### uva 202 Repeating Decimals

2016-04-09 21:19:51