Oracle 面试题:分数转小数并用括号标注循环节 — 字符串 / 哈希映射/面试辅助/代面试

84次阅读
没有评论

Given two integers, numerator and denominator, return the decimal representation of the fraction numerator/denominator as a string.

  • If the fractional part terminates, output the exact decimal (trailing zeros are acceptable).
  • If the fractional part is repeating, enclose the repeating sequence in parentheses.
  • You may assume denominator ≠ 0 and the sign should be handled correctly.

Examples

  • input = 12, 5 → output =“2.4”(or“2.40000”)
  • input = 1, 6 (0.1666…) → output =“0.1(6)”

长除法 模拟:整数部分直接相除;小数部分用“余数 → 小数位起始索引”的哈希表记录。一旦某个余数再次出现,说明从首次出现位置到当前位置为循环节,用括号包裹。注意:符号、溢出、去除前导零与终止条件(余数为 0)。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Oracle、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0