码迷,mamicode.com
首页 > 其他好文 > 详细

3-8

时间:2016-07-23 18:08:34      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

#define _CRT_SECURE_NO_WARNINGS 

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int q[3000]; // quotient, size of the array ?
//int r[3000]; // remainder
int s[3000]; // whether a remainder occurs, totally b possibleremainders [0..b-1] with 1 <= b <= 3000

int main()
{
    int a, b;

    while (scanf("%d%d", &a, &b)) {
        memset(q, 0, sizeof(q));
        // memset(r, 0, sizeof(r));
        memset(s, 0, sizeof(s));

        int num = 0;
        q[num] = a / b; // q[0] is the integral part, and r[0] and s[0] are not used
        printf("%d/%d = %d.", a, b, a / b);

        a = a % b; 
        while (a != 0 && s[a] == 0) { // totally b possibleremainders [0..b-1], so the while loop will run no more than b times.
            ++num;
            s[a] = 1;
            q[num] = (10 * a) / b;
            // r[num] = a;
            a = (10 * a) % b;
        }

        if (a == 0) { // divide exactly
            for (int i = 1; i <= num; i++) // [1..num]
                printf("%d", q[i]);
            printf("(0)\n");
            printf("1 = number of digits in repeating cycle\n");
        }
        else {
            int pos = 1;
            printf("(");
            for (int i = 1; i <= 50 && i<=num; i++)
            {
                /*
                if (r[i] == a) // previous equal remainder
                {
                    printf("(");
                    pos = i;
                }
                */
                printf("%d", q[i]);
            }

            if (num > 50)
                printf("...");
            printf(")\n");
            printf("%d = number of digits in repeating cycles\n", num - pos + 1);
        }
    }

    return 0;
}

 

3-8

标签:

原文地址:http://www.cnblogs.com/patrickzhou/p/5698964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!