语言:java(没有用大数处理类,完全按照c++实现)
代码
package poj1001;
import java.util.Scanner;
/**
* Created by fant on 17-3-21.
*/
public class Main {
public static void main(String[] args) {
Main tool = new Main();
Scanner scan = new Scanner(System.in);
while (scan.hasNextBigDecimal()) {
String flo = scan.nextBigDecimal().toString();
int pow = scan.nextInt();
tool.handle(flo, pow);
}
}
void handle(String flo, int pow) {
int pointPos = flo.indexOf('.');
int resultPointPos = (pointPos == -1 ? 0 : flo.length() - pointPos - 1) * pow;
if (pointPos != -1) {
String front = flo.substring(0, pointPos);
String end = flo.substring(pointPos + 1);
flo = front + end;
}
String result = "1";
for (int i = 0; i < pow; i++) {
result = multiply(result, flo);
}
System.out.println(convert_to_result(result, resultPointPos));
}
String convert_to_result(String num, int pos) {
String result = "";
if (pos == 0) {
result = num;
} else {
String front = num.substring(0, num.length() - pos);
String end = num.substring(num.length() - pos);
result = front + "." + end;
}
int first_index_noZero = 0;
while (result.charAt(first_index_noZero) == '0') {
first_index_noZero++;
}
if (result.indexOf('.') != -1) {
result = result.substring(first_index_noZero);
int last_index_noZero = result.length() - 1;
while (result.charAt(last_index_noZero) == '0') {
last_index_noZero--;
}
if (result.charAt(last_index_noZero) == '.') {
result = result.substring(0, last_index_noZero);
} else {
result = result.substring(0, last_index_noZero + 1);
}
} else {
result = result.substring(first_index_noZero);
}
return result;
}
static void myPrint(Object obj) {
System.out.println(obj);
}
/**
* 取出字符串str在index下标处的整数
*
* @param str
* @param index
* @return
*/
int getIntFromIndex(String str, int index) {
return str.charAt(index) - '0';
}
String format(String str, int totalCount) {
int remainFrontZeroCount = totalCount - str.length();
String temp = "";
for (int i = 0; i < remainFrontZeroCount; i++) {
temp = temp + "0";
}
return temp + str;
}
/**
* 尾部添加0
*
* @param source
* @param count 尾部0的个数
* @return 处理后的字符串
*/
String appendZero(String source, int count) {
String temp = "";
for (int i = 0; i < count; i++) {
temp = temp + "0";
}
return source + temp;
}
/**
* 乘积的辅助函数,用于求和
*
* @param arr
* @return
*/
String add(String[] arr) {
String result = "";
int itemLength = arr[0].length();
int addition = 0;
for (int i = itemLength - 1; i >= 0; i--) {
int sum = 0;
for (int j = 0; j < arr.length; j++) {
sum = sum + getIntFromIndex(arr[j], i);
}
sum = sum + addition;
addition = sum / 10;
int newValue = sum - addition * 10;
result = newValue + result;
}
result = addition + result;
return result;
}
/**
* 处理整数乘法
*
* @param op1 第一个操作数
* @param op2 第二个操作数
* @return 乘积
*/
String multiply(String op1, String op2) {
String result = "";
int sumItemCount = op2.length();
String[] sumItemArr = new String[sumItemCount];
for (int i = op2.length() - 1; i >= 0; i--) {
int additionalItem = 0;
sumItemArr[i] = "";
int b = getIntFromIndex(op2, i);
for (int j = op1.length() - 1; j >= 0; j--) {
int a = getIntFromIndex(op1, j);
int ji = a * b + additionalItem;
additionalItem = ji / 10;
int newValue = ji - additionalItem * 10;
sumItemArr[i] = String.valueOf(newValue) + sumItemArr[i];
}
sumItemArr[i] = String.valueOf(additionalItem) + sumItemArr[i];
int tailZeroCount = op2.length() - 1 - i;
sumItemArr[i] = appendZero(sumItemArr[i], tailZeroCount);
}
int length = sumItemArr[0].length();
for (int i = 0; i < sumItemCount; i++) {
sumItemArr[i] = format(sumItemArr[i], length);
}
result = add(sumItemArr);
return result;
}
}