Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一个晚上才ac,发个,帮助后人

Posted by 2006140020 at 2011-04-07 03:04:39 on Problem 1001
我在做的时候用的二维数组存放中间结果,没有考虑到数组相加的时候最前面的进位的时候把大整数长度也要加1,所以wa。


代码有点长:

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner cin = null;
		cin = new Scanner(System.in);
		MyBigInteger b = null;
		while (cin.hasNext()) {
			String d = cin.next();
			if (d.contains(".")) {
				d = trim0(d);
			}
			int n = cin.nextInt();
			if (n == 0) {
				System.out.println("1");
				continue;
			}
			if (d.equals("")) {
				System.out.println("0");
				continue;
			}
			int dotpos = 0;// 小数点的位置
			String[] ss = d.split("\\.");
			String str = "";
			if (ss.length == 2) {
				str = ss[0] + ss[1];
				dotpos = ss[1].length() * n;
			} else if (ss.length == 0) {
				System.out.println("0");
				continue;
			} else if (ss.length == 1) {
				if (trim0(ss[0]).equals("")) {
					System.out.println("0");
					continue;
				}
				str = ss[0];
			}
			int k = Integer.parseInt(str);
			b = new MyBigInteger(k);
			for (int i = 0; i < n - 1; i++) {
				b = b.multi(k);
			}
			// 输出结果
			int i = b.n - 1;
			boolean flag = false;
			if (i < dotpos) {// 如果是纯小数,先输出一个小数点
				System.out.print(".");
				flag = true;
				for (int y = 1; y < dotpos - i; y++) {// 输出小数点之后的零
					System.out.print("0");
				}
			}
			while (i >= 0) {
				if (i == dotpos - 1 && !flag)
					System.out.print(".");
				System.out.print(b.nums[i]);
				i--;
			}
			System.out.println();// 1 781 682 504 867
		}
	}

	// 去掉一个字符串中末尾的0
	private static String trim0(String str) {
		for (int i = str.length() - 1; i >= 0; i--) {
			if (str.charAt(i) == '0') {
				str = str.substring(0, str.length() - 1);
			} else {
				break;
			}
		}
		return str;
	}
}

class MyBigInteger {
	int max = 250;
	int rows = 100;
	int[] nums = new int[max];
	int n;// 大整数的长度
	int[][] temp = new int[rows][max];

	public MyBigInteger() {
	}

	public MyBigInteger(int k) {
		String s = k + "";
		int index = 0;
		for (int i = s.length() - 1; i >= 0; i--) {
			nums[i] = Integer.parseInt(s.charAt(index++) + "");
			n++;
		}
	}

	// 这个大整数乘以k.如果k是多余一位数的数,那么要根据四则运算,每一位都要乘
	public MyBigInteger multi(int k) {
		int maxindex = 0;// 记录temp数组最大的列index,数组中的有效元素的下表都小于等于index
		int i = 0;
		String s = k + "";
		int klen = s.length();
		int index = 0;
		int d = klen - 1;
		for (i = 0; i < klen; i++) {// k的每一位都要乘以大整数
			int c = 0;
			int j = 0;
			int kk = Integer.parseInt(s.charAt(d--) + "");
			for (j = 0; j < n; j++) {// 大整数的每一位
				int t = nums[j] * kk + c;// 乘以并加上进位,得到每一位相乘的结果
				// if(==null)System.out.println("t is null: "+i+" "+j);
				temp[i][j + index] = t % 10;// 本次按位相乘得到的结果
				if (maxindex < j + index) {
					maxindex = j + index;
				}
				c = t / 10;// 得到本次的进位
			}
			if (c != 0) {
				temp[i][j + index] = c;
				if (maxindex < j + index) {
					maxindex = j + index;
				}
			}
			index++;
		}
		// 每一位乘完毕之后,得到数组temp,将temp相加,得到的大整数最终加上小数点就是相乘结果
		MyBigInteger bb = new MyBigInteger();
		index = 0;
		int c = 0;
		for (i = 0; i < max; i++) {// 列
			int t = 0 + c;
			for (int j = 0; j < rows; j++) {// 行
				t += temp[j][i];
			}
			bb.nums[index++] = t % 10;
			c = t / 10;
			if(i==maxindex&&c!=0){
				maxindex++;//就是这里,如果最后一位要进位,就要把大整数的长度加1
			}
		}
		if (c != 0) {
			nums[i] = c;
		}
		bb.n = maxindex + 1;
		return bb;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator