poj 1001 Exponentiation

来源:互联网 发布:网络维修工具包 编辑:程序博客网 时间:2024/06/02 08:12

写题的时候经常在discuss里面看到“java大法好”,的确很好啊偷笑,但是用多了会变笨(学长说),对于我们这些刚刚入坑的菜鸟,有时间和精力还是走c/c++模拟之路吧,毕竟入了坑就不能偷懒啊。

import java.math.*;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
double r=cin.nextDouble();
int n=cin.nextInt();
            BigDecimal over = BigDecimal.valueOf(r);
            //BigDecimal over = BigDecimal.ONE;
            String res=over.pow(n).stripTrailingZeros().toPlainString();
            if(res.startsWith("0"))
{
res=res.substring(1);
}
System.out.println(res);  
            //for(int i=0;i<n;i++)
            // over=over.multiply(num1);
            //System.out.println( over.stripTrailingZeros().toPlainString());
            //int temp = over.intValue();   //用此获得BigDecimal的整数位
            //if(temp==0)
            //System.out.println(new DecimalFormat(".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").format(over.stripTrailingZeros()));
            //System.out.println(new DecimalFormat(".00000000000000000000000000000000000000000000000000000000000000000000").format(over));
            //String out = over.toString();
            //System.out.println(out+'\n');
            //System.out.format(null, null, args);
}
}
}


通过博客http://www.cnblogs.com/tsw123/p/4378697.html学习了一点java处理大数的皮毛(感谢博主),而在这里http://www.bkjia.com/Javabc/767546.html又上一个台阶。


/*stripTrailingZeros
public BigDecimal stripTrailingZeros()返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal。


/*toPlainString
public String toPlainString()返回不带指数字段的此 BigDecimal 的字符串表示形式。对于具有正标度的值,小数点右边的数字个数用于指示标度。对于具有零或负标度的值,生成得到的字符串,好像将该值转换为在数值上等于具有零标度的值一样,并且好像零标度值的所有尾部零都出现在该结果中。 如果非标度值小于零,则整个字符串以减号 '-' ('-') 为前缀。如果非标度值为零或正数,则没有任何符号字符作为前缀。 注意,如果将此方法的结果传递到 string constructor,则只需要恢复此 BigDecimal 的数值;新的 BigDecimal 的表示形式可以有不同的标度。尤其是,如果此 BigDecimal 具有负标度,则在由字符串构造方法进行处理时,此方法产生的字符串将具有零标度。 (此方法的作用类似于 1.4 和更早版本中的 toString 方法。)


/*java中substring的用法
str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;
str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str;

/*startsWith

startsWith() 方法用于检测字符串是否以指定的前缀开始。
语法
public boolean startsWith(String prefix, int toffset)

public boolean startsWith(String prefix)
参数
prefix -- 前缀。
toffset -- 字符串中开始查找的位置。
返回值
如果字符串以指定的前缀开始,则返回 true;否则返回 false。


format()

参见http://blog.csdn.net/lonely_fireworks/article/details/7962171


参考网址:

http://www.bkjia.com/Javabc/767546.html

http://www.cnblogs.com/tsw123/p/4378697.html

http://jerval.iteye.com/blog/874983

http://www.runoob.com/java/java-string-startswith.html



后补(干净的去bug代码):


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()){double r=cin.nextDouble();int n=cin.nextInt();            BigDecimal over = BigDecimal.valueOf(r);            String res=over.pow(n).stripTrailingZeros().toPlainString();            if(res.startsWith("0")&&res.length()!=1)res=res.substring(1);System.out.println(res);  }}}

之前的代码对这组数据

000.10  1
000000  1
000.00  1
.00000  0
000010  1

中的第二、三个案例会输出空的字符串而不是“0”(但是也AC了),改正一下健壮性更强了。

0 0