Fraction to Recurring Decimal

题意:

给定两个整数a,b,一个除数一个被除数,得到结果,输出为字符串;

若结果为整数(2/1 = 2),则直接转成字符串 2;

若结果为小数(1/2 = 0.5),直接输出 0.5;

若结果为循环小数(2/3=0.6666)输出 0.(6),

分析:

若a%b==0,则说明结果为整数;

若不等于0,则可以分为两步:

首先取整除得到整数部分,然后取模,结果c,c*10/b得到小数部分第一位;(c*10 )%b得到新的c;接着下一轮直到出现重复的c,或者c为零结束;

出现重复的c说明会得到循环小数,出现c等于0,说明小数部分最终会结束;

http://www.programcreek.com/2014/03/leetcode-fraction-to-recurring-decimal-java/

public String fractionToDecimal(int numerator, int denominator) {
	if (numerator == 0)
		return "0";
	if (denominator == 0)
		return "";
 
	String result = "";
 
	// is result is negative
	if ((numerator < 0) ^ (denominator < 0)) {
		result += "-";
	}
 
	// convert int to long
	long num = numerator, den = denominator;
	num = Math.abs(num);
	den = Math.abs(den);
 
	// quotient 
	long res = num / den;
	result += String.valueOf(res);
 
	// if remainder is 0, return result
	long remainder = (num % den) * 10;
	if (remainder == 0)
		return result;
 
	// right-hand side of decimal point
	HashMap<Long, Integer> map = new HashMap<Long, Integer>();
	result += ".";
	while (remainder != 0) {
		// if digits repeat
		if (map.containsKey(remainder)) {
			int beg = map.get(remainder); 
			String part1 = result.substring(0, beg);
			String part2 = result.substring(beg, result.length());
			result = part1 + "(" + part2 + ")";
			return result;
		}
 
		// continue
		map.put(remainder, result.length());
		res = remainder / den;
		result += String.valueOf(res);
		remainder = (remainder % den) * 10;
	}
 
	return result;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android resource fraction refers to the ability to specify resource values as fractions or percentages of a parent resource. This is commonly used in Android development to create responsive layouts that adapt to different screen sizes and resolutions. For example, instead of specifying a fixed width or height for a view, you can use resource fractions to define the size relative to the parent container. This allows the view to scale proportionally when the container's size changes. To use resource fractions in Android, you can define them in XML resource files using the "fraction" unit. For instance, you can set the width of a view to half of the parent container by using a fraction value of "0.5". Here's an example of how you can use resource fractions in an XML layout file: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/blue" /> </LinearLayout> ``` In this example, two views are placed inside a LinearLayout with a vertical orientation. Both views have a width defined as half of the parent container, using the "@fraction/0.5" resource value. Using resource fractions can help create more flexible and responsive UI designs in Android applications.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值