Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator and print the decimal representation. If the decimal representation has a repeating sequence of digits, indicate the sequence by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as 0.(3), and 41/333 = 0.123123123...is denoted as 0.(123). Use xxx.0 to denote an integer. Typical conversions are:
1/3 = 0.(3) 22/5 = 4.4 1/7 = 0.(142857) 2/2 = 1.0 3/8 = 0.375 45/56 = 0.803(571428)
PROGRAM NAME: fracdec
INPUT FORMAT
A single line with two space separated integers, N and D, 1 <= N,D <= 100000.
SAMPLE INPUT (file fracdec.in)
45 56
OUTPUT FORMAT
The decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.
SAMPLE OUTPUT (file fracdec.out)
0.803(571428)
题意:
给出 N,D(1 ~ 100000),代表分子和分母,求出商,循环节用括号括起来。每76个字符输出一行。
思路:
模拟。用 res 数组保留商,rem 数组标记余数的位置(数组标记位置法),若出现一样的余数说明有循环节,记录这个位置到时候输出“(" 用。
AC:
/* TASK:fracdec LANG:C++ ID:sum-g1 */ #include <cstdio> #include <string.h> #define MAX 500000 using namespace std; int res[MAX], rem[100005]; //rem记录位置,res记录结果 int main() { freopen("fracdec.in", "r", stdin); freopen("fracdec.out", "w", stdout); int den, num, r = -1; int ans = 0, same = -1, k = 0; scanf("%d%d",&den,&num); memset(rem,-1,sizeof(rem)); while (r && same == -1) { r = den % num; rem[r] != -1 ? same = rem[r] : rem[r] = k++; res[ans++] = den / num; den = r * 10; } int time = 0, fir = res[0]; printf("%d.", res[0]); if (!fir) ++time; while (fir) { ++time; fir /= 10; } ++time; if (same == -1) { for (int i = 1; i < ans; ++i) { printf("%d", res[i]); if(!(++time % 76)) printf("\n"); } if(k == 1) { printf("0"); if(!(++time % 76)) printf("\n"); } printf("\n"); } else { for (int i = 0; i < ans; ++i) { if(i) { printf("%d",res[i]); if(!(++time % 76)) printf("\n"); } if (i == same) { printf("("); if(!(++time % 76)) printf("\n"); } } printf(")\n"); } return 0; }
相关推荐
Write a program that will accept a fraction of the form N/... Use xxx.0 to denote an integer. Typical conversions are: 1/3 = 0.(3) 22/5 = 4.4 1/7 = 0.(142857) 2/2 = 1.0 3/8 = 0.375 45/56 = 0.803(571428)
Manhattan GMAT Math-Fractions, Decimals & Percents.pdf
Neverending Fractions: An Introduction to Continued Fractions
other math levels (ideas: word problems, easier and harder math, fractions, decimals, time, etc) and other subjects as well (ideas: reading, maps, geometry, science, etc?) (Note: This app is
and secondly, senior or graduate students who would like an extensive introduction to the analytic theory of continued fractions. The book contains several recent results and new angles of approach ...
这是快速将小数转换为分数的JAVA版本,因此您不再需要登录即可转换小数。 必须具有JAVA才能运行.JAR文件。
7.1.5 Conversion of fractions to decimals 52 7.2 Percentages 52 7.2.1 Conversion of fractions and decimals into percentage 53 7.2.2 Value added tax (VAT) 54 7.3 Bulking of sand 54 8 Graphs 59 8.1 ...
As continued fractions become more important again, in part due to their use in finding algorithms in approximation theory, this timely book revives the approach of Wallis, Brouncker and Euler and ...
Special functions are pervasive in all ... We emphasise that only 10% of the continued fractions contained in this book, can also be found in the Abramowitz and Stegun project or at the Wolfram website!
作者: Oleg Karpenkov 出版社: Springer ISBN: 9783642393679
the most recent developments in solving the celebrated 1812 Gauss' problem which originated the metrical theory of continued fractions. At the same time, they study exhaustively the Perron-Frobenius ...
资源来自pypi官网。 资源全名:binary_fractions-1.0.2.tar.gz
As continued fractions become more important again, in part due to their use in finding algorithms in approximation theory, this timely book revives the approach of Wallis, Brouncker and Euler and ...
MANHATTAN GMAT Fractions Decimals and Percents GMAT Strategy Guide
and secondly, senior or graduate students who would like an extensive introduction to the analytic theory of continued fractions. The book contains several recent results and new angles of approach ...
Continued fractions and Pade approximants C. Brezinski
organic matter is generally divided into two fractions: bitumen and kerogen. Kerogen is insoluble in normal organic solvents and believed to be the source material for oil and gas that formed during ...
这是本关于连分数的数学文献 这本书里面有很多连分数公式L. Lorentzen and H. Waadeland, “Continued Fractions with Applications,” North Holland, Amsterdam, 1992.
Continued fractions C. D Olds美国新数学丛书《连分数》的英文原版
Design an external function convertF (not function member of class Fraction and iFraction) to convert the improper fractions to mixed fractions.