Repeating Decimals UVA - 202
The decimal expansion of the fraction / is 0.03, where the is used to indicate that the cycle
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
/ 0.1()
/ .()
/ 0.004()
/ .()
/ 0.6()
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 / is , which begins at position (as opposed to
which begins at positions or and as opposed to which begins at positions or ).
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 . 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 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 places, place a left parenthesis where the cycle
begins — it will begin within the first places — and place ‘...)’ after the 50th digit.
Sample Input Sample Output
/ = 3.04()
= number of digits in repeating cycle
/ = .()
= number of digits in repeating cycle
/ = .(...)
= 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;
}
Repeating Decimals UVA - 202的更多相关文章
- uva 202(Repeating Decimals UVA - 202)
题目大意 计算循环小数的位数,并且按照格式输出 怎么做 一句话攻略算法核心在于a=a%b*10,用第一个数组记录被除数然后用第二个数组来记录a/b的位数.然后用第三个数组记录每一个被除数出现的位置好去 ...
- Repeating Decimals UVA - 202---求循环部分
原题链接:https://vjudge.net/problem/UVA-202 题意:求一个数除以一个数商,如果有重复的数字(循环小数),输出,如果没有,输出前50位. 题解:这个题一开始考虑的是一个 ...
- UVa 202 Repeating Decimals(抽屉原理)
Repeating Decimals 紫书第3章,这哪是模拟啊,这是数论题啊 [题目链接]Repeating Decimals [题目类型]抽屉原理 &题解: n除以m的余数只能是0~m-1, ...
- UVa 202 - Repeating Decimals
给你两个数,问你他们相除是多少,有无限循环就把循环体括号括起来 模拟除法运算 把每一次的被除数记下,当有被除数相同时第一个循环就在他们之间. 要注意50个数之后要省略号...每一次输出之后多打一个回车 ...
- UVa 202 Repeating Decimals【模拟】
题意:输入整数a和b,输出a/b的循环小数以及循环节的长度 学习的这一篇 http://blog.csdn.net/mobius_strip/article/details/39870555 因为n% ...
- 【习题 3-8 UVA - 202】Repeating Decimals
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 余数出现循环节. 就代表出现了循环小数. [代码] #include <bits/stdc++.h> using nam ...
- 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 ...
- UVa202 Repeating Decimals
#include <stdio.h>#include <map>using namespace std; int main(){ int a, b, c, q, r, p ...
- uva 202
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> # ...
随机推荐
- Java Web项目中缺少Java EE 6 Libraries怎么添加
Java Web项目中缺少Java EE 6 Libraries怎么添加 具体步骤如下: 1.项目名称上点击鼠标右键,选择"Build Path-->Configure Build P ...
- freemarker处理空值
freemarker处理空值 1.设计思路 (1)封装学生类和课程类 (2)新建学生课程页面ftl文件 (3)创建测试方法 2.封装课程类 Course.java: /** * @Title:Cour ...
- (七)java类和对象
一个类定义一个新的数据类型,也就是定义了一个逻辑框架,定义了它的成员之间的关系.可以通过这种类型来声明该类型的对象,通过new关键词来实例化对象,也就是为该类型的对象动态的分配物理内存空间,这个分配过 ...
- 从DataTable中查询数据
/// <summary> /// 从DataTable中查询数据 /// </summary> /// <param name="tb">待处 ...
- 在visual studio 2017中配置Qt
简述 这两天因为软件工程课要用vs2017写一个C++的GUI界面,就打算学习Qt,但是vs2017配置起Qt来不像vs2013,15那么简单,而且现在网上对于vs2017配置Qt的教程很少,也不详细 ...
- python 字符串格式化输出 %d,%s及 format函数
旧式格式化方式:%s,%d 1.顺序填入格式化内容 s = "hello %s, hello %d"%("world", 100) print(s) 结果: ' ...
- 从零一起学Spring Boot之LayIM项目长成记(三) 数据库的简单设计和JPA的简单使用。
前言 今天是第三篇了,上一篇简单模拟了数据,实现了LayIM页面的数据加载.那么今天呢就要用数据库的数据了.闲言少叙,书归正传,让我们开始吧. 数据库 之前有好多小伙伴问我数据库是怎么设计的.我个人用 ...
- 【SDOI2009】HH的项链 (莫队)
题面 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的 ...
- 一次日语翻译的Chrome插件开发经历
序言 去年7月刚过了日语N2,想着今年考个N1,为了加深日语文化的了解,还有学习日语,平时免不了经常上日语网站. 但是毕竟水平有限,所以不免遇到不认识的单词,日语单词的一个特点就是很多单词你知道是什么 ...
- Mysql遇到 is marked as crashed and should be repaired 问题解决方法
遇到 找到mysql的安装目录的bin/myisamchk工具,在命令行中输入: myisamchk -c -r /var/lib/mysql/ambari/alert_current.MYI 问题解 ...