24h購物| | PChome| 登入
2013-01-03 15:57:40| 人氣1,389| 回應0 | 上一篇 | 下一篇

[UVa][日期計算][JAVA] 11356 - Dates

推薦 0 收藏 0 轉貼0 訂閱站台

D

NEXT generation Contest - 4

Time Limit – 2 secs

Dates

 

30 days has September,
April, June and November
All the rest have 31
And February’s great with 28
And Leap Year’s February’s fine with 29

The Gregorian calendar, the current standard calendar in most part of the world, adds a 29th day to February in all years evenly divisible by 4, except for centennial years (those ending in -00) which are not evenly divisible by 400. Thus 1600, 2000 and 2400 are leap years but 1700, 1800, 1900, 2100, 2200 and 2300 are not.

In this problem, we are concerned with dates. You will be given a date and an integer K. You have to find the date in the calendar after K days from the given date.

Input

The first line of input is an integer T(T<50) that represents the number of test cases. Each case contains two lines. The first line is a date in the format yyyy-month-dd. year is an integer in the range [1900, 3000], month is a string from the set {January, February, March, April, May, June, July, August, September, October, November and December} and dd is an integer in the range [01,31]. The second line contains an integer K(0<K<10000).

The input date will be a valid one.

Output

For each input, output the case number followed by the date after K days in the same format as that of input. Look at the sample for exact format.

Sample Input

Output for Sample Input

2

1984-December-30

2

1984-October-12

318

Case 1: 1985-January-01

Case 2: 1985-August-26

 

ProblemSetter: Sohel Hafiz



import java.util.Scanner;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    static String[] month = { "January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November",
            "December" };

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        int cases = 0;
        String a, b, c, s;
        while (t-- != 0) {
            s = cin.next();
            int pos1 = s.indexOf('-', 0);
            int pos2 = s.indexOf('-', pos1 + 1);
            a = s.substring(0, pos1);
            b = s.substring(pos1 + 1, pos2);
            c = s.substring(pos2 + 1);
            int dd = cin.nextInt(), monthnum = 0;
            for (int i = 0; i < 12; i++)
                if (month[i].equals(b))
                    monthnum = i;
            GregorianCalendar date = new GregorianCalendar(Integer.parseInt(a),
                    monthnum, Integer.parseInt(c));
            date.add(Calendar.DATE, dd);
            System.out.printf("Case %d: %d-%s-%02d\n", ++cases,
                    date.get(Calendar.YEAR), month[date.get(Calendar.MONTH)],
                    date.get(Calendar.DATE));

        }
    }
}

台長: Morris
人氣(1,389) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 782 - Contour Painting
此分類上一篇:[UVA][greedy+并查集] 1316 - Supermarket

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文