2014编程之美初赛A题焦距

字符串分割:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) throws IOException {
		
	
		DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
		String s;
		double inputs[] = new double[3];
		String  units[]  = new String[3];
		s = in.readLine();
		int T = Integer.valueOf(s);		
		for (int n = 1; n <= T; n++) {
			   s = in.readLine();
			   String[] numopt= s.split(" ");
			   Pattern p1 = Pattern.compile("([0-9]*[.]*[0-9]*)([a-z]{1,2})");
			   for(int i=0;i<numopt.length;i++) {
			   Matcher m1 = p1.matcher(numopt[i]);
				   if(m1.find()) {
					   System.out.print(m1.group(1)+" ");
					   System.out.println(m1.group(2));
					   inputs[i]= Double.parseDouble(m1.group(1));
					   units[i] = m1.group(2);
				   }
			   }
			   inputs[0]=switchunit(units[0],inputs[0]);
			   inputs[1]=switchunit(units[1],inputs[1]);
			   double result = inputs[2]*inputs[0]/inputs[1];
			   BigDecimal bg = new BigDecimal(result);
			   double j = bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
			   System.out.println("Case "+n+": "+j+"px");
			}
	
		
	
	}

	private static double switchunit(String unit1, double a) {
		// TODO Auto-generated method stub
		switch(unit1) {
		    case "mm":return a;
		    case "m":return a*1000;
		    case "dm":return a*100;
		    case "cm":return a*10;
		    case "um":return a/1000;
		    case "num":return a/1000000;
		    
		}
		return a;
	}

}
    原文作者:江湖小虾米东方未明
    原文地址: https://blog.csdn.net/xiezhongweiwei/article/details/24135935
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞