1001 . Exponentiation


Problems involving the computation(估计,计算) of exact values of very large magnitude(大小,量极) and precision are common. For example, the computation of the national debt(国债) is a taxing experience for many computer systems. 

This problem requires that you write a program to compute the exact value of R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. 


The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.


The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros(前导0) should be suppressed in the output. Insignificant(无关紧要的) trailing zeros(后补0) must not be printed. Don't print the decimal point(小数点) if the result is an integer.

Java.
//java 自带BigDecimal很方便。但需string类处理。

import java.math.BigDecimal;
import java.io.*;
import java.text.DecimalFormat.*;
import java.util.*;

public class Main
{
	public static void main(String args[])
	{
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext())
		{
			String a = cin.next();
			int b = cin.nextInt();
			BigDecimal f = new BigDecimal(a);
			BigDecimal g = new BigDecimal("1");
			for(int i =1 ;i <= b; i++)
			{
				g = g.multiply(f);
			}
			BigDecimal y = new BigDecimal(0);
			if(g.compareTo(y) == 0)
				System.out.println("0");  	//等于0时
			else {
				String result;
				result = g.toPlainString();
				int pos;
				int is = 1;
				pos = result.lastIndexOf(".");  
				for(int i = pos-1 ;i >= 0 ; i--)	//前导零的处理
				{
					if(result.charAt(i) !='0')
						is = 0;
				}
				if(is == 1)	//需要切割
				{
					result = result.substring(pos,result.length());
				}

				is = -1;
				pos = result.lastIndexOf(".");
				if(result.charAt(result.length()-1) == '0')
					for(int i = result.length()-1;i > pos; i--)
					{
						if(result.charAt(i) == '0')
						{
							is = i;
						}
						else break;
					}
					if(is != -1)
						if(is != pos + 1)
							result = result.substring(0,is);
						else 
							result = result.substring(0,is-1);

						System.out.println(result);
			}
			//String result = g.toPlainString();

		}
	}
}

//hasNext()是Scanner类的一个方法,判断是否有输入,有输入项,a .hasNext()为true,没有输入项,a .hasNext()为false
//compareTo比较此对象与指定对象的顺序。字典序。如果该对象小于、等于或大于指定对象,则分别返回负整数-1、零0或正整数1
//toPlainString():返回不带指数字段的此 BigDecimal 的字符串表示形式
//int lastIndexOf(int ch): 返回此字符串指定字符最后一次出现








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值