首页 > 代码库 > POJ1008 Maya Calendar

POJ1008 Maya Calendar

对比才能看出差距!先是我写的一段代码,然后是参考高手的一段代码,对比分析。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int lineNumber = scanner.nextInt();
		System.out.println(lineNumber);
		String[] Haab = new String[]{"pop", "no", "zip", "zotz", "tzec", 
				"xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", 
				"kankin", "muan", "pax", "koyab", "cumhu", "uayet"};
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		String[] inputs = new String[lineNumber];
		for(int i=0; i<lineNumber; i++){
			inputs[i] = scanner.nextLine();
		}
		
		for(int i=0; i<lineNumber; i++){
			String input = inputs[i];
			String[] strDate = input.split(" ");
			
			String strDay = strDate[0].substring(0, strDate[0].length() - 1);
			int day = Integer.parseInt(strDay);
			for(int j=0; j<Haab.length; j++){
				map.put(Haab[j], j);
			}
			int month = map.get(strDate[1]);
			int year = Integer.parseInt(strDate[2]);
			int totalDays = year*365 + month*20 + day;
			
			int midDayNumber = totalDays % 13 + 1;
			int midYear = totalDays / 260;
			String midDay = null;
			switch(totalDays % 20){
			case 0: midDay = "imix"; break; 
			case 1: midDay = "ik"; break; 
			case 2: midDay = "akbal"; break;
			case 3: midDay = "kan"; break;
			case 4: midDay = "chicchan"; break;
			case 5: midDay = "cimi"; break;
			case 6: midDay = "manik"; break;
			case 7: midDay = "lamat"; break;
			case 8: midDay = "muluk"; break;
			case 9: midDay = "ok"; break;
			case 10: midDay = "chuen"; break;
			case 11: midDay = "eb"; break;
			case 12: midDay = "ben"; break;
			case 13: midDay = "ix"; break;
			case 14: midDay = "mem"; break;
			case 15: midDay = "cib"; break;
			case 16: midDay = "caban"; break;
			case 17: midDay = "eznab"; break;
			case 18: midDay = "canac"; break;
			case 19: midDay = "ahau"; break;
			}
			System.out.println(midDayNumber + " " + midDay + " " + midYear);
		}
	}
}
下面是一个高手的代码,简洁高效,参考对比:

import java.util.ArrayList;  
import java.util.Scanner;  
  
class Main {  
  
    public static void main(String[] args) {  
        String[] Haab = new String[] { "pop", "no", "zip", "zotz", "tzec",  
                "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac",  
                "kankin", "muan", "pax", "koyab", "cumhu", "uayet" };  
        ArrayList<String> HaabList = new ArrayList<String>();  
        for (int i = 0; i < Haab.length; i++) {  
            HaabList.add(Haab[i]);  
        }  
        String[] Tzolkin = new String[] { "imix", "ik", "akbal", "kan",  
                "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen",  
                "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac",  
                "ahau" };  
        Scanner in = new Scanner(System.in);  
        int ncase = in.nextInt();  
        System.out.println(ncase);  
        while (ncase-- != 0) {  
            String strHDay = in.next();  
            int hDay = Integer.parseInt(strHDay.substring(0,  
                    strHDay.length() - 1));  
            int hDays = HaabList.indexOf(in.next()) * 20 + hDay + 365  
                    * in.nextInt();  
            int tYear = hDays / 260;  
            String tMonth = Tzolkin[hDays % 20];  
            int tDay = hDays % 13 + 1;  
            System.out.println(tDay + " " + tMonth + " " + tYear);  
        }  
    }  
}  
1、我的代码在String strDay = strDate[0].substring(0, strDate[0].length() - 1);这行会报错java.lang.StringIndexOutOfBoundsException: String index out of range: -1。跟踪了很久发现是第一个元素总是为空。原来nextInt()方法仅仅读取了一个int型的token,但并不换行。紧接着使用nextLine()方法的话会读取换行前的其他内容,然后换行等待下一个输入,所以nextLine()读到的第一个是null。连续输入部分改成下面这样也可以,开始就使用nextLine()。

Scanner scanner = new Scanner(System.in);
int lineNumber = Integer.parseInt(scanner.nextLine());
String[] inputs = new String[lineNumber];
for(int i=0; i<inputs.length; i++){
	inputs[i] = scanner.nextLine();
}
也可以像参考代码那样,每次用next()接收一个输入,而不是接收一行输入(这样就不需要像我那样用一个inputs[]数组来保存所有输入了,代码可以再简洁一些)。

2、我的代码在最后打印天名称的时候使用的是switch...case,虽然可以正常打印,但我总感觉手写麻烦而且不简洁。对比参考代码,先放到数组里,然后根据数组下标去找就巧妙很多。

POJ1008 Maya Calendar