CSU 8月月赛 Decimal 小数化分数
2024-08-27 04:05:24
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1303 这个OJ很容易跪所以我贴一下题目
Description
任意一个分数都是有理数,对于任意一个有限小数,我们都可以表示成一个无限循环小数的形式(在其末尾添加0),对于任意一个无限循环小数都可以转化成一个分数。现在你的任务就是将任意一个无限循环小数转化成既约分数形式。所谓既约分数表示,分子和分母的最大公约数是1。
Input
有多组数据。
每组数据一行。输入为0.a1a2a3...ak(b1b2...bm)的形式,其中a1a2a3...ak为非循环部分,(b1b2b3..bm)为循环部分。数据保证非循环部分的长度k和循环部分的长度m不会超过8.
Output
对于每组测试数据输出A/B,其中A是分子,B是分母,A,B均为整数。
Sample Input
0.0(714285)
0.0(5)
0.9(671)
Sample Output
1/14
1/18
4831/4995
其实就是无限小数化分数 推一下发现对于0.abc(def)这样的小数
我们可以对其放大10^n倍
变成abc.(def)
再放大10^m倍
变成abcdef.(def)
然后令原数为X , 两式相减
得到方程(10^(n+m)-10^n) * X = (abcdef-abc)
求GCD化简即可...
PS : 我用系统pow(10,2)竟然会得到99...浮点数靠不住啊...结果自己写了一个...
/********************* Template ************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std; #define EPS 1e-8
#define MAXN 3000
#define MOD (int)1e9+7
#define PI acos(-1.0)
#define INF 0x7fffffff
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max3(a,b,c) (max(max(a,b),c))
#define min3(a,b,c) (min(min(a,b),c))
#define BUG cout<<"BUG! "<<endl
#define LINE cout<<"------------------"<<endl
#define L(t) (t << 1)
#define R(t) (t << 1 | 1)
#define Mid(a,b) ((a + b) >> 1)
#define lowbit(a) (a & -a)
#define FIN freopen("in.txt","r",stdin)
#pragma comment (linker,"/STACK:102400000,102400000") typedef long long LL;
// typedef unsigned long long ULL;
// typedef __int64 LL;
// typedef unisigned __int64 ULL;
LL gcd(LL a,LL b){ return b?gcd(b,a%b):a; }
//int lcm(int a,int b){ return a*b/gcd(a,b); } /********************* F ************************/
LL mpow(int a,int b){
LL ans = ;
for(int i = ; i < b ; i++){
ans *= a;
}
return ans;
}
char a[MAXN];
int main(){
while(gets(a)){
int len = strlen(a);
int i;
for(i = ; i < len ; i++)
if(a[i] == '.') break;
LL num = ;
int cnt1 = ;
for(i = i+ ; i < len ; i++){
if(a[i] == '(') break;
num = num* + (a[i]-'');
cnt1++;
}
LL num2 = ;
int cnt2 = ;
for(i = i+ ; i < len ; i++){
if(a[i] == ')') break;
num2 = num2* + (a[i]-'');
cnt2++;
}
LL p,q;
if(cnt2 == ){
LL r1 = num;
LL r2 = mpow(,cnt1);
p = r1/gcd(r1,r2);
q = r2/gcd(r1,r2);
}else{
LL r1 = mpow(,cnt1+cnt2) - mpow(,cnt1);
LL r2 = num*mpow(,cnt2)+num2 - num;
p = r2/gcd(r1,r2);
q = r1/gcd(r1,r2);
}
cout<<p<<"/"<<q<<endl; }
return ;
}
最新文章
- iOS 报错汇总
- 烂泥:ubuntu 14.04搭建OpenVPN服务器
- js控制固定div和随屏滚动div兼容多浏览器和纯css控制(来自网络)
- EDIT Ini写Ini配置
- python消息队列snakemq使用总结
- IOS 特定于设备的开发:监测Retina支持
- java加密算法入门(一)-算法概念及单向加密
- Kubernetes集群部署关键知识总结
- [转]Xcode编译生成ipa文件(因为两种证书所以打两个ipa)
- Eclipse中Git的使用以及IDEA中Git的使用
- ASP.NET Core Web 项目 发布的IIS7提示“HTTP Error 502.5 - Process Failure
- Java8新特性值Lambda --->;匿名函数
- ssh中文手册
- Intel Edison学习笔记(一)—— 刷系统
- Spark Core(四)用LogQuery的例子来说明Executor是如何运算RDD的算子(转载)
- linux下编译GD(freetype+libjpeg+libpng+gd-devel)
- objective c, property, copy
- SignalR 设计理念(二)
- Python Django 的学习资料
- Asp.Net 之 前台绑定常用总结
热门文章
- Scala之面向对象
- 工作日志:dispatch_once、网络缓存、分享问题
- pip源
- BZOJ 3637: Query on a tree VI LCT_维护子树信息_点权转边权_好题
- webpack(零工程构建一个前端项目)详解
- 一个Web报表项目的性能分析和优化实践(四):MySQL建立索引,唯一索引和组合索引
- Unity C# 关于设计模式的思考
- quick 3.3载入Spine问题
- WAS集群系列(5):集群搭建:步骤3:安装IHS软件
- 常见的C字符串处理函数的源代码