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

帮忙看看什么原因

Posted by torycatkin at 2008-04-01 17:03:36 on Problem 1001
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
	private int pointNums = 0;
	//计算两个数的字符串乘积
	private String getMultResult(String mulf,String muls){
		char[] c1 = mulf.toCharArray();
		char[] c2 = muls.toCharArray();
		int len1 = c1.length;
		int len2 = c2.length;
		int count = 0;
		List retList = new ArrayList();
		for(int i=(len1-1);i>=0;i--){
			int aNum = Integer.valueOf(String.valueOf(c1[i])).intValue();
			Stack ret = new Stack();
			boolean addFlag = false;
			for(int j=(len2-1);j>=0;j--){
				int bNum = Integer.valueOf(String.valueOf(c2[j])).intValue();
				int num = aNum*bNum;
				if(addFlag){
					Integer temp = (Integer)ret.pop();
					if(temp!=null)
						num = num + temp.intValue();
				}
				if(num>=10){
					ret.push(new Integer(num%10));
					ret.push(new Integer(num/10));
					addFlag = true;
				}
				else{
					ret.push(new Integer(num));
					addFlag = false;
				}
			}
			for(int m=0;m<count;m++){
				ret.insertElementAt(new Integer(0),0);
			}
			retList.add(ret);
			count++;
		}
		String resultValue = getAddResult(retList);
		return resultValue;
	}
	
	private String getAddResult(List valueList){
		Iterator itor = valueList.iterator();
		int count = 0;
		Stack result = new Stack();
		while(itor.hasNext()){
			Stack tStack = (Stack)itor.next();
			if(count==0){
				result = tStack;
			}
			else{
				int len1 = result.size();
				int len2 = tStack.size();
				if(len1>len2)
					result = sumAll(result,tStack);
				else 
					result = sumAll(tStack,result);
			}
			count++;
		}
		StringBuffer buf = new StringBuffer();
		while(result.size()>0){
			buf.append(result.pop().toString());
		}
		return buf.toString();
	}
	
	private Stack sumAll(Stack s1,Stack s2){
		Iterator itor = s2.iterator();
		int count = 0;
		int len = s1.size();
		Stack result = new Stack();
		boolean addFlag = false;
		while(itor.hasNext()){
			Integer c2 = (Integer)itor.next();
			Integer c1 = (Integer)s1.get(count);
			int value = c2.intValue()+c1.intValue();
			if(addFlag){
				Integer s = (Integer)result.pop();
				if(s!=null)
					value = value + s.intValue();
			}
			if(value>=10){
				result.push(new Integer(value%10));
				result.push(new Integer(value/10));
				addFlag = true;
			}
			else{
				result.push(new Integer(value));
				addFlag = false;
			}
			count++;
		}
		while(count<len){
			Integer c1 = (Integer)s1.get(count);
			int value = c1.intValue();
			if(addFlag){
				Integer s = (Integer)result.pop();
				if(s!=null)
					value = value + s.intValue();
			}
			if(value>=10){
				result.push(new Integer(value%10));
				result.push(new Integer(value/10));
				addFlag = true;
			}
			else{
				result.push(new Integer(value));
				addFlag = false;
			}
			count++;
		}
		return result;
	}
	
	private String formatStrNum(String str,int pointNums){
		if(pointNums<=0)
			return str;
		StringBuffer strf = new StringBuffer(str);
		int len = strf.length();
		while(len<pointNums){
			strf.insert(0,"0");
			len++;
		}
		int pointPos = len - pointNums;
		String str1 = strf.substring(0,pointPos);
		String str2 = strf.substring(pointPos);
		return str1+"."+str2;
	}
	private String formatInput(String input){
		StringBuffer ret = new StringBuffer(input);
		int pointPos = input.indexOf(".");
		int len = input.length();
		if(pointPos==0){
			ret.insert(0,'0');
			pointPos = 1;
		}
		else if(pointPos==(len-1)){
			ret.deleteCharAt(pointPos);
			pointPos = -1;
		}
		if(pointPos==-1){
			int a = Integer.parseInt(ret.toString());
			this.pointNums = 0;
			return String.valueOf(a);
		}
		else{
			StringBuffer str1 = new StringBuffer(ret.substring(0,pointPos));
			StringBuffer str2 = new StringBuffer(ret.substring(pointPos+1));
			int leng = str2.length();
			for(int i=(leng-1);i>=0;i--){
				char arg = str2.charAt(i);
				if(arg=='0'){
					str2.deleteCharAt(i);
				}
				else
					break;
			}
			this.pointNums = str2.length();
			int a = Integer.parseInt(str1.toString());
			if(a==0 && pointNums>0){
				int b = Integer.parseInt(str2.toString());
				return String.valueOf(b);
			}
			else if(a>0 && pointNums<=0){
				return String.valueOf(a);
			}
			else if(a>0 && pointNums>0){
				return String.valueOf(a)+str2.toString();
			}
			return "0";
		}
	}

	public static void main(String[] arg){	
		
		try {
			Main m = new Main();
			BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));        
			int ii=0;
			while(ii<6){
				String line = stdin.readLine();
				StringTokenizer st = new StringTokenizer(line);
				String a = String.valueOf(st.nextToken());
				int b = Integer.parseInt(st.nextToken());
				if(b==0)
				break;
				a = m.formatInput(a);
				if(a.equals("0")){
					System.out.println(0);
					return;
				}
				String num = a;
				for(int i=1;i<b;i++){
					num = m.getMultResult(a,num);
				}
				num = m.formatStrNum(num,m.pointNums*b);
				System.out.println(num);
				ii++;
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

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