[North Central NA Contest 2018] Rational Ratio

2023-11-08

Description

Every (positive) rational number can be expressed as a ratio of two (positive) integers. However, in decimal form, rational numbers often have an infifinitely repeating pattern, e.g., 1/7 = 0.142857142857142857… A convenient way of writing this repeating pattern is to put a bar over the fifirst occurrence of the repeating part, so 1/7 would be written:

Given a rational number consisting of a series of digits, followed by a decimal point, followed by more digits, and then a number indicating how many of the rightmost digits repeat (i.e., the number of digits under the bar), your task is to find the ratio of two integers, in the most reduced form, that represent the same rational number. For example, for the input “0.142857 6” you should fifind 1/7.

Input

The input will be a single line with two numbers separated by one space. The fifirst number will consist of 1 to 3 digits (0–9), followed by a decimal point, followed by 1 to 11 digits (0–9), representing the decimal form of the number, possibly with leading zeros. The second number will be a positive integer indicating how many of the rightmost digits of the preceding number repeat. The fifirst number will always be greater than 0. The second number will never be less than 1 nor larger than the number of digits to the right of the decimal point.

Output

Print the corresponding fraction in its most reduced form, that is, the fraction with the smallest possible integer values in the numerator and denominator.

Samples

Input Copy

0.142857 6

Output

1/7

Input Copy

1.6 1

Output

5/3

Input Copy

123.456 2

Output

61111/495

题意:
将一个无线循环浮点数 转换为分数的形式,给定循环的位数

规律:
[该规律将附到做题总结第26条] 传送门
先只是考虑小数点后的部分:
对于一个浮点数 0. a b c d e f 0.abcdef 0.abcdef,假如它的循环节长度为6,即循环节为 a b c d e f abcdef abcdef,那么可以将其转换为
a b c d e f / 999999 abcdef/999999 abcdef/999999,循环节的长度是多少,就除以一个长度为多少的每位为9的数
假如小数点后面的数字不全属于循环节的部分,那么说我们就可以将不属于循环节的部分挪到整数部分,然后最终再把结果除一下即可

string s, t;
int n, flag;
ll zheng, pre, jie;
ll divid;
int siz;
int main() {
	cin >> s >> n;
	int len = s.size();
	for (int i = 0; i < len; i++) {
		if (s[i] == '.') {
			flag = 1;
			continue;
		}
		if (!flag) {
			zheng *= 10;
			zheng += s[i] - '0';
		}
		else {
			t.push_back(s[i]);
		}
	}
	int sizFen = t.size();
	for (int i = sizFen - n; i < sizFen; i++) {
		jie *= 10;
		jie += t[i] - '0';
	}
	int lenPre = sizFen - n;
	for (int i = 0; i < sizFen - n; i++) {
		pre *= 10;
		pre += t[i] - '0';
	}
	for (int i = 1; i <= lenPre; i++) {
		zheng *= 10;
	}
	zheng += pre;

	ll fenzi = 0, fenmu = 0;
	fenzi = jie;
	for (int i = 1; i <= n; i++) {
		fenmu *= 10;
		fenmu += 9;
	}
	fenzi += zheng * fenmu;
	//right pos:
	for (int i = 1; i <= lenPre; i++) {
		fenmu *= 10;
	}
	ll gd = gcd(fenzi, fenmu);
	cout << fenzi / gd << '/' << fenmu / gd << endl;
	return 0;
}
/**


**/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[North Central NA Contest 2018] Rational Ratio 的相关文章

随机推荐

  • vite和webpack的区别

    vite和webpack的区别 从底层原理上来说 Vite是基于esbuild预构建依赖 而esbuild是采用go语言编写 因为go语言的操作是纳秒级别 而js是以毫秒计数 所以vite比用js编写的打包器快10 100倍 Vite和We
  • 成为合格管理者的几个关键词

    http www csdn net article 2014 05 05 2819612 Management 职业通路是狭窄的 金字塔 结构很好地描绘了每个人在职场将要走过的路 在职位与薪酬待遇紧密挂钩的当今职场 芸芸技术专家总有一天会面
  • 【Neo4j】第 10 章:图嵌入 - 从图到矩阵

    大家好 我是Sonhhxg 柒 希望你看完之后 能对你有所帮助 不足请指正 共同学习交流 个人主页 Sonhhxg 柒的博客 CSDN博客 欢迎各位 点赞 收藏 留言 系列专栏 机器学习 ML 自然语言处理 NLP 深度学习 DL fore
  • Linux 如何查看文件夹的大小(du、df、ls、find)

    Linux 如何查看文件夹的大小 du df ls find 参考链接 https blog csdn net Cappuccino jay article details 125168388 一 du 命令 查看当前目录和子目录文件夹 文
  • 微信小程序中下载app的方法

    微信小程序中下载app的方法 因为微信小程序本身是不推荐引流到外部的 所以正规的方法其实都是被禁止掉的 大致方向是打开内部浏览器 进入应用宝下载页面 如果app接入应用宝 或者是通过右上角的在系统浏览器中打开 自动跳转到App Store中
  • XML中常用转义符

    编写XML代码常用转译符 逻辑与 amp lt 小于 lt gt 大于 gt 双引号 quot XML实体中不允许出现 lt gt 等特殊字符 否则XML语法检查时将出错 如果编写的XML文件必须包含这些字符 则必须分别写成 amp lt
  • vue实现前端人机验证

    最简单的一个例子 安装插件 npm install vue puzzle vcode save
  • 微信模板消息,动态配置

    微信模板消息 扫码关注公众号 模板消息配置数据库表设计 SET NAMES utf8mb4 SET FOREIGN KEY CHECKS 0 Table structure for kl b template msg DROP TABLE
  • js设置全屏显示和取消全屏显示,实现F11功能

    点击后
  • 通讯协议022——全网独有的OPC AE知识四之接口(六)

    本文简单介绍OPC AE规范的接口IConnectionPointContainer的相关知识 更多通信资源请登录网信智汇 wangxinzhihui com OPC AE规范描述了OPC事件服务器应该实现的对象和接口 实现在多个OPC客户
  • 计算机视觉——利用openCV调用本地摄像头采集图片并截图保存

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 前言 一 采集方式 二 代码部分 三 效果演示 前言 由于本人研究生课题是深度学习目标检测方向 因此需要大量的数据集 在本地采样时 可以利用openCV提供的库函数
  • Failed to create SubnetManager: error retrieving pod spec for ‘kube-system/kube-flannel-ds-amd64-xxx

    kubelet报错如下 kubelet 3807 E1215 18 01 25 516905 3807 aws credentials go 77 while getting AWS credentials NoCredentialProv
  • 从UE4到UE5:工程及资产迁移的原理和一般方法

    按照官方建议 明年早些时候发布UE5正式版本 UE5EA仅用于新技术探索和尝鲜 项目开发还是应该在UE4上进行 现在迁移只是为了解新的features 提前规划哪些可能用于现在的工程 2022 4 8更新 UE5正式版已经发布 喜大普奔 赶
  • Java线程:新特征-有返回值的线程

    本文转载至 http lavasoft blog 51cto com 62575 222082 在Java5之前 线程是没有返回值的 常常为了 有 返回值 破费周折 而且代码很不好写 或者干脆绕过这道坎 走别的路了 现在Java终于有可返回
  • pandas数据处理基础——筛选指定行或者指定列的数据

    pandas主要的两个数据结构是 series 相当于一行或一列数据机构 和DataFrame 相当于多行多列的一个表格数据机构 本文为了方便理解会与excel或者sql操作行或列来进行联想类比 1 重新索引 reindex和ix 上一篇中
  • SPP空间金字塔池化(spatial pyramid pooling, SPP)原理与pytorc实现

    1 为什么需要SPP 过去的卷积神经网络CNN由卷积层 全连接层组成 其中卷积层对于输入数据的大小并没有要求 唯一对数据大小有要求的则是第一个全连接层 因此基本上所有的CNN都要求数据数据固定大小 例如著名的VGG模型则要求输入数据大小是
  • "ORA-00942: 表或视图不存在 "的原因和解决方法

    采用Oracle数据库 使用Powerdesigner设计 生成Sql文件导入后查询出现 ORA 00942 表或视图不存在 很是郁闷 这个问题以前出现过 当初解决了 但因好久没有使用 这次竟然忘了 害得我浪费了好些时间 为了避免再次忘记
  • 消息中间件(MQ)

    一 什么是消息中间件 关注于数据的发送和接收 利用高效可靠的异步消息传递机制集成分布式系统 通过提供消息传递和消息排队模型 它可以在分布式环境下扩展进程间的通信 二 为什么需要消息中间件 1 系统解耦 假设你有个系统A 这个系统A会产出一个
  • 数据治理总结

    项目背景 前提 参与人员均了解熟悉数据中心 业务痛点 始于一次吐槽大会 1 开发及使用人员信息不对称 2 表中字段增减随意 3 相似数据冗余 4 定制化表过多 扩展功能不足 维护成本高 5 缺少注释 全凭猜测 浪费时间 项目计划 1 确定治
  • [North Central NA Contest 2018] Rational Ratio

    Description Every positive rational number can be expressed as a ratio of two positive integers However in decimal form