- 浏览: 247507 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
mabusyao:
漠北空城 写道请问下,你这个是JDK版本是多少呢?!忘记了,应 ...
HashMap 源码解读 -
漠北空城:
请问下,你这个是JDK版本是多少呢?!
HashMap 源码解读 -
schumee:
完美团队~
项目沉思录 - 1.1 -
winie:
整理下 搞成引擎嘛 国产需要这样的engine
简单工作流引擎 -
mabusyao:
某位同学给我提供的堪称完美的解决方案:1. 将三个int数组放 ...
CraneWork
/*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;
}
}
发表评论
-
大数据下的实体解析
2016-07-07 12:03 671大数据时代的实体解析困境 <!--[if !sup ... -
中文相似度匹配算法
2015-12-30 14:44 1921基于音形码的中文字 ... -
各种语言写的wordcount
2015-09-24 16:07 0Java版本: String input ... -
数组双指针算法的研究
2015-07-14 16:59 2418双指针算法在数组/链 ... -
摩尔投票法
2015-06-30 20:13 18275摩尔投票法 提问: 给定一个int型数组,找出该数 ... -
(转)最长回文字串算法
2015-01-18 14:30 1396来自(http://blog.163.com/zhaohai ... -
STRUTS2 源码 - Logging System
2012-05-24 08:51 1357看了STRUTS2的源码,了解了它的logging系统,觉得还 ... -
在线词典的数据结构实现。
2012-05-18 08:37 0昨天在网上看到了一道百度的面试题: Baidu写道 ... -
存储中间计算结果的动态规划算法
2012-04-18 15:50 1178public class RodCutting { ... -
java写的四则运算器
2011-08-19 22:19 2660本打算做一个从RE到NFA的转换器,思路已经理清了,但是在动手 ... -
什么是最小二乘拟合
2007-09-14 21:27 923对于一个数据点(x1, y1), ... (xn, yn)的集 ... -
palindrome
2008-07-14 17:39 950package palindrome;/*Problem St ... -
CraneWork
2008-07-14 19:14 952/*Problem Statement There are ... -
SalesRouting
2008-07-15 10:53 739package salesRouting;/*Problem ... -
FactorialSystem
2008-07-21 19:36 817/*Problem StatementIn the facto ... -
HardDuplicateRemover
2008-07-23 15:17 878/*Problem StatementWe have a se ... -
BlockStructure
2008-07-23 20:55 760/*Problem StatementA group of v ... -
SkipStones
2008-07-28 17:31 790/*Problem StatementWhen a stone ... -
TheaterVisit
2008-07-28 17:31 741/*Problem StatementYou want to ... -
SquareDigits
2010-07-06 12:36 990Problem Statement ***Note: ...
相关推荐
污水处理计算书
object-tracking.zip
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
初识Flask的md格式文件
本次设计是系统介绍了智能台灯的应用背景、设计原理、软硬件电路等。以STM32作为主控,应用定时模块、人体感应模块、光敏模块,使得智能台灯具备调光,监督使用者的坐姿,节能,时间提醒等功能。有五级自动、手动灯光。ds1302实时时钟模块、HCSR04超声波模块、dht11温湿度模块、HCSR501人体感应模块、光敏电阻模块和无源蜂鸣器模块。
基于matlab实现模拟退火算法计算函数最小值以及SVM参数寻优.rar
麦肯锡-工具与方法工作手册gl.ppt
客流量预测.rar客流量预测.rar客流量预测.rar
【项目资源】:基于VB+access实现大气污染模型(系统+翻译+论文+开题).zip 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。
ASP+ACCESS网上远程教育网毕业设计(调研报告+源代码+设计说明书+答辩).zip
基于matlab实现脑电信号分析MATLAB工具箱,是做信号处理的必备工具,尤其针对生物医学信号处理.rar
dephi+sqlserver2000题库与试卷生成系统.zip
ASP+ACCESS学生信息管理系统设计(源代码+设计说明书).zip
CD销售管理系统JSP+SQL(源代码+设计说明书+英文文献).zip
污水处理计算书
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.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