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,虽然可以正常打印,但我总感觉手写麻烦而且不简洁。对比参考代码,先放到数组里,然后根据数组下标去找就巧妙很多。

时间: 2024-10-10 19:19:48

POJ1008 Maya Calendar的相关文章

POJ-1008 Maya Calendar

[题目描述] 将Haab历法的日期转化为Tzolkin历法,前者一年365天,后者一年260天.(简直比我们的历法转换还简单,都没闰年!) [思路分析] 简单的思路就是将Haab历法的日期转换成从第一天开始的总天数,再转换成Tzolkin历法.由于最大年份为5000年,乘上365天也不会使int型越界,所以放心转吧. 做题过程中主要复习的知识点是: 1. STL中map的用法: map<string, int> HaabMonth; // 定义 HaabMonth.insert( pair&l

[POJ] POJ1008 Maya Calendar

该篇作为POJ之路的起点,初学乍道,请多指教. Problem: Maya Calendar Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, professor discovered that the Maya civilization used a 365

算法:POJ1008 Maya Calendar

此题非常水,不做说明. package practice; import java.io.BufferedInputStream; import java.util.Scanner; /** * @author caiyu * @date 2014-12-2 */ public class POJ1008 { static final String[] HaabMonth = new String[] { "pop", "no", "zip",

POJ-1008: Maya Calendar 详解1: 直接计算法

> 分析 >> 本题先计算出总天数,再进行转换即可 > 注意 >> 字符串不要抄错 >> 输出时也要先输出个数 >> 函数"strnlen"会导致GCC编译不过 > 附代码 1 #include "stdio.h" 2 #include "stdlib.h" 3 4 const char haabMonStr[19][8] = 5 { "pop", "

poj 1008:Maya Calendar(模拟题,玛雅日历转换)

Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, profes

[POJ] #1008# Maya Calendar : 字符处理/同余问题

一. 题目 Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74085   Accepted: 22819 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message,

B - Maya Calendar(第二季水)

Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, professor discovered that the Maya civilization used a 365 day long year, called Haab, which had 19 month

Poj Maya Calendar

http://poj.org/problem?id=1008 Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64607 Accepted: 19908 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an

Poj OpenJudge 百练 Bailian 1008 Maya Calendar

1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66971   Accepted: 20644 Description During his last sabbatical, professor M. A. Ya