解密犯罪时间
标题:解密犯罪时间 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
警察在侦破一个案件时,得到了线人给出的可能犯罪时间,形如 “HH:MM” 表示的时刻。
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); Set<Character> set = new HashSet<>(); // 获得构造用的数字 for (int i = 0; i < 5; i++) { if(i != 2) set.add(input.charAt(i)); } Character[] nums = set.toArray(new Character[0]); int intTime = getIntTime(input); List<String> timeList = createTime(nums); timeList.sort((o1, o2) -> { int i1 = getIntTime(o1); i1 = i1 < intTime?i1 + 1439 - intTime:i1 - intTime; int i2 = getIntTime(o2); i2 = i2 < intTime?i2 + 1439 - intTime:i2 - intTime; return i1 - i2; }); if(timeList.size() > 1){ System.out.println(timeList.get(1)); }else{ System.out.println(timeList.get(0)); } } // 构造时间的数字,时间的数字表示 // 0000- 1439 static List<String> createTime(Character[] nums){ int l = nums.length; List<String> list = new ArrayList<>(); for (int i = 0; i < l; i++) { if(nums[i] - '0' <= 2){ char[] time = new char[5]; time[0] = nums[i]; for (int j = 0; j < l; j++) { if(nums[i] - '0' <= 1 || (nums[i] == '2' && nums[j] - '0' <= 3 )){ time[1] = nums[j]; for (int k = 0; k < l; k++) { if (nums[k] - '0' <= 5){ time[3] = nums[k]; for (int m = 0; m < l; m++) { time[4] = nums[m]; time[2] = ':'; list.add(new String(time)); } } } } } } } return list; } static int getIntTime(String strTime){ String[] temp = strTime.split(":"); return Integer.parseInt(temp[0]) * 60 + Integer.parseInt(temp[1]); } }
import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class Main { // 时间转化字符串为时间时间对象 public static Date toDate(String hour, String minute) { Integer iHour = Integer.parseInt(hour); Integer iMinute = Integer.parseInt(minute); if (iHour >= 24 || iMinute > 60) { return null; } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, iHour); calendar.set(Calendar.MINUTE, iMinute); return calendar.getTime(); } // 判断时间是否满足要求 public static boolean isExists(String key, char c) { return key.indexOf(c) != -1; } // 对输入时间数字排序 public static String getKey(String hour, String minute) { String sKey = hour + minute; char[] ss = sKey.toCharArray(); // 数字进行排序 Arrays.sort(ss); return String.valueOf(ss); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); // 截取时间和分钟部分 String[] input = str.split(":"); String sHour = input[0]; String sMinute = input[1]; String sKey = getKey(sHour, sMinute); Date bTime = toDate(sHour, sMinute); Date eTime = null; // 格式化时间格式 SimpleDateFormat ft = new SimpleDateFormat("HH:mm"); for (int i = 0; i < sKey.length(); ++i) { StringBuilder minute = new StringBuilder(sMinute); minute.setCharAt(1, sKey.charAt(i)); // 时间转换 Date time = toDate(sHour, minute.toString()); if (time != null && time.compareTo(bTime) == 1 && !str.equals(ft.format(time))) { if (eTime == null || time.compareTo(eTime) == -1) { eTime = time; } } } if (null != eTime) { System.out.println(ft.format(eTime)); return; } for (int i = 0; i < sKey.length(); ++i) { if (sKey.charAt(i) > sMinute.charAt(0)) { StringBuilder minute = new StringBuilder(sMinute); minute.setCharAt(0, sKey.charAt(i)); minute.setCharAt(1, sKey.charAt(0)); // 结束时间 eTime = toDate(sHour, minute.toString()); break; } } if (null != eTime) { System.out.println(ft.format(eTime)); return; } for (int i = 0; i < sKey.length(); ++i) { if (sKey.charAt(i) > sHour.charAt(1)) { StringBuilder hour = new StringBuilder(sHour); hour.setCharAt(1, sKey.charAt(i)); // 拼接结束时间 eTime = toDate(hour.toString(), sKey.charAt(0) + "" + sKey.charAt(0)); break; } } if (null != eTime) { System.out.println(ft.format(eTime)); return; } for (int i = 0; i < sKey.length(); ++i) { if (sKey.charAt(i) > sHour.charAt(0)) { StringBuilder hour = new StringBuilder(sHour); hour.setCharAt(0, sKey.charAt(i)); hour.setCharAt(1, sKey.charAt(0)); eTime = toDate(hour.toString(), sKey.charAt(0) + "" + sKey.charAt(0)); break; } } if (null != eTime) { System.out.println(ft.format(eTime)); return; } eTime = toDate(sKey.charAt(0) + "" + sKey.charAt(0), sKey.charAt(0) + "" + sKey.charAt(0)); System.out.println(ft.format(eTime)); } }
import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println( nextTime(str)); } public static String nextTime(String time){ String[] arr = time.split(":"); Set<Character> set = new HashSet(); set.add(arr[0].charAt(0)); set.add(arr[0].charAt(1)); set.add(arr[1].charAt(0)); set.add(arr[1].charAt(1)); int hour = Integer.valueOf(arr[0]); int minute = Integer.valueOf(arr[1]); int min = minute+ hour*60; for(int i = min+1; i< min+24*60; i++){ String newHour = ""+(i%(24*60))/60; if(newHour.length() == 1){ newHour = "0" + newHour; } String newMinute = ""+(i%(24*60))%60; if(newMinute.length() == 1){ newMinute = "0" + newMinute; } if(set.contains(newMinute.charAt(0)) && set.contains(newMinute.charAt(1)) && set.contains(newHour.charAt(0)) && set.contains(newHour.charAt(1))){ return newHour+ ":" +newMinute; } } return null; } }