1514:数值的整数次方 @jobdu
题目1514:数值的整数次方
最后一组数据不知道为什么无法通过,翻看了一下记录,发现用Java做的没有一个AC,于是就不浪费时间纠结了。
C++版本的可参考这两个:算法都是一样的
http://blog.csdn.net/SunnyYoona/article/details/14646053
http://blog.csdn.net/arcsinsin/article/details/12917119
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class S11 { public static void main(String[] args) throws FileNotFoundException { BufferedInputStream in = new BufferedInputStream(new FileInputStream("in.in")); System.setIn(in); Scanner cin = new Scanner(System.in); while (cin.hasNextInt()) { long n = cin.nextInt(); for(int i=0; i<n; i++){ double ret = power(cin.nextDouble(), cin.nextInt()); if(ret != Double.POSITIVE_INFINITY){ System.out.println(String.format("%.2ef", ret)); }else{ System.out.println("INF"); } } } } public static double power(double base, int n){ if(n == 0){ return 1; } if(n == 1){ return base; } if(n < 1){ return 1.0/power(base, -n); } if((n & 1) == 1){ // odd double tmp = power(base, (n-1)>>1); return tmp * tmp * base; }else{ double tmp = power(base, n>>1); return tmp * tmp; } } }