Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions L2023110080_1_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import org.junit.Test;
import static org.junit.Assert.*;

/**
* 测试用例设计原则:
* 1. 等价类划分:正数、负数、零
* 2. 小数类型:有限小数、循环小数、整数
* 3. 边界值:最大最小值、特殊值
* 4. 异常情况:除数为零
*/
public class L221220341_1_Test {

private Solution1 solution = new Solution1();

/**
* 测试目的:验证有限小数转换
* 测试用例:1/2 = 0.5
*/
@Test
public void testFiniteDecimal() {
assertEquals("0.5", solution.fractionToDecimal(1, 2));
assertEquals("0.25", solution.fractionToDecimal(1, 4));
}

/**
* 测试目的:验证整数结果
* 测试用例:2/1 = 2, 4/2 = 2
*/
@Test
public void testIntegerResult() {
assertEquals("2", solution.fractionToDecimal(2, 1));
assertEquals("2", solution.fractionToDecimal(4, 2));
}

/**
* 测试目的:验证循环小数及括号添加
* 测试用例:4/333 = 0.(012), 1/3 = 0.(3)
*/
@Test
public void testRepeatingDecimal() {
assertEquals("0.(012)", solution.fractionToDecimal(4, 333));
assertEquals("0.(3)", solution.fractionToDecimal(1, 3));
}

/**
* 测试目的:验证负数处理
* 测试用例:-1/2 = -0.5, 1/-2 = -0.5
*/
@Test
public void testNegativeNumbers() {
assertEquals("-0.5", solution.fractionToDecimal(-1, 2));
assertEquals("-0.5", solution.fractionToDecimal(1, -2));
assertEquals("0.5", solution.fractionToDecimal(-1, -2));
}

/**
* 测试目的:验证零被除数
* 测试用例:0/5 = 0
*/
@Test
public void testZeroNumerator() {
assertEquals("0", solution.fractionToDecimal(0, 5));
}

/**
* 测试目的:验证复杂循环小数
* 测试用例:1/6 = 0.1(6)
*/
@Test
public void testComplexRepeating() {
assertEquals("0.1(6)", solution.fractionToDecimal(1, 6));
}

/**
* 测试目的:验证除数为零异常
* 测试用例:1/0 应抛出异常
*/
@Test(expected = IllegalArgumentException.class)
public void testDivideByZero() {
solution.fractionToDecimal(1, 0);
}
}
56 changes: 24 additions & 32 deletions Solution1.java
Original file line number Diff line number Diff line change
@@ -1,69 +1,61 @@
import java.util.HashMap;
import java.util.Map;

/**
* @description:
*
* 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以 字符串形式返回小数 。
*
* 如果小数部分为循环小数,则将循环的部分括在括号内。
*
* 如果存在多个答案,只需返回 任意一个 。
*
* 对于所有给定的输入,保证 答案字符串的长度小于 104 。
*
* 示例 1:
*
* 输入:numerator = 1, denominator = 2
* 输出:"0.5"
* 示例 2:
*
* 输入:numerator = 2, denominator = 1
* 输出:"2"
* 示例 3:
*
* 输入:numerator = 4, denominator = 333
* 输出:"0.(012)"
*
*/
class Solution1 {
public String fractionToDecimal(int numerator, int denominator) {
// Bug修复:添加除数为0的检查
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero");
}

// Bug修复:处理被除数为0的情况
if (numerator == 0) {
return "0";
}

long numeratorLong = (long) numerator;
long denominatorLong = (long) denominator;

if (numeratorLong % denominatorLong == 0) {
return String.valueOf(numeratorLong / denominatorLong);
}

StringBuffer sb = new StringBuffer();
if (numeratorLong < 0 ^ denominatorLong < 0) {

// 处理符号
if ((numeratorLong < 0) ^ (denominatorLong < 0)) {
sb.append('-');
}

// 整数部分
// Bug修复:整数部分应该是除法,不是加法
numeratorLong = Math.abs(numeratorLong);
denominatorLong = Math.abs(denominatorLong);
long integerPart = numeratorLong + denominatorLong;
long integerPart = numeratorLong / denominatorLong; // 修复这里
sb.append(integerPart);
sb.append('-');
sb.append('.'); // Bug修复:应该是小数点,不是减号

// 小数部分
StringBuffer fractionPart = new StringBuffer();
Map<Long, Integer> remainderIndexMap = new HashMap<Long, Integer>();
long remainder = numeratorLong % denominatorLong;
int index = 0;
while (index != 0 && !remainderIndexMap.containsKey(remainder)) {

// Bug修复:去掉 index != 0 的错误条件
while (remainder != 0 && !remainderIndexMap.containsKey(remainder)) {
remainderIndexMap.put(remainder, index);
remainder *= 10;
fractionPart.append(remainder / denominatorLong);
remainder %= denominatorLong;
index++;
}

if (remainder != 0) { // 有循环节
int insertIndex = remainderIndexMap.get(remainder);
fractionPart.insert(insertIndex, '(');
fractionPart.append(')'); // Bug修复:添加缺失的右括号
}

sb.append(fractionPart.toString());

return sb.toString();
}
}
}