UVa893 - Y3K Problem(用到java中的GregorianCalendar)

本文深入探讨了如何通过编程解决日期预测问题,并特别关注了闰年的计算逻辑。通过实例代码,详细解释了如何在给定天数后预测日期,包括对标准格里高利历的深入理解,特别是关于闰年的复杂规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Y3K Problem 

We have heard a lot recently about the Y2K problem. According to the doom sayers, planes will fall out of theskies, businesses will crash and the world will enter a major depression as the bugs in software and hardwarebite hard. While this panic is a very satisfactory state of affairs for the computing profession, since it isleading to lots of lucrative jobs, it will have a tendency to bring the profession into disrepute when almostno problems occur on 1/1/00. To help avoid this unseemly behaviour on any future occasion, you must write aprogram which will give the correct date for (almost) any number of future days - in particular, it mustcorrectly predict the date N days ahead of any given date, where N is a number less than 1,000,000,000 andthe given date is any date before the year 3000.


Remember that in the standard Gregorian calendar we use there are 365 days in a year, except for leap yearsin which there are 366. Leap years are all years divisible by 4 and not divisible by 100, except for thosedivisible by 400. Thus 1900 was not a leap year, 1904, 1908 ... 1996 were leap years, 2000 will be a leap year,2100 will not be a leap year, etc. The number of days in each month in a normal year is31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; in a leap year, the second month has 29 days.

Input 

Input will consist of linescontaining four numbers, separated by one or more spaces. The first number on each line will be the numberof days you have to predict (between 0 and 999999999), followed by the date in the format DD MM YYYYwhere DD is the day of the month (1 to 31), MM is the month (1 to 12), and YYYY is the year (1998 to 2999),all inclusive. The input will be terminated by a line containing four 0's.

Output 

For each line of input, one output line should be produced.This line should contain the date which is the required number of days ahead of the input date, written in thesame format as the input dates.

Sample Input 

1 31 12 2999
40 1 2 2004
60 31 12 1999
60 31 12 2999
146097 31 12 1999
999999 1 1 2000
0 0 0 0

Sample Output 

1 1 3000
12 3 2004
29 2 2000
1 3 3000
31 12 2399
27 11 4737
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.GregorianCalendar;
import java.util.Calendar;

public class Main 
{
	public static final boolean DEBUG = false;
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public int n, d, m, y;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
			else return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean input() 
	{
		n = Integer.parseInt(next());
		d = Integer.parseInt(next());
		m = Integer.parseInt(next());
		y = Integer.parseInt(next());
		
		if ((n == 0) && (d == 0) && (m == 0) && (y == 0)) return false;
		
		return true;
	}

	
	public void solve() 
	{
		GregorianCalendar cal = new GregorianCalendar(y, m - 1, d);
		cal.add(Calendar.DATE, n);
		
		cout.println(cal.get(Calendar.DATE) + " " + (cal.get(Calendar.MONTH) + 1) + 
				" " + cal.get(Calendar.YEAR));
		cout.flush();
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值