Detailed explanation of the classic date problem of enumeration algorithm java

content

enumeration algorithm

date problem

enumerate ideas

specific code


 

enumeration algorithm

The enumeration algorithm is one of the algorithms we use the most in our daily life, and its core idea is to enumerate all possibilities . The essence of the enumeration method is to search for the correct solution from all the candidate answers.

The use of this algorithm needs to meet two conditions: (1) the number of candidate answers can be predetermined ; (2) the range of candidate answers must have a certain set before solving.

The enumeration algorithm is simple and rude. He violently enumerates all possibilities, trying all the methods as much as possible. While the enumeration algorithm is brute force and can be slow, it's really our top priority! Because the enumeration method becomes the easiest to implement, and the result is always correct.

date problem

Topic link http://oj.hzjingma.com/p/7164?view=classic

Topic description

Xiao Ming is sorting out a batch of historical documents. Many dates appear in these historical documents. Xiao Ming knows that these dates are from January 1, 1960 to December 31, 2059. What troubles Xiao Ming is that the format of these dates is very inconsistent, some use year/month/day, some use month/day/year, and some use day/month/year. What's more troublesome is that the first two digits of the year are also omitted, so that there are many possible dates corresponding to a date in the literature.   
For example, 02/03/04, it may be March 04, 2002, February 3, 2004, or March 02, 2004.   
Given a date in the literature, can you help Xiao Ming determine what possible dates correspond to it?

enter

A date in the format "AA/BB/CC". (0 <= A, B, C <= 9)   

output

Output several different dates, one line for each date, in the format "yyyy-MM-dd". Multiple dates are arranged from morning to night.

Sample

enter
02/03/04
output

2002-03-04

2004-02-03

2003-03-02

enumerate ideas

The general idea of ​​the date problem is to judge whether the date is legal or not based on our attempts to enumerate, such as the month 1-12, the date 1-31 or the judgment of leap year and so on. The key is to enumerate all the dates to judge, find the correct answer, and avoid repeated enumeration or missing items.

specific code

package Test;

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class _日期问题 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String data = scanner.next();
		String[] str = data.split("/");//以字符 ' / ' 分割成字符串数组
		TreeSet<String> ans = new TreeSet<String>();//存放正确答案的集合,利用set集合去重
		// 01/02/03
		//枚举三种情况日/月/年 ,年/月/日,月/日/年
		String case1 = f(str[0], str[1], str[2]);
		String case2 = f(str[2], str[0], str[1]);
		String case3 = f(str[2], str[1], str[0]);
		//如果case字符串合法则加入结果集
		if (case1.length() > 0)
			ans.add(case1);
		if (case2.length() > 0)
			ans.add(case2);
		if (case3.length() > 0)
			ans.add(case3);
		//遍历输出结果
		Iterator<String> iterator = ans.iterator();
		for (String anser : ans) {
			System.out.println(anser);
		}
	}
	/**
	 * 判断是否合法,
	 * @param year
	 * @param month
	 * @param day
	 * @return 空字符串"" 表示不合法
	 */
	private static String f(String year, String month, String day) {
		int _year = Integer.parseInt(year);
		int _month = Integer.parseInt(month);
		int _day = Integer.parseInt(day);
		
		if (_year <= 59)//0-59表示2000年以后,要加上2000
			_year += 2000;
		else			//60-99表示1960 - 1999年,加上1900
			_year += 1900;
		
		if (_month > 12 || _month < 1)//判断月份是否合法  <1或者>12均不合法
			return "";
		
		if (_day > 31 || _day < 1)//判断日期是否合法<1或者>31均不合法
			return "";
		/**
		 * 接下来判断每个月份对应的日期是否合法(前面已经保证月份1-31)
		 * 1,3,5,7,8,10,12每个月固定31天,一定合法
		 * 因此要判断其他月份的时候是否合法
		 */
		if(_month == 2) {
			//闰年>29不合法
			if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0) {
				if (_day > 29)
					return "";
			}
		}else {
			//其他月份>30不合法
			if (_day > 30)
				return "";
		}
		//月份和日期不足10 要补0
		if (_month < 10) {
			month = "0" + _month;
		}
		if (_day < 10) {
			day = "0" + _day;
		}
		return _year + "-" + month + "-" + day;
	}
}

 

 

 

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123755641