Repeating Decimals

来源:互联网 发布:网络教育毕业总结 编辑:程序博客网 时间:2024/06/16 04:58

The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03 repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number (fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have no such repeating cycles.
Examples of decimal expansions of rational numbers and their repeating cycles are shown below. Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.
这里写图片描述
Write a program that reads numerators and denominators of fractions and determines their repeating cycles.
For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal length string of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thus for example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0 which begins at positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).

Input

Each line of the input file consists of an integer numerator, which is nonnegative, followed by an integer denominator, which is positive. None of the input integers exceeds 3000. End-of-file indicates the end of input.

Output

For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycle to the right of the decimal or 50 decimal places (whichever comes first), and the length of the entire repeating cycle.
In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle begins — it will begin within the first 50 places — and place ‘…)’ after the 50th digit.

Sample Input

76 25
5 43
1 397

Sample Output

76/25 = 3.04(0)
1 = number of digits in repeating cycle
5/43 = 0.(116279069767441860465)
21 = number of digits in repeating cycle
1/397 = 0.(00251889168765743073047858942065491183879093198992…)
99 = number of digits in repeating cycle

题意:计算一个分数的循环小数的位数
方法:作逐步除法,判断是否有重复的商,若有,那么循环的小数必然在这两个商之间

#include <iostream>#include <stdio.h>#include <memory.h>const int MAX_CYCLE_LEN = 3005;const int MAX_POS_LEN = 3005;const int LIMIT = 50;int reminder_exist[MAX_POS_LEN];int reminder_pos[MAX_POS_LEN];using namespace std;int main(){    int numerator, demoninator;    int quot, reminder;    while(scanf("%d %d", &numerator, &demoninator) != EOF && demoninator)    {        int orig_numerator = numerator;        int cycle_len, cycle_start;        int digit[MAX_CYCLE_LEN];        int Integer;        memset(reminder_exist, 0, sizeof(reminder_exist));        memset(reminder_pos, 0, sizeof(reminder_pos));        quot = numerator / demoninator;        reminder = numerator % demoninator;        Integer = quot;        for(int i=0;;i++)        {            if(reminder_exist[reminder])            {                cycle_start = reminder_pos[reminder];                cycle_len = i - cycle_start;                break;            }            else            {                reminder_exist[reminder] = 1;                reminder_pos[reminder] = i;            }            numerator = reminder * 10;            quot = numerator / demoninator;            reminder = numerator % demoninator;            digit[i] = quot;        }        printf("%d/%d = %d.", orig_numerator, demoninator, Integer);        for(int i=0; i<cycle_start; i++)            printf("%d", digit[i]);        printf("(");        if(cycle_len <= LIMIT)        {            for(int i=cycle_start; i<cycle_len + cycle_start; i++)                printf("%d", digit[i]);            printf(")\n");        }        else        {            for(int i=cycle_start; i<LIMIT + cycle_start; i++)                printf("%d", digit[i]);            printf("...)\n");        }        printf("   %d = number of digits in repeating cycle\n", cycle_len);        printf("\n");    }    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在丈夫面前被别人犯侵天翼电影 中文版丈夫的上司连续浸七天 吉尺明步110部连接 吉g明教师步中文字迅雷下载 吉次明涉作品 被丈夫好友侵犯有感觉了中文3 侵犯新娘在丈夫面前被侵先锋 吉朋步高清在线 吉译朋步查官 新娘在婚礼上的厕所被动态 在丈夫遗像面前被侵演员叫什么 吉译朋步30分钟视频教程 吉译朋步义兄 吉译朋步孩子线播放 丈夫面前侵犯中文高清在线播放 在线中文字幕最好看最经典 丈夫面前不允许出声的侵犯2 2018吉译明i作品全集一览 好看的中文字幕推荐 yellow字幕幕网 中文字幕人人视频在线萝莉 中文永久字幕在线播放 好看的字幕最新字幕一经典幕 母乳妻诱惑中文字幕 有什么好看的电影 2018好看的大陆电影 什么动漫电影好看中文 在线播放丝服制袜 2018最新手机中文字幕 中文字幕视频2018 中文字幕爸爸在线播放 濡x母 もう、やめて… 中文字幕在线手机播放2018 免费中文字幕 啄木鸟中文字幕在线播放军舰 中文字幕永久在线2016 免费伦电影中文字幕 大嫂潮湿中文字幕在线迅雷 小白看看永久播放平台 看永久看免费2018中文字 妻人射中文字幕