package demo01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class t03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 特殊时间
//年份四位范围 1000 9999 月日四位范围0101 1231 mm:ss
//int month =0;
int index = 0;
int year = 1000;
int month = 1;
int day = 1;
int the = 0;//分钟
int ss = 0;//秒
//1 3 5 7 8 10 12 31天
//4 6 9 11 30天
//2 29天或28天
for (year = 1000; year <= 9999; year++) {
if (!fourpan(year + "")) {
//判断年份是否符合 若不符合进入下次循环
continue;
}
for (month = 1; month <= 12; month++) {
String newMonth = "";
if (month < 10) {
newMonth = "0" + month;
} else {
newMonth = month + "";
}
//根据年份月份获取最大天数
int maxDay = getMaxDay(year, month);
for (day = 1; day <= maxDay; day++) {
String newday = "";
if (day < 10) {
newday = "0" + day;
} else {
newday = day + "";
}
//判断月日符不符合
if (!fourpan(newMonth + newday)) {
continue;
}
if (!two(year+"",newMonth + newday)){
continue;
}
for (the = 0; the <= 59; the++) {
String newThe = "";
if (the < 10) {
newThe = "0" + the;
} else {
newThe = the + "";
}
for (ss = 0; ss <= 59; ss++) {
String newdateTime = "";
if (ss < 10) {
newdateTime = "0" + ss;
} else {
newdateTime = ss + "";
}
if (!fourpan(newThe + newdateTime)) {
continue;
}
//如果是四个相同的则判断跟月份是不是相同的两位数字
if (two(newMonth + newday, newThe + newdateTime)) {
System.out.println("年:"+year+"月日:"+newMonth+newday+"分秒:"+newThe+newdateTime);
index++;
}
}
}
}
}
}
System.out.println(index);
}
//通过传年份和月份判断最大日期
public static int getMaxDay(int year, int month) {
//List<Integer> list = new ArrayList<>();
int[] thirtyOne = {1, 3, 5, 7, 8, 10, 12};//三十一天
int[] thirty = {4, 6, 9, 11};
for (int i = 0; i < thirty.length; i++) {
if (thirtyOne[i] == month) {
return 31;
}
}
for (int i = 0; i < thirty.length; i++) {
if (thirty[i] == month) {
return 30;
}
}
//闰年
if (year % 400 == 0 || (year % 4 == 0 && year %100 !=0)) {
return 29;
} else {
//平年
return 28;
}
}
//判断获取的四位数字是否是三个相同数字和一个不相同数字组成的
public static boolean fourpan(String sub) {
//System.out.println(sub);
List<Character> list = new ArrayList<>();
for (int j = 0; j < sub.length(); j++) {
list.add(sub.charAt(j));
}
Collections.sort(list);
boolean qian = list.get(0).equals(list.get(1)) && list.get(1).equals(list.get(2));
boolean hou = list.get(1).equals(list.get(2)) && list.get(2).equals(list.get(3));
if (qian || hou){
return true;
}
return false;
}
//判断两个四位数是否是两种相同数字组成
public static boolean two(String firtt, String twist) {
List<Character> list1 = new ArrayList<>();
List<Character> list2 = new ArrayList<>();
for (int i = 0; i < firtt.length(); i++) {
list1.add(firtt.charAt(i));
list2.add(twist.charAt(i));
}
Collections.sort(list1);
Collections.sort(list2);
String strlist1 = "";
String strlist2 = "";
for (int i = 0; i < firtt.length(); i++) {
strlist1 += list1.get(i);
strlist2 += list2.get(i);
}
return strlist1.equals(strlist2);
}
}
【问题描述】
2022 年 2 月 22 日 22:20 是一个很有意义的时间,年份为 2022,由 3 个 2 和 1 个 0 组
成,如果将月和日写成 4 位,为 0222,也是由 3 个 2 和 1 个 0 组 成,如果将时间中的时和
分写成 4 位,还是由 3 个 2 和 1 个 0 组成。
小蓝对这样的时间很感兴趣,他还找到了其它类似的例子,比如 111 年 10 月 11 日
01:11,2202 年 2 月 22 日 22:02 等等。
请问,总共有多少个时间是这种年份写成 4 位、月日写成 4 位、时间写成 4 位后由 3
个一种数字和 1 个另一种数字组成。注意 1111 年 11 月 11 日 11:11 不算,因为它里面没有
两种数字。
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
正确答案是212 这个运行出来是205,搞半天不知道那错了