分数->小数

该博客探讨如何将分数表示为小数,特别是处理无限重复小数的情况。通过一个C语言实现的示例,解释了如何计算分数的整数部分、小数部分以及确定循环节的开始。当找到相同的余数时,标记循环节并将其括在括号内。
摘要由CSDN通过智能技术生成

一:问题描述

Description: As we know, a rational number is a number that can be represented as y/x where x and y are integers and y is not equal to zero. On the hand, a rational number could also be represented in decimal form and the resulting decimal is a finite decimal or an infinite repeating decimal. Now, you are given the x and y , please return the decimal form of y/x in string format, and enclose the repeating part for the infinite repeating decimal in parentheses, for example:
X=1, Y=2, return ‘0.5’
X=2, Y=3, return ‘0.(6)’
X=7, Y=15, return ‘0.4(6)’

二:斟酌题意:

其实就是将 一个 分数用 小数表示,如果是无限循环小数,那么用括号将循环节 括起来。事例已经说明这点。

三:思路以及方法

不妨先假设分子为26,分母为23。根据此实例加以分析。
我们先计算整数部分 pre = 26/23 ,小数部分 为 分子 numerator = 26 % 23 =3,分母denominator为 23。我们循环计算余数 remainder[i] = numerator *10 % denominator ,如

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值