Repeating Decimals UVA - 202

 1 The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03
 2 repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number
 3 (fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have no
 4 such repeating cycles.
 5 Examples of decimal expansions of rational numbers and their repeating cycles are shown below.
 6 Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.
 7 fraction decimal expansion repeating cycle cycle length
 8 1/6 0.1(6) 6 1
 9 5/7 0.(714285) 714285 6
10 1/250 0.004(0) 0 1
11 300/31 9.(677419354838709) 677419354838709 15
12 655/990 0.6(61) 61 2
13 Write a program that reads numerators and denominators of fractions and determines their repeating
14 cycles.
15 For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal length
16 string of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thus
17 for example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0
18 which begins at positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).
19 Input
20 Each line of the input file consists of an integer numerator, which is nonnegative, followed by an integer
21 denominator, which is positive. None of the input integers exceeds 3000. End-of-file indicates the end
22 of input.
23 Output
24 For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycle
25 to the right of the decimal or 50 decimal places (whichever comes first), and the length of the entire
26 repeating cycle.
27 In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the
28 entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle
29 begins — it will begin within the first 50 places — and place ‘...)’ after the 50th digit.
30 Sample Input
31 76 25
32 5 43
33 1 397
34 Sample Output
35 76/25 = 3.04(0)
36 1 = number of digits in repeating cycle
37 5/43 = 0.(116279069767441860465)
38 21 = number of digits in repeating cycle
39 1/397 = 0.(00251889168765743073047858942065491183879093198992...)
40 99 = number of digits in repeating cycle

题目

题目大意:输入,a,b(保证a/b是循环小数),问:从那一段开始循环,非循环部分直接输出,循环部分用括号括起来,但是如果循环部分未在小数点后50位内全部打出,在第50位后输出"...)"

分析:首先我们要先解决拿取小数部分的问题(毕竟如果直接a/b的小数部分会有精度损失),而a%b*10/b(如1/10,1%10*10/10),用这种方法恰好解决了这个问题,接着就是如何判断循环的问题,我个人的想法是看a%b的余数,如果余数相同,该从这位后都以前一部分循环。

PS:注意输出格式,第二行开头要空3格,每个例子间空一行。

#define debug
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
#include<iomanip>
#include<map>
#include<set>
#define f first
#define s second
#define pb push_back
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll>PLL;
typedef pair<int,ll>Pil;
const ll INF = 0x3f3f3f3f;
const double inf=1e8+100;
const double eps=1e-8;
const int maxn =1e4+100;
const int N = 1e3+10;
const ll mod=1e9+7;
//------
//define
int arr[maxn];
map<int,int>mp;
//solve
void solve() {
	int a,b;
	while(cin>>a>>b) {
		memset(arr,0,sizeof(arr));
		mp.clear();
		cout<<a<<"/"<<b<<" = "<<a/b<<".";
		a%=b;
		int ze=0;
		int r,l;
		mp[a]=0;
		for(int i=1;i<3001;i++){
			a*=10;
			arr[i]=a/b;
			if(mp.count(a%b)){
				l=mp[a%b];
				r=i;
				break;
			}else{
				mp[a%b]=i;
			}
			a%=b;
		}
		int tl=l+1,tr=r;//第l+1才是循环的头
		for(int i=1;i<=50&&i<=tr;i++){
			if(i==tl)cout<<"(";
			cout<<arr[i];
		}
		if(r<=50)
		cout<<")"<<endl<<"   "<<r-l;
		else{
			cout<<"...)"<<endl<<"   "<<r-l;
		}
		cout<<" = number of digits in repeating cycle"<<endl<<endl;
	}
}
//main
int main() {
	ios_base::sync_with_stdio(false);
#ifdef debug
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	cin.tie(0);
	cout.tie(0);
	solve();
	/*
		#ifdef debug
			fclose(stdin);
			fclose(stdout);
			system("out.txt");
		#endif
	*/
	return 0;
}

  

原文地址:https://www.cnblogs.com/visualVK/p/8675094.html

时间: 2024-10-08 01:40:22

Repeating Decimals UVA - 202的相关文章

UVA202 UVALive5141 Repeating Decimals

问题链接:UVA202 UVALive5141 Repeating Decimals.基础训练级的问题,用C语言编写程序. 问题简述:输入两个整数numerator和denominator,分别为分子和分母.0≤分子,1≤分母≤3000.输出a/b的循环小数表示以及循环节长度.如果循环周期大于50,只显示50位,之后的全部用"..."表示. 解题思路:先取出整数部分(numerator/denominator的商),然后用余数(numerator%denominator的余数)计算小数

UVA202循环小数Repeating Decimals

Repeating Decimals The decimal expansion of the fraction 1/33 is , where the 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 cyc

UVa 202 Repeating Decimals

计算循环小数的循环节 输入整数a和b(0<=a<=3000,1<=b<=3000),输出a/b的循环小数表示以及循环节长度. 例如,a=5,b=43,小数表示为0.(116279069767441860465),循环字节长度为21 可以用数组储存数字,模拟竖式除法来解决. 附AC代码: 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int MAX=3050;

UVa 202 Repeating Decimals【模拟】

题意:输入整数a和b,输出a/b的循环小数以及循环节的长度 学习的这一篇 http://blog.csdn.net/mobius_strip/article/details/39870555 因为n%m的余数只可能是0到m-1中的一个,根据抽屉原理,当计算m+1次时至少存在一个余数相同 发现看了题解理解起来也好困难啊, 后来手动画了一下5/7的竖式除法的式子,,理解一些了 1 #include<iostream> 2 #include<cstdio> 3 #include<c

uva 202 Repeating Decimals 模拟

弱校连萌题目链接:http://acm.bnu.edu.cn/v3/contest_show.php?cid=5772#problem/G 需要先想到出现循环节意味着出现了以前出现过的余数 然后就自己手写一个大数除法 后来看别人博客发现其实不用STL也可以 检查余数是否出现过可以是常数级的 因为除数不大于3000 所以开一个3010的数组来存余数标记即可 当然我还是用的map 因为复杂度实在太松了 这题烦得很 注意“输出前50位”是指所有数字加起来50位... 不是小数点后50位...(哭晕在厕

UVa 202 Repeating Decimals 题解

The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03 repeats inde?nitely with no intervening digits. In fact, the decimal expansion of every rational number (fraction) has a repeating cycle as opposed

202 - Repeating Decimals

#include <stdio.h> #include <string.h> using namespace std; int isExist(int* list, int size, int val); int main(){ int m, n, divided[5000], count, idx, flag; char decimal[5000]; while(scanf("%d%d", &m, &n) != EOF){ count = 0,

UVa202 Repeating Decimals

#include <stdio.h>#include <map>using namespace std; int main(){    int a, b, c, q, r, places;    map<int, int> rmap;    pair<map<int, int>::iterator, bool> pr;    int qarr[50];    while (scanf("%d %d", &a, &

UVa 202 大数除法

背景:1_WA:忘了每个答案之间有一个空白行!2_WA:没看见等号左右两边都有空格!!!!!!!! 思路:整数和小数分开来求,整数部分直接用整型除法,小数部分:分子=(分子%分母)*10.并且把每个分子储存在str[0]中,当出现已经出现过的分子时,小数部分开始循环! 学习: 1.巢鸽原理(抽屉原理,狄利克雷原则): 简单的描述:如果有n个笼子,n+1只鸽子居住,则至少有一个笼子有两只鸽子. 一般化的描述(用高斯函数来叙述):将n个元素分到m个集合中,至少有一个集合中元素个数大于等于[(n-1)