-
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
-
输入可能包含多个测试样例。
对于每个输入文件,第一行输入一个整数T,表示测试案例的数目,接下来的T行每行输入一个浮点数base和一个整数exponent,两个数中间用一个空格隔开。 -
对应每个测试案例,
输出一个浮点数代表答案,保留两位小数即可。 -
5 1.0 10 0.0 -5 1.0 0 1.2 5 2.0 -1
-
1.00e+00f INF 1.00e+00f 2.49e+00f 5.00e-01f
-
题目描述:
-
输入:
-
输出:
-
样例输入:
-
样例输出:
常规解法:直接采用循环。
高效解法:采用二分法的原理,减少乘的次数,使用移位操作把指数减少一倍。
java代码:
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.text.DecimalFormatSymbols;
import java.text.DecimalFormat;
public class Main {
public static StreamTokenizer stin;
public static StringBuilder sbout;
public static void main(String[] args) throws IOException {
stin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
sbout = new StringBuilder();
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setExponentSeparator("e");
dfs.setInfinity("INF");
DecimalFormat df = new DecimalFormat("0.00E00", dfs);
while (stin.nextToken() != StreamTokenizer.TT_EOF) {
int lines = (int) stin.nval;
while (lines-- > 0) {
stin.nextToken();
double base = stin.nval;
stin.nextToken();
int exponent = (int) stin.nval;
double result = pow(base, exponent);
String formatedOutput = df.format(result);
if ("INF".equals(formatedOutput)) {
sbout.append(formatedOutput);
} else {
if (!formatedOutput.contains("e-")) {
formatedOutput = formatedOutput.replace("e", "e+");
}
sbout.append(formatedOutput).append("f");
}
sbout.append("\n");
}
}
System.out.print(sbout);
}
public static double pow(double base, int exponent) {
if (isEqual(base, 0.0)) {
if (exponent < 0) {
return Double.POSITIVE_INFINITY;
} else {
return 0;
}
}
if (0 == exponent) {
return 1.0;
}
if (1 == exponent) {
return base;
}
int absE = Math.abs(exponent);
double result = pow(base, absE >> 1);
result *= result;
if (1 == (exponent & 1)) {
result *= base;
}
if (exponent < 0) {
result = 1.0 / result;
}
return result;
}
public static boolean isEqual(double d1, double d2) {
double epsilon = 0.00000001;
return Math.abs(d1 - d2) < epsilon;
}
#include <iostream>
#include <fstream>
using namespace std;
double PowerWithUnsignedExponent(double base, unsigned int absExponent)
{
double result = 1.0;
for (int i = 0; i < absExponent; i++)
result *= base;
return result;
}
bool equal(double num1, double num2)
{
if ((num2 - num1) > -0.0000001 && (num2 - num1) < 0.0000001)
return true;
return false;
}
bool g_InvalidInput = false;
double Power(double base, int exponent)
{
g_InvalidInput = false;
if (equal(base, 0.0) && exponent < 0)
{
g_InvalidInput = true;
return 0.0;
}
unsigned int absExponent = (unsigned int)exponent;
if (exponent < 0)
absExponent = (unsigned int)(-exponent);
double result = PowerWithUnsignedExponent(base, absExponent);
if (exponent < 0)
return 1.0 / result;
return result;
}
int main()
{
int T;
double base;
int exponent;
double result;
scanf("%d", &T);
while (T--)
{
scanf("%lf %d", &base, &exponent);
result = Power(base, exponent);
if (g_InvalidInput)
printf("INF\n");
else
printf("%.2ef\n", result);
}
return 0;
}
C代码:
#include <stdio.h>
int isInvalidInput = 0;
int equal(double num1,double num2) {
if((num1 - num2>-0.0000001) && (num1-num2) < 0.0000001) {
return 1;
}else return 0;
}
double PowerWithUnsignedExponent(double base , unsigned int exponent) {
double result = 1.0;
int i ;
for(i = 1 ; i <= exponent ; ++i) {
result *=base;
}
return result ;
}
double Power(double base,int exponent) {
isInvalidInput = 0;//输入合法
if(equal(base ,0.0) && exponent < 0 ) {//对0求倒数
isInvalidInput = 1 ;
return 0.0;
}
unsigned int absExponent = (unsigned int)(exponent);
if(exponent < 0) absExponent = (unsigned int)(-exponent);//先把指数转化为正数
double result = PowerWithUnsignedExponent(base,absExponent);
if(exponent < 0) result = 1.0 / result;
return result ;
}
int main() {
double a,result;
int b;
int i ,n;
while(scanf("%d",&n)!=EOF) {
for(i = 0 ; i < n ; i++) {
scanf("%lf %d",&a,&b);
result = Power(a,b);
if(isInvalidInput)printf("INF\n");
else printf("%.2ef\n",result);
}
}
return 1;
}
测试用例:
底数和指数分别设为正数、负数和零。
体会:
(1)本题主要考虑边界条件,考察思维的严密性;
(2) 浮点数我们不能直接采用等号判断两个数是否相等;