现在的位置: 首页 > 综合 > 正文

POJ 1001 Exponentiation

2013年09月05日 ⁄ 综合 ⁄ 共 726字 ⁄ 字号 评论关闭

题目链接:http://poj.org/problem?id=1001

 

分析:这是求一个大数的N次幂,但是有点注意的是如果尾部有0要去掉无效的0 前方有0.什么的要把前面的0也给去掉

源代码:

 

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while (sc.hasNext()) {

			String str = sc.nextLine();

			String[] nums = str.split(" ");
			
			BigDecimal num = new BigDecimal(nums[0]);
			int n;
			int i = 1;
			
			while (nums[i].equals("")) {// 如果中间多几个空格,会有空字符串,这里去空字符串
				i++;//6.7592  9这组测试数据  注意观察!!  否则提交会RE
			}

			n = Integer.parseInt(nums[i]);//将字符串转换成整型
			num = num.pow(n);
			num = num.stripTrailingZeros();// 去掉尾部的0
			String result = num.toPlainString();// 不带指数字段的此 BigDecimal 的字符串表示形式。
			if (result.startsWith("0.")) {//如果以0开始  但是如果用0却会WA
				result = result.substring(1); // 去掉前面的0
			}

			System.out.println(result);

		}

	}

}

抱歉!评论已关闭.