首页 > 其他 > 详细

3-8

时间:2016-07-23 18:07:21      阅读:156      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!