RecurringNumbers

本文介绍了一种将带有循环小数的十进制数转换为最简分数形式的方法。通过分析循环部分和非循环部分的小数,利用数学原理将其转换为分子分母形式,并确保分数不可再被简化。
部署运行你感兴趣的模型镜像

/*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;
}

}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
标题中的"EthernetIP-master.zip"压缩文档涉及工业自动化领域的以太网通信协议EtherNet/IP。该协议由罗克韦尔自动化公司基于TCP/IP技术架构开发,已广泛应用于ControlLogix系列控制设备。该压缩包内可能封装了协议实现代码、技术文档或测试工具等核心组件。 根据描述信息判断,该资源主要用于验证EtherNet/IP通信功能,可能包含测试用例、参数配置模板及故障诊断方案。标签系统通过多种拼写形式强化了协议主题标识,其中"swimo6q"字段需结合具体应用场景才能准确定义其技术含义。 从文件结构分析,该压缩包采用主分支命名规范,符合开源项目管理的基本特征。解压后预期可获取以下技术资料: 1. 项目说明文档:阐述开发目标、环境配置要求及授权条款 2. 核心算法源码:采用工业级编程语言实现的通信协议栈 3. 参数配置文件:预设网络地址、通信端口等连接参数 4. 自动化测试套件:包含协议一致性验证和性能基准测试 5. 技术参考手册:详细说明API接口规范与集成方法 6. 应用示范程序:展示设备数据交换的标准流程 7. 工程构建脚本:支持跨平台编译和部署流程 8. 法律声明文件:明确知识产权归属及使用限制 该测试平台可用于构建协议仿真环境,验证工业控制器与现场设备间的数据交互可靠性。在正式部署前开展此类测试,能够有效识别系统兼容性问题,提升工程实施质量。建议用户在解压文件后优先查阅许可协议,严格遵循技术文档的操作指引,同时需具备EtherNet/IP协议栈的基础知识以深入理解通信机制。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值