题目链接如下:
我的代码如下(start表示开始循环的位数,pos表示在小数点后的位数,cycle表示循环的长度,map表示从被除数映射到当时的pos位数):
- #include
- #include
-
- int m, n, temp, pos, cycle, start;
-
- int main(){
- while(scanf("%d", &m) == 1){
- scanf("%d", &n);
- std::map<int, int> a;
- temp = m % n * 10;
- pos = 1;
- while(1){
- if(a[temp]){
- cycle = pos - a[temp];
- start = a[temp];
- break;
- }
- a[temp] = pos;
- temp = temp % n * 10;
- pos++;
- }
- printf("%d/%d = %d.", m, n, m / n);
- temp = m % n * 10;
- for(pos = 1; pos <= std::min(start + cycle - 1, 50); ++pos){
- if(pos == start){
- printf("(");
- }
- printf("%d", temp / n);
- temp = temp % n * 10;
- }
- if(start - 1 + cycle > 50){
- printf("...");
- }
- printf(")\n %d = number of digits in repeating cycle\n\n", cycle);
- }
- return 0;
- }