xindoo is
always here

hdoj 1753 (Java)


刚刚开始用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();
	}
}
打赏
未经允许不得转载:XINDOO » hdoj 1753 (Java)
分享到: 更多 (0)

评论 抢沙发

xindoo

联系我联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏