首页 技术 正文
技术 2022年11月12日
0 收藏 802 点赞 3,471 浏览 3151 个字

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.

fraction  decimal expansion   repeating cycle   cycle length
1/6        0.1(6)       6         1

5/7       0.(714285)     714285        6

1/250      0.004(0)       0         1

300/31     9.(677419354838709)  677419354838709 15

655/990     0.6(61)       61         2

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


题意:输入整数a和b(0<=a、b<=3000),输出a/b的循环小数以及循环节长度,最多显示50位,超过50位后面的用“…”表示。输出循环节的小数位数


看到这道题目的时候是一脸懵圈呀,完全不知道它在说什么,连高精度小数都不会算。。。

在百度了过后发现了高精度小数的一个算法

a对b取余然后乘10除b直到除数重复,另取一个数组保存次数,那么除数重复的时候查看保存次数即可直到从第几项开始重复了

 memset(vis, -, sizeof(vis));
int c = a % b, cnt = ;
c *= ;
while(vis[c] == -)
{
res[cnt] = c / b;
vis[c] = cnt++;
c %= b;
c *= ;
}

其中vis数组用于保存一个被除数访问的次数

res数组用于保存小数值

cnt保存除法次数

那么为什么可以这么计算

下面来讲述一下这个算法的运行过程


首先,明确c/b为整数部分

然后将c*10的意义就是将小数点后移一位

一位一位的实现计算

从而可以实现不断计算小数位数的功能

下面贴出代码

源代码摘自:https://blog.csdn.net/flyawayl/article/details/51892740

 //#define LOCAL
#include <stdio.h>
#include <string.h>
const int maxn = + ; int a, b;
int vis[maxn], res[maxn]; int main() {
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
while(scanf("%d%d", &a, &b) == ) {
memset(vis, -, sizeof(vis));
int c = a % b, cnt = ;
c *= ;
while(vis[c] == -) {
res[cnt] = c / b;
vis[c] = cnt++;
c %= b;
c *= ;
}
// repeating cycle start-position
int sta_pos = vis[c];
printf("%d/%d = %d.", a, b, a/b);
for(int i = ; i < sta_pos; i++) {
printf("%d", res[i]);
}
printf("(");
if(cnt - sta_pos <= ) {
for(int i = sta_pos; i < cnt; i++) {
printf("%d", res[i]);
}
} else {
for(int i = sta_pos; i < sta_pos+; i++) {
printf("%d", res[i]);
}
printf("...");
}
printf(")\n");
printf(" %d = number of digits in repeating cycle\n\n", cnt - sta_pos);
}
return ;
}

2019-02-16  05:13:31  Author:LanceYu

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905