刚刚开始用Java,代码难免不够简洁。
import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while (cin.hasNext()) { BigDecimal a = cin.nextBigDecimal(); BigDecimal b = cin.nextBigDecimal(); a = a.add(b); if (a.compareTo(BigDecimal.valueOf(0.0)) == 0) { System.out.println(0); continue; } String out = new String(a.toPlainString()); int l = out.length(); boolean flag = false; for (int i = 0; i < l; i++) if (out.charAt(i) == '.') flag = true; int q = l - 1; while (out.charAt(q) == '0') q--; if (out.charAt(q) == '.') q--; if (flag == false && out.charAt(q) != '.') q = l-1; int p = 0; while (out.charAt(p) == '0') p++; if (out.charAt(p) == '.') System.out.print(0); for (int i = p; i <= q; i++) System.out.print(out.charAt(i)); System.out.println(); } cin.close(); } }