POJ 1001

语言: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) {
            //是小数
            //去掉前导0
            result = result.substring(first_index_noZero);
            //去掉尾部0
            int last_index_noZero = result.length() - 1;
            while (result.charAt(last_index_noZero) == '0') {
                last_index_noZero--;
            }
            if (result.charAt(last_index_noZero) == '.') {
                // 1.0 1
                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];
            // 添加尾部0
            int tailZeroCount = op2.length() - 1 - i;
            sumItemArr[i] = appendZero(sumItemArr[i], tailZeroCount);
//            System.out.println(sumItemArr[i]);
        }//所有累加项均已求出
        // 添加首部0
        int length = sumItemArr[0].length();
        for (int i = 0; i < sumItemCount; i++) {
            sumItemArr[i] = format(sumItemArr[i], length);
        }
        // 求和
        result = add(sumItemArr);
        return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值