An Awful Problem

An Awful Problem

问题描述:

In order to encourage Hiqivenfin to study math, his mother gave him a sweet candy when the day of the month was a prime number. Hiqivenfin was happy with that. But several days later, his mother modified the rule so that he could get a candy only when the day of the month was a prime number and the month was also a prime number. He felt a bit upset because he could get fewer candies. What's worse, his mother changed the rule again and he had to answer a question before he could get a candy in those days. The question was that how many candies he could get in the given time interval. Hiqivenfin wanted to cry and asked you for help. He promised to give you half of a candy if you could help him to solve this problem.

Input

There are multiple test cases. The first line of input is an integer T (0 < T <= 50), indicating the number of test cases. Then T test cases follow. The i-th line of the next T lines contains two dates, the day interval of the question. The format of the date is yyyy mm dd. You can assume both dates are valid. Hiqivenfin was born at 1000-01-01 and would not die after 2999-12-31, so the queries are all in this interval.

Hiqivenfin didn't seem to be an earthman, but the calendar was the same as that we usually use. That is to say, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.

Output

Output the number of candies Hiqivenfin could get in the time interval. Both sides of the interval are inclusive.

Sample Input

2
1000 01 01 1000 01 31
2000 02 01 2000 03 01

Sample Output

0
10

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Candy {

    public static int candies[];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        candies = new int[n];
        Date[][] dates = new Date[n][2];
        String str;
        for (int i = 0; i < dates.length; i++) {
            str = scanner.nextLine();
            dates[i][0] = transformDate(str.substring(0, str.length()/2));
            dates[i][1] = transformDate(str.substring(str.length()/2+1, str.length()));
        }
        for (int i = 0; i < dates.length; i++) {
            updateDate(dates[i][0], dates[i][1], i);
        }
        scanner.close();
        for (int i = 0; i < candies.length; i++) {
            System.out.println(candies[i]);
        }
    }

    /**
     * 把字符串转换成日期类型
     * @param dateStr 日期字符串串
     * @return 日期
     * 
     * 这里要注意一下,打印出日期的年份是从1990开始的,也就是说1000 01 01 是-900年1月1日。但是并不影响后面的判断。
     * 用java.util.Date类型通过增加一天的毫秒数获得的新日期会自动根据是否为闰年而进行日期的设置,这里可以避免人为的判断
     */
    public static Date transformDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 增加1天的毫秒数,当date1小于date2时进行下一步。
     * @param date1 起始日期
     * @param date2 结束日期
     * @param i 日期的行数(1-n)
     */
    public static void updateDate(Date date1, Date date2, int i) {
        while(date1.getTime()<=date2.getTime()) {
            System.out.println(date1);
            count(date1, i);
            date1.setTime(date1.getTime()+1000*60*60*24);
        }

    }

    /**
     * 通过日期获得月份和号数,月份要记得加1,满足都是素数,当前行数的糖果数加1
     * @param date 日期
     * @param i  日期的行数(1-n)
     */
    public static void count(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
//    System.out.println(month+"\t"+day);
        if (isPrimeNumber(month) && isPrimeNumber(day)) {
            candies[i]++;
        }
    }

    /**
     * 判断一个数是否为素数
     * @param number (1-31)
     * @return 返回一个Boolean值
     */
    public static boolean isPrimeNumber(int number) {
        if (number == 1) {
            return false;
        }
        for (int i = 2; i <= Math.pow(number, 0.5); i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
}