`
mabusyao
  • 浏览: 247507 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

RecurringNumbers

J# 
阅读更多

/*Problem Statement

A rational number is defined as a/b, where a and b are integers, and b is greater than 0.

Furthermore, a rational number can be written as a decimal that has a group of digits that repeat indefinitely.

A common method of writing groups of repeating digits is to place them inside parentheses like 2.85(23) = 2.852323 ... 23...

Given a decimal representation of a rational number in decimalNumber, convert it to a fraction formatted as "numerator/denominator",

where both numerator and denominator are integers.

The fraction must be reduced. In other words, the denominator must be as small as possible, but greater than zero.

Definition

Class: RecurringNumbers
Method: convertToFraction
Parameters: String
Returns: String
Method signature: String convertToFraction(String decimalNumber)
(be sure your method is public)

Constraints
- decimalNumber will have between 3 and 10 characters inclusive.
- decimalNumber will contain only characters '0' - '9', '.', '(' and ')'.
- The second character in decimalNumber will always be '.'.
- There will be at most one '(' and ')' in decimalNumber.
- '(' in decimalNumber will be followed by one or more digits ('0' - '9'), followed by ')'.
- ')' in decimalNumber will not be followed by any other character.
Examples
0) "0.(3)" Returns: "1/3" 0.(3) = 0.333... = 1/3
1) "1.3125" Returns: "21/16" Note there are no recurring digits here, although we could write it as 1.3125(0) or 1.3124(9).
2) "2.85(23)" Returns: "14119/4950"
2.85(23) = 2.852323... = 285/100 + 23/9900 = 28238/9900 = 14119/4950. Make sure to reduce the fraction, as shown in the final step.
3) "9.123(456)" Returns: "3038111/333000"
4) "0.111(1)" Returns: "1/9"
5) "3.(000)" Returns: "3/1"

This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited.
(c)2003, TopCoder, Inc. All rights reserved. */

package recurringNumbers;

public class RecurringNumbers {
static String intPart = null;
static String stablePart = null;
static String recPart = null;

private static void checkFormat(String num) throws Exception{
if(num.indexOf(".") == -1) {
Integer.valueOf(num).intValue();
intPart = num;
}
else {
String[] values = new String[2];
values[0] = num.substring(0, num.indexOf("."));
values[1] = num.substring(num.indexOf(".") + 1);
//if(values.length != 2) throw new NumberFormatException();

Integer.valueOf(values[0]).intValue();
intPart = values[0];

if(!values[1].contains("(")) {
Integer.valueOf(values[1]).intValue();
stablePart = values[1];
} else {
if(! values[1].contains(")") || values[1].indexOf("(") > values[1].indexOf(")")) throw new NumberFormatException();

if(values[1].indexOf("(") != 0){
Integer.valueOf(values[1].substring(0, values[1].indexOf("("))).intValue();
stablePart = values[1].substring(0, values[1].indexOf("("));
}

Integer.valueOf(values[1].substring(values[1].indexOf("(") + 1, values[1].indexOf(")"))).intValue();
recPart = values[1].substring(values[1].indexOf("(") + 1, values[1].indexOf(")"));
}
}
}

public static String convertToFraction(String decimalNumber) throws Exception{

checkFormat(decimalNumber);

Real realInt = null;
realInt = new Real(Integer.valueOf(intPart).intValue(), 1);

Real realStable = null;
if(stablePart != null) {
StringBuffer temp = new StringBuffer();
temp.append("1");
for (int i = 0; i < stablePart.length(); i++) {
temp.append("0");
}
realStable = new Real(Integer.valueOf(stablePart).intValue(), Integer.valueOf(temp.toString()).intValue());
}

Real realRec = null;
if(recPart != null) {
StringBuffer temp = new StringBuffer();
for (int i = 0; i < recPart.length(); i++) {
temp.append("9");
}
if(stablePart != null && stablePart.length() > 0) {
for (int j = 0; j < stablePart.length(); j++) {
temp.append("0");
}
}
realRec = new Real(Integer.valueOf(recPart).intValue(), Integer.valueOf(temp.toString()).intValue());
}

Real result = realInt;
if(realStable != null) result = Real.plus(result, realStable);
if(realRec != null) result = Real.plus(result, realRec);


return result.show();
}


}

class Real {
int num = 0;
int den = 0;

Real(int num, int den){
int temp = gcd(num, den);
if(temp != 0) {
this.num = num/temp;
this.den = den/temp;
}
else {
this.num = num;
this.den = den;
}
}

int gcd(int a, int b) {
if (b == 0) return a;

if (b > a) return gcd(b, a);
else return gcd(b, a % b);
}

String show() {
StringBuffer temp = new StringBuffer();
temp.append("result is:").append(" " + this.num).append("/" + this.den);
return temp.toString();
}

static Real plus(Real a, Real b) {
Real temp = new Real(a.num * b.den + a.den * b.num, a.den * b.den);
return temp;
}

}

分享到:
评论

相关推荐

    工艺计算MBBR.xls

    污水处理计算书

    object-tracking.zip

    object-tracking.zip

    pyopenjtalk-0.3.3

    win10/win11下使用, 包含pyopenjtalk-0.3.3-cp39-cp39-win_amd64.whl,pyopenjtalk-0.3.3-cp310-cp310-win_amd64.whl,pyopenjtalk-0.3.3-cp311-cp311-win_amd64.whl三个版本的whl文件,解决GPT_SoVITS中pip install安装pyopenjtalk失败。

    613155687470549安卓鸿蒙手机版_10.7.6.6.apk

    613155687470549安卓鸿蒙手机版_10.7.6.6.apk

    初识Flask的md格式文件

    初识Flask的md格式文件

    基于stm32f103C8T6智能台灯设计与制作

    本次设计是系统介绍了智能台灯的应用背景、设计原理、软硬件电路等。以STM32作为主控,应用定时模块、人体感应模块、光敏模块,使得智能台灯具备调光,监督使用者的坐姿,节能,时间提醒等功能。有五级自动、手动灯光。ds1302实时时钟模块、HCSR04超声波模块、dht11温湿度模块、HCSR501人体感应模块、光敏电阻模块和无源蜂鸣器模块。

    基于matlab实现模拟退火算法计算函数最小值以及SVM参数寻优.rar

    基于matlab实现模拟退火算法计算函数最小值以及SVM参数寻优.rar

    麦肯锡-工具与方法工作手册gl.ppt

    麦肯锡-工具与方法工作手册gl.ppt

    客流量预测.rar客流量预测.rar客流量预测.rar

    客流量预测.rar客流量预测.rar客流量预测.rar

    基于VB+access实现大气污染模型(系统+翻译+论文+开题).zip

    【项目资源】:基于VB+access实现大气污染模型(系统+翻译+论文+开题).zip 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。

    ASP+ACCESS网上远程教育网毕业设计(调研报告+源代码+设计说明书+答辩).zip

    ASP+ACCESS网上远程教育网毕业设计(调研报告+源代码+设计说明书+答辩).zip

    基于matlab实现脑电信号分析MATLAB工具箱,是做信号处理的必备工具,尤其针对生物医学信号处理.rar

    基于matlab实现脑电信号分析MATLAB工具箱,是做信号处理的必备工具,尤其针对生物医学信号处理.rar

    dephi+sqlserver2000题库与试卷生成系统.zip

    dephi+sqlserver2000题库与试卷生成系统.zip

    ASP+ACCESS学生信息管理系统设计(源代码+设计说明书).zip

    ASP+ACCESS学生信息管理系统设计(源代码+设计说明书).zip

    CD销售管理系统JSP+SQL(源代码+设计说明书+英文文献).zip

    CD销售管理系统JSP+SQL(源代码+设计说明书+英文文献).zip

    AO工艺设计计算.xls

    污水处理计算书

    node-v12.21.0-linux-armv7l.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v10.21.0-linux-ppc64le.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    ASP+ACCESS房产信息管理系统(源代码+设计说明书).zip

    ASP+ACCESS房产信息管理系统(源代码+设计说明书).zip

    ASP+ACCESS在线手机销售系统(设计说明书+源代码).zip

    ASP+ACCESS在线手机销售系统(设计说明书+源代码).zip

Global site tag (gtag.js) - Google Analytics