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

系统总是说我的程序Runtime Error,哪位大哥能帮小弟看看我的程序啊?我加了注释

Posted by ahy at 2004-11-07 15:20:49 on Problem 1001
import java.util.*;
import java.io.*;
import java.math.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		String s = stdin.readLine();
		while(s!=null) {	//循环读入,当readLine()返回null时停止循环
			StringTokenizer st = new StringTokenizer(s);
			StringBuffer sb_r = new StringBuffer(st.nextToken());	//转换第一个语汇单元,即基数r
			int n = Integer.parseInt(st.nextToken());	//转换第二个语汇单元,即指数n
			System.out.println("n=" + n);//输出读入的指数,本行在提交时删除
			if(sb_r.indexOf(".")!=-1) {	//当基数中存在小数点“.”时,去除小数部分的后置零
				//从最末位往前找,找到第一个非零字符,并把该非零字符后面的零全部删去
				for(int i=sb_r.length()-1; i>=0; i--) {
					if(sb_r.charAt(i)!='0') {
						sb_r.delete(i+1,sb_r.length());
						break;
					}
				}
			}
			BigDecimal r = new BigDecimal(sb_r.toString());	//将基数转换为用于计算的类型
			System.out.println("r=" + r);//输出读入的基数,本行在提交时删除
			BigDecimal res = r;
			if(n==0) {	//如果指数为零,则直接输出1,并再次读入数据,进行下一次循环
				System.out.println(1);
				s = stdin.readLine();
				continue;
			}
			//将基数r相乘n次,求得幂结果
			for(int i=1; i<n; i++) {
				res = res.multiply(r);
			}
			String[] s_res = res.toString().split("E");	//对科学记数法的结果进行拆分,分为尾数和阶数
			if(s_res.length==1) {	//如果没有阶数,则删去前导零,并输出结果,然后再次读入数据并进行下一次循环
				StringBuffer sb_res = new StringBuffer(s_res[0]);
				if(sb_res.charAt(0)=='0') {
					sb_res.delete(0,1);
					s_res[0] = sb_res.toString();
				}
				System.out.println(s_res[0]);
				s = stdin.readLine();
				continue;
			}else {	//有阶数,则按照阶数正负进行左右移动小数点
				int max = Integer.parseInt(s_res[1]);
				StringBuffer sb_res = new StringBuffer(s_res[0]);
				if(max<0) {	//阶数为负,往左移动,添加适当的零
					if(sb_res.length()>1){	//删去小数点,移动位数相应减1
						sb_res.deleteCharAt(1);
					}
					max = Math.abs(max) - 1;
					for(int i=0; i<max; i++) {
						sb_res.insert(0,'0');
					}
					sb_res.insert(0,'.');	//在最前面添加小数点
				}else {	//阶数为正,往右移动,添加适当的零
					for(int i=0; i<max; i++) {
						sb_res.append('0');
					}
				}
				System.out.println(sb_res);
				s = stdin.readLine();
			}
		}
	}
}

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