题解 | #ranko的手表#
ranko的手表
https://www.nowcoder.com/practice/37275e85ae7c4453920eae6b9f7f45fc
总结:
1.本题计算最大最小值,可以使用枚举法把所有符合规则的字符串保存起来,之后再计算最大最小值。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String t1 = sc.nextLine(); String t2 = sc.nextLine(); //用于存储符合规则的字符串 List<Integer> list1 = new ArrayList<>(); List<Integer> list2 = new ArrayList<>(); //24小时存储符合规则的时间 for(int i=0;i<60*24;i++){ int hour = i/60; int minu = i%60; if((t1.charAt(0)=='?'||t1.charAt(0)-'0'==hour/10)&&(t1.charAt(1)=='?'||t1.charAt(1)-'0'==hour%10) &&(t1.charAt(3)=='?'||t1.charAt(3)-'0'==minu/10)&&(t1.charAt(4)=='?'||t1.charAt(4)-'0'==minu%10)) list1.add(i); if((t2.charAt(0)=='?'||t2.charAt(0)-'0'==hour/10)&&(t2.charAt(1)=='?'||t2.charAt(1)-'0'==hour%10) &&(t2.charAt(3)=='?'||t2.charAt(3)-'0'==minu/10)&&(t2.charAt(4)=='?'||t2.charAt(4)-'0'==minu%10)) list2.add(i); } int max = 0; int min = 60*24; for(int i=0;i<list1.size();i++) for(int j=0;j<list2.size();j++){ if(list1.get(i)<list2.get(j)){ max = Math.max(list2.get(j)-list1.get(i),max); min = Math.min(list2.get(j)-list1.get(i),min); } } System.out.print(min+" "+max); } }