首页 > 试题广场 >

福尔摩斯的约会 (20)

[编程题]福尔摩斯的约会 (20)
  • 热度指数:66273 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母'D',代表星期四;第2对相同的字符是'E',那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字母A到N表示);后面两字符串第1对相同的英文字母's'出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。
(注:两字符串的比较过程必须按照两字符串中对应的下标相同的字符进行比较!)

输入描述:
输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。


输出描述:
在一行中输出约会的时间,格式为“DAY HH:MM”,其中“DAY”是某星期的3字符缩写,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期
四,FRI表示星期五,SAT表示星期六,SUN表示星期日。题目输入保证每个测试存在唯一解。
示例1

输入

3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm

输出

THU 14:04
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        String DAY[]={"MON ","TUE ","WED ","THU ","FRI ","SAT ","SUN "};
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        String c = sc.next();
        String d = sc.next();
        char m[]=xiangtongday(a,b);

        //星期几
        System.out.print(DAY[m[0]-'A']);
       //小时
       if(m[1]<'9'&&m[1]>='0')
           System.out.print("0"+m[1]+":");
       else
           System.out.print((m[1]-'A')+10+":");

       //分钟
       if(count(c,d)<10&&count(c,d)>=0)
           System.out.print("0"+count(c,d));
       else
           System.out.print(count(c,d));

    }
    //返回 c[0]==星期几  c[1]=日期
    public static char[] xiangtongday(String a,String b){

         char x[];
         char y[];
         x=a.toCharArray();
         y=b.toCharArray();
         int length;
         int count1=0;
         int flag=0;
         char c[] = new char [20];
         if(a.length()<b.length())
             length=a.length();
         else
             length=b.length();
       for(int i=0;i<length;i++)
       {
           if(x[i]==y[i]&&x[i]>='A'&&x[i]<='G')
           {
              c[count1]=x[i];
              count1++;
              flag=i;
              break;
           }
       }
        for(int i=flag+1;i<=length;i++)
        {
            if(x[i]==y[i]&&((x[i]>='A'&&x[i]<='G')||x[i]>='0'&&x[i]<='9'))
            {
                c[count1]=x[i];
                break;
            }

        }

        return c;
        }

      //返回分钟
   public static int  count(String a,String b) {
       char x[] = new char[60];
       char y[] = new char[60];
       int count1=0;
       int length=0;
       x = a.toCharArray();
       y = b.toCharArray();
       if (a.length() < b.length())
           length = a.length();
       else
           length = b.length();
       for (int i = 0; i < length; i++) {
           if (x[i] == y[i] && x[i] >= 'a' && x[i] <= 'z') {
               count1 = i;
               break;
           }
       }
       return count1;
   }
}
是我不够细还是这个题目太细了啊,一个=号没加愣是看了半个小时才看出来😅

发表于 2021-05-14 19:20:52 回复(0)
非常多的易错点。
package io.github.haoranhsu;
import java.util.Scanner;
/**
 * 福尔摩斯的约会
 * 题目描述
 * 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb
 * s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,
 * 因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母'D',代表星期四;第2对相同的
 * 字符是'E',那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字
 * 母A到N表示);后面两字符串第1对相同的英文字母's'出现在第4个位置(从0开始计数)上,代表第4分钟。
 * 现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。
 * 输入描述:
 * 输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。
 * 输出描述:
 * 在一行中输出约会的时间,格式为“DAY HH:MM”,其中“DAY”是某星期的3字符缩写,即MON表示星期一,
 * TUE表示星期二,WED表示星期三,THU表示星期四,FRI表示星期五,SAT表示星期六,SUN表示星期日。
 * 题目输入保证每个测试存在唯一解。
 * 输入例子:
 * 3485djDkxh4hhGE
 * 2984akDfkkkkggEdsb
 * s&hgsfdk
 * d&Hyscvnm
 * 输出例子:
 * @author Hsu
 * @date 2020/11/3
 * 其实还是用toCharArray比较好!!
 */

public class Pat1004 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String stringA = scanner.nextLine();
        String stringB = scanner.nextLine();
        String stringC = scanner.nextLine();
        String stringD = scanner.nextLine();
        StringBuilder builder = new StringBuilder();
        int flag = 0;
        for(int i = 0; i < stringA.length() && i < stringB.length(); i++){
                if(stringA.charAt(i) == stringB.charAt(i) && Character.isUpperCase(stringA.charAt(i))){
                    if(flag == 0) {
                        builder.append(getDate(stringA.charAt(i) - 'A'));
                        flag++;
                        continue;
                    }

                }
                //这里,后一个判断条件必须放在最后,因为parseInt对于字母不可用!!
            if(stringA.charAt(i) == stringB.charAt(i) && (((stringA.charAt(i) >= 'A' && stringA.charAt(i) <= 'N') || (Integer.parseInt(String.valueOf(stringA.charAt(i))) >= 0 && Integer.parseInt(String.valueOf(stringA.charAt(i))) <= 9)))){
                if(flag == 1){
                    builder.append(" ");
                    //这里不能传入stringA.charAt(i),因为这个是char!要输入的是int
                    //或者-'0'
                    builder.append(getTime(stringA.charAt(i)));
                    builder.append(":");
                    break;
                }

            }
            }


        for(int i = 0; i < stringC.length(); i++) {

                if(stringC.charAt(i) ==  stringD.charAt(i) && Character.isLetter(stringC.charAt(i))){
                    String minute;
                    minute = i < 10 ? "0"+ i : String.valueOf(i);
                    builder.append(minute);
                    break;
            }

        }
        System.out.println(builder.toString());
    }
    private static String getDate(int date){
        switch (date){
            case 0:
                return "MON";
            case 1:
                return "TUE";
            case 2:
                return "WED";
            case 3:
                return "THU";
            case 4:
                return "FRI";
            case 5:
                return "SAT";
            case 6:
                return "SUN";
            default:
                return null;
        }
    }
    private static String getTime(int num){
        //又一个易错点! 要加上 (char),0要加上 ‘0’
        if(Character.isDigit(num) && (char)num >= '0' && (char)num <= '9'){
            String ans = "0".concat(String.valueOf((char)num));
            return ans;
        }
        else {
            return String.valueOf(num - 'A' + 10);
        }
    }
}


发表于 2020-11-03 21:59:57 回复(0)
import java.util.Scanner;
public class Main{
    public static void main (String[] agrs){
        Scanner sc=new Scanner(System.in);
        String a=sc.next();
        String b=sc.next();
            String c=sc.next();
        String d=sc.next();
        int t;
        int x;
       
        if(a.length()>b.length()){
            t=b.length();    
        }
        else{
            t=a.length();
            
        }
          if(c.length()>d.length()){
            x=d.length();    
        }
        else{
            x=c.length();   
        }
    String w=   time(a,t,b);
    int l=hold(c,x,d);
    System.out.println(day(w.charAt(0))+" "+date(w.charAt(1))+mmm(l));
    System.out.println(w);
    }
    
  public static String day(char a)  {
      switch(a-64){
          case 1:  return "MON";    
            case 2:  return "TUE";    
              case 3:  return "WED";  
                case 4:  return "THU";              
             case 5:  return "FRI";  
              case 6:  return "SAT";  
              default :return "SUN";
      }
      
  }
    
   public static String date(char bb)  {
      
      if(bb>='0'&&bb<='9')
          return 0+""+bb+":";
       else{
         switch(bb-64) {
             
        case 1: return 10+":";   
             case 2:return 11+":";      
             case 3:return 12+":";  
                 case 4:return 13+":"; 
                 case 5:return 14+":";  
                 case 6:return 15+":";  
                 case 7:return 16+":";  
                 case 8:return 17+":";  
                 case 9:return 18+":";  
                 case 10:return 19+":";  
                 case 11:return 20+":";  
                 case 12:return 21+":";
                 case 13:return 22+":";  
                 default:return 23+":"; 
         } 
           
           
       }
      
  }  
   public static String mmm(int c)  {
      if(c<10)
          return 0+""+c;
       else
           return c+"";
      
  }   
   public static int hold(String c,int x,String d)  {
          int u,i,j;
        
           for( j=0;j<x;j++){
           if((c.charAt(j)+"").equals(d.charAt(j)+"")&&(c.charAt(j))>'a'&&(c.charAt(j))<'z')
           {
               return j;
               
                  
           } 
           
           
           
       }
       
        return 0;
        
        
      }   
   public static String time(String a,int t,String b)  {
          int g=0;
          String s="";
          char o;
          char p;
       
           for(int j=0;j<t;j++){
           if((a.charAt(j)+"").equals(b.charAt(j)+""))
           {
               if(g==2)
               {s=s+a.charAt(j)+"";return s;}
              if((a.charAt(j))>='A'&&(a.charAt(j))<='G')
              { s=s+a.charAt(j)+"";g=1;++g;}
                 
               
           }
           
           } 
       
       return null;  
      }   
}
发表于 2019-05-28 00:23:18 回复(1)
import java.util.Arrays;
import java.util.Scanner;
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        String s1=sc.nextLine();
        String s2=sc.nextLine();
        String s3=sc.nextLine();
        String s4=sc.nextLine();
        //s1和s2中遇到的第一个相同的大写字母是第几个
        char same[]=new char[2];
        int count=0;
        int m1=Math.min(s1.length(), s2.length());
        for(int i=0;i<m1;i++)
        {
                if(s1.charAt(i)==s2.charAt(i))
                {    
                    if(s1.charAt(i)>='A'&&s1.charAt(i)<='G'&&count==0)
                    {
                        same[0]=s1.charAt(i);
                        count++;
                    }
                    else if(count==1)
                        {same[1]=s1.charAt(i);
                        break;}
                }
        }
        weeks(same[0]);
        shi(same[1]);
        int m2=Math.min(s3.length(),s4.length());
        int k=0;
        for(int i=0;i<m2;i++)
        {
                if(s3.charAt(i)==s4.charAt(i)&&yingwen(s3.charAt(i)))
                {
                    k=i;
                    System.out.println(String.format("%02d", k));
                }
                if(k!=0)
                    break;
        }
    }
    static void weeks(char x)
    {
        switch(x)
        {
        case 'A':System.out.print("MON");break;
        case 'B':System.out.print("TUE");break;
        case 'C':System.out.print("WED");break;
        case 'D':System.out.print("THU");break;
        case 'E':System.out.print("FRI");break;
        case 'F':System.out.print("SAT");break;
        case 'G':System.out.print("SUN");break;
        }
    }
    public static boolean upchar(char c)
    {
        return c>=65&&c<=90;
    }
    public static void shi(char c)
    {
        char xiaoshi[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N'};
        int s=Arrays.binarySearch(xiaoshi, c);
        System.out.print(" "+String.format("%02d", s)+":");
    }
    public static boolean yingwen(char c)
    {
        return (c>=65&&c<=90)||(c>=97&&c<=122);
    }
}

发表于 2019-05-24 19:03:49 回复(0)
import java.util.Scanner;

public clas***ain {
    //    3485djDkxh4hhGE
//    2984akDfkkkkggEd***
//    s&hgsfdk
//    d&Hyscvnm
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StringBuffer buffer1 = new StringBuffer(scanner.next());
        StringBuffer buffer2 = new StringBuffer(scanner.next());
        StringBuffer buffer3 = new StringBuffer(scanner.next());
        StringBuffer buffer4 = new StringBuffer(scanner.next());

        String first = null;
        String second = null;

        int i = 0;
        while (true) {
            i++;
            if (buffer1.charAt(i) == buffer2.charAt(i) && (buffer2.charAt(i) >=
                    'A' && buffer2.charAt(i) <= 'Z')) {
                first = String.valueOf(buffer1.charAt(i));
                break;
            }
        }
        while (true) {
            i++;
            if (buffer1.charAt(i) == buffer2.charAt(i)) {
                second = String.valueOf(buffer1.charAt(i));
                break;
            }
        }
//        System.out.println(first+" "+second);
        String array[] = {"A", "B", "C", "D", "E", "F", "G"};
        String arrayWeek[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
        String week = null;
        for (int j = 0; j < array.length; j++) {
            if (array[j].equals(first)) {
                week = arrayWeek[j];
            }
        }

        int hour = 0;
        if (second.toCharArray()[0] >= '0' && second.toCharArray()[0] <= '9') {
            hour = new Integer(second).intValue();
        } else {
            String s = second.toLowerCase();
            hour = 10 + s.charAt(0) - "a".charAt(0);
        }

//        System.out.println(week+" "+hour);
        String s3 = buffer3.toString();
        String s4 = buffer4.toString();

        int minute = 0;
        for (int j = 0; j < s3.length(); j++) {
            if (s3.toCharArray()[j] == s4.toCharArray()[j]) {
                if( (s3.toCharArray()[j]>='a'&&s3.toCharArray()[j]<='z')||(s3.toCharArray()[j]>='A'&&s3.toCharArray()[j]<='Z')){
                    minute = j;
                    break;
                }
            }
        }

//        System.out.println(minute);
        if (minute < 10||hour<10) {
            System.out.println(week + " " + String.format("%02d", hour) + ":" + String.format("%02d", minute));
        } else {
            System.out.println(week + " " + hour + ":" + minute);
        }
    }
}

发表于 2019-05-16 08:40:55 回复(1)
private static final String[] days = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; private static final String dayCode = "ABCDEFG"; private static final String timeCode = "0123456789ABCDEFGHIJKLMN";  private static String weekDay = null; private static int HOUR = 0; private static int MINUNTE = 0;  public static void main(String[] args) {  Scanner scanner = new Scanner(System.in);  String clude1 = scanner.nextLine();  String clude2 = scanner.nextLine();  String clude3 = scanner.nextLine();  String clude4 = scanner.nextLine();   boolean judgeFlag = false;  boolean flag = false;  for (int i = 0; i < clude1.length(); i++) {  char v = clude1.charAt(i);   if (isLetter(v) && !judgeFlag) {  judgeFlag = true;  }   if (v == clude2.charAt(i)) {  if (!judgeFlag) continue;  if (isUppercase(v) && !flag) {  weekDay = days[dayCode.indexOf(v)];  flag = true;  } else {  if (isRightData(v)) {  if (v <= '9' && v >= '0') {  HOUR = Character.getNumericValue(v);  } else {  HOUR = timeCode.indexOf(v);  }  }  }  }  }   for (int i = 0; i < clude3.length(); i++) {  char v = clude3.charAt(i);  if (isRightData(v) && v == clude4.charAt(i)) {  MINUNTE = i;  break;  }  }  System.out.println(weekDay + " " + (HOUR / 10 >= 1 ? HOUR : ("0" + HOUR)) + ":" + (MINUNTE / 10 >= 1 ? MINUNTE : ("0" + MINUNTE)));  }  private static boolean isUppercase(char ch) {  return 'A' <= ch && 'Z' >= ch; }  private static boolean isLowCase(char ch) {  return 'a' <= ch && 'z' >= ch; }  private static boolean isLetter(char ch) {  return (isLowCase(ch) || isUppercase(ch)); }  private static boolean isRightData(char ch) {  return (ch >= '0' && ch <= '9') || isLetter(ch); }
发表于 2019-04-01 15:40:41 回复(0)
//题目不难,考细心,一开始没读懂题目,最后一对字符串居然要对应位置才行
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str1 = sc.nextLine();
            String str2 = sc.nextLine();
            String str3 = sc.nextLine();
            String str4 = sc.nextLine();
            char[] ch1 = str1.toCharArray();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ch1.length; i++) {
                if (ch1[i] > 'Z' || ch1[i] < 'A' && sb.length() < 1) {
                    continue;
                }
                if (str2.contains(String.valueOf(ch1[i]))) {
                    sb.append(ch1[i]);
                    if (str2.indexOf(ch1[i]) + 1 < str2.length()) {
                        str2 = str2.substring(str2.indexOf(ch1[i]) + 1, str2.length() - 1);
                    }
                }
            }
            switch (sb.charAt(0)) {
            case 'A':
                System.out.print("MON ");
                break;
            case 'B':
                System.out.print("TUE ");
                break;
            case 'C':
                System.out.print("WED ");
                break;
            case 'D':
                System.out.print("THU ");
                break;
            case 'E':
                System.out.print("FRI ");
                break;
            case 'F':
                System.out.print("SAT ");
                break;
            case 'G':
                System.out.print("SUN ");
                break;
            }
            if (sb.charAt(1) > '9') {
                System.out.print(sb.charAt(1) - 'A' + 10);
            } else {
                System.out.print("0" + sb.charAt(1));
            }
            char[] ch2 = str4.toCharArray();
            int minute = -1;
            for (int i = 0; i < ch2.length; i++) {
                if ((ch2[i] >= 'A' && ch2[i] <= 'Z') || (ch2[i] >= 'a' && ch2[i] <= 'z')) {
                    if (str3.charAt(i) == ch2[i]) {
                        minute = i;
                        break;
                    }
                }
            }
            if (minute < 10) {
                System.out.print(":0" + minute);
            } else {
                System.out.print(":" + minute);
            }
        }
    }
}

发表于 2019-03-24 01:24:37 回复(0)
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
  while(in.hasNext()) {
  String[] nn=new String[4];
  for(int i=0;i<nn.length;i++) {
   nn[i]=in.next();
  }
  int o=0;
  for(int i=0;i<nn[0].length();i++) {
   if(nn[0].charAt(i)==nn[1].charAt(i)&&(nn[0].charAt(i)<='Z'&&nn[0].charAt(i)>='A')) {
    String s="";
    switch(nn[0].charAt(i)) {
    case 'A':s="MON";break;
    case 'B':s="TUE";break;
    case 'C':s="WED";break;
    case 'D':s="THU";break;
    case 'E':s="FRI";break;
    case 'F':s="SAT";break;
    case 'G':s="SUN";break;
    }
    System.out.print(s+" ");
    o=i;
    break;
   }
  }
  for(int i=o+1;i<nn[0].length();i++) {
   if(nn[0].charAt(i)==nn[1].charAt(i)) {
    if(nn[0].charAt(i)>='0'&&nn[0].charAt(i)<='9') {
     System.out.print("0"+(nn[0].charAt(i)-'0')+":");
    }
    else {
     System.out.print((10+nn[0].charAt(i)-'A')+":");
    }
    break;
   }   
  }
  for(int i=0;i<nn[2].length();i++) {
   if(nn[2].charAt(i)==nn[3].charAt(i)&&(String.valueOf(nn[2].charAt(i)).toLowerCase().charAt(0)>='a'&&String.valueOf(nn[2].charAt(i)).toLowerCase().charAt(0)<='z')) {
    if(i>=10) {
     System.out.print(i);
    }
    else {
     System.out.print("0"+i);
    }
    break;
   }
  }
 }
}
}

发表于 2019-03-08 18:49:34 回复(0)
importjava.util.Scanner;
publicclassMain{
    publicstaticvoidmain(String[] args) {
        Scanner scan =newScanner(System.in);
        String[] arr=newString[4];
        for(inti=0;i<4;i++){
            String str=scan.next();
            arr[i]=str;
        }
        StringBuffer sb=newStringBuffer("");
        intk=0;
        for(intj=0;j<arr[0].length();j++){
            if(arr[0].charAt(j)>='A'&&arr[0].charAt(j)<='G'&&arr[1].charAt(j)==arr[0].charAt(j)){
                switch(arr[0].charAt(j)){
                    case'A':sb.append("MON");break;
                    case'B':sb.append("TUE");break;
                    case'C':sb.append("WEN");break;
                    case'D':sb.append("THU");break;
                    case'E':sb.append("FRI");break;
                    case'F':sb.append("SAT");break;
                    case'G':sb.append("SUN");break;
                    default:break;
                }
                k=j;
                break;
                 
            }
        }
         
        for(intj=k+1;j<arr[0].length();j++){
            if(((arr[0].charAt(j)>='A'&&arr[0].charAt(j)<='N')||(arr[0].charAt(j)>='0'&&arr[0].charAt(j)<='9'))&&arr[1].charAt(j)==arr[0].charAt(j)){              
                if(arr[0].charAt(j)>='A'&&arr[0].charAt(j)<='N'){
                    sb.append(" "+(10+(arr[0].charAt(j)-'A'))+":");
                }
                if(arr[0].charAt(j)>='0'&&arr[0].charAt(j)<='9'){
                    sb.append(" 0"+arr[0].charAt(j)+":");
                }
                break;
            }
        }
         
        for(intj=0;j<arr[2].length();j++){
            if(((arr[2].charAt(j)>='a'&&arr[2].charAt(j)<='z')||(arr[2].charAt(j)>='A'&&arr[2].charAt(j)<='Z'))&&arr[3].charAt(j)==arr[2].charAt(j)){              
                sb.append(j<10?"0"+j:j);
                break;
            }
        }
        System.out.println(sb.toString());
    }
     
}
发表于 2018-11-17 10:44:00 回复(0)

还是要读题仔细 这题就是考个读题 注意两个字母不仅要判断大小写 是不是数字 还要判断具体字母范围

public class Test4_1 { public static void main(String[] args) {
        String a, b, c, d;
        Scanner in = new Scanner(System.in);
        a = in.next();
        b = in.next();
        c = in.next();
        d = in.next();
        System.out.print(getDay(a,b)+" "+getHour(a,b)+":"+getMin(c,d));
    } public static String getDay(String a, String b) { char day = getSameChar1(a, b).charAt(0); return charToDay(day);
    } public static String getHour(String a, String b) { char hour = getSameChar1(a, b).charAt(1); return charToHour(hour);
    } public static String getMin(String a, String b) { int index = 0; for (int i = 0; i min(a.length(), b.length()); i++) { char a1 = a.charAt(i); char a2 = b.charAt(i); if ((a1 == a2) && isLetter(a1)) {
                index = i; break;
            }
        }
        String result = "";
        String.format("%02d", index);
        result = String.format("%02d", index); return result;
    } //字母转化为星期天的缩写  public static String charToDay(char a) {
        String day = ""; switch (a) { case 'A':
                day = "MON"; break; case 'B':
                day = "TUE"; break; case 'C':
                day = "WED"; break; case 'D':
                day = "THU"; break; case 'E':
                day = "FRI"; break; case 'F':
                day = "SAT"; break; case 'G':
                day = "SUN"; break; default: break;
        } return day;
    } public static String charToHour(char a) {
        String result = ""; if (isUpperCase(a)) {
            result = (a - 55) + "";
        } else {
            result = "0" + a;
        } return result;
    } public static String getSameChar1(String a, String b) { int len = Math.min(a.length(), b.length()); char day = 'a'; char hour = 'a'; int flag = 0; for (int i = 0; i char a1 = a.charAt(i); char b1 = b.charAt(i); if (isBetweenAandG(a1) && (a1 == b1)) {
                day = a1;
                flag = i; break;
            }
        } for (int i = flag + 1; i char a1 = a.charAt(i); char b1 = b.charAt(i); if ((isNum(a1) || isBetweenAandN(a1)) && (a1 == b1)) {
                hour = a1; break;
            }
        } return "" + day + hour;
    } public static boolean isUpperCase(char a) { return a >= 'A' && a 'Z';
    } public static boolean isNum(char a) { return a >= '0' && a '9';
    } public static boolean isBetweenAandN(char a) { return a >= 'A' && a 'N';
    } public static boolean isBetweenAandG(char a) { return a >= 'A' && a 'G';
    } public static boolean isLetter(char a) { return isUpperCase(a) || (a >= 'a' && a 'z');
    }
}
发表于 2018-10-01 00:59:28 回复(0)
注意要对应位置的字符相同才算成对(调到崩溃)
注意序列长度不同
注意三个元素的判定依据
/**
 * 
 * @author ChopinXBP
 * 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。
 * 大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”
 * 前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母'D',代表星期四;第2对相同的字符是'E',
 * 那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、 以及大写字母A到N表示);
 * 后面两字符串第1对相同的英文字母's'出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串
 * 
 * 注意点:1.要对应位置的字符相同才算成对,2.注意序列长度不同,3.注意三个元素的判定依据
 * 
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class HolmesCode {     public static Scanner in = new Scanner(new BufferedInputStream(System.in));     public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));     public static ArrayList<String> str = new ArrayList<>();     public static String[] week = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };     public static int date = -1;     public static int hour = -1;     public static int min;     public static void main(String[] args) {         // TODO Auto-generated method stub         Read();         Decode();         Print();         out.close();         in.close();     }     public static void Read() {         for (int i = 0; i < 4; i++) {             String string = in.nextLine();             str.add(string);         }     }     public static void Decode() {         // 存放前两个序列的相同元素(注意序列长度不同)         ArrayList<Character> sameword = new ArrayList<>();         for (int i = 0; i < Math.min(str.get(0).length(), str.get(1).length()); i++) {             char word = str.get(0).charAt(i);             char word2 = str.get(1).charAt(i);             if (word == word2 && ((word >= 'A' && word <= 'Z') || (word >= '0' && word <= '9'))) {                 sameword.add(word);             }         }         // 解码日期和小时         for (int i = 0; i < sameword.size(); i++) {             Character word = sameword.get(i);             if (date == -1) {                 if (word >= 'A' && word <= 'Z') {                     date = word - 'A';                 }             } else {                 if (word >= 'A' && word <= 'N') {                     hour = word - 'A' + 10;                     break;                 } else if (word >= '0' && word <= '9') {                     hour = Integer.parseInt(word.toString());                     break;                 }             }             if (hour != -1)                 break;         }         // 解码分钟         for (int i = 0; i < Math.min(str.get(2).length(), str.get(3).length()); i++) {             char word = str.get(2).charAt(i);             char word2 = str.get(3).charAt(i);             if (word == word2 && ((word >= 'A' && word <= 'Z') || (word >= 'a' && word <= 'z'))) {                 min = i;                 break;             }         }     }     public static void Print() {         out.print(week[date] + ' ');         if (hour < 10) {             out.print('0');         }         out.print(hour);         out.print(':');         if (min < 10) {             out.print('0');         }         out.print(min);     }
}

发表于 2018-08-16 17:33:52 回复(0)
import java.util.Scanner;  
/**  * 福尔摩斯的约会 */ 
public class Main { 
        static char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G'}; 
        static String[] weeks = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; 

        public static void main(String[] args) {
        Scanner s = new Scanner(System.in);  
        String[] arr = new String[4]; 
        int day = 0; int hour = 0;
        int minute = 0; 
        for (int i = 0; i length; i++) {
            arr[i] = s.nextLine();  
        } 
        int len1 = Math.min(arr[0].length(), arr[1].length()); 
        int len2 = Math.min(arr[3].length(), arr[2].length()); 
        int i; 
        for (i = 0; i < len1; i++) { 
            if (arr[0].charAt(i) > 64 && arr[0].charAt(i) < 91) { 
                if (arr[0].charAt(i) == arr[1].charAt(i)) { 
                    for (int k = 0; k chars.length; k++) { 
                        if (arr[0].charAt(i) == chars[k]) {
                            day = k;  
                            break;  
                        }
                    } 
                    break;  
                }
            }
        } 
        for (i=i+1; i < len1; i++) { 
            if (arr[0].charAt(i) > 64 && arr[0].charAt(i) < 79  
                || arr[0].charAt(i) > 47 && arr[0].charAt(i) < 58) { 
                if (arr[0].charAt(i) == arr[1].charAt(i)) { 
                    if (arr[0].charAt(i) > 47 && arr[0].charAt(i) < 58) {
                        hour = arr[0].charAt(i) - 48;  
                    } else {
                        hour = arr[0].charAt(i) - 55;  
                    } 
                    break;  
                 }
            }
        } 
        //分钟  
        for (i = 0; i <len2; i++) { 
            if (arr[2].charAt(i) > 64 && arr[2].charAt(i) < 91  
                || arr[2].charAt(i) > 96 && arr[2].charAt(i) < 123) { 
                if (arr[2].charAt(i) == arr[3].charAt(i)) {
                    minute = i;  break;  
                 }
            }
        }
        String hours = hour 10 ? "0" + hour : "" + hour;  
        String min = minute 10 ? "0" + minute : "" + minute;  
        System.out.println(weeks[day] + " " + hours + ":" + min);  
     }
}
编辑于 2018-07-25 22:43:36 回复(0)
有没有大佬可以直接键值对写的?
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Pattern;  public class Main { public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  String str = null;  int i=0;  String[] lineData=new String[5];  //获取数据  while (i<4) {//使用readLine方法,一次读一行  str = br.readLine();  if(str.equals("")) { continue;  }else{
                lineData[i] = str;  i++;  }
        } //进行前两个的匹配得到准确的日期前两位  char[] getWeekOne=lineData[0].toCharArray();  char[] getWeekTwo=lineData[1].toCharArray();  char tempweek=' ';  char temphour=' ';  int firstOrTwo=1;  for(int j=0;j<getWeekOne.length;j++){ if(getWeekOne[j]==getWeekTwo[j]) { if (firstOrTwo == 1) { if (Pattern.matches(("[A-Z]"), String.valueOf(getWeekOne[j]))) {
                        tempweek = getWeekOne[j];  firstOrTwo++;  }
                } else { if (Pattern.matches(("[0-9a-zA-Z]"), String.valueOf(getWeekOne[j]))) {
                        temphour=getWeekOne[j];  break;  }
                }
            }
        } //准确分钟  char[] getMinuteOne=lineData[2].toCharArray();  char[] getMinuteTwo=lineData[3].toCharArray();  String finalMinute="";  for(int j=0;j<getMinuteOne.length&&j<getMinuteTwo.length;j++){ if(getMinuteOne[j]==getMinuteTwo[j]){ if (Pattern.matches(("[1-9a-zA-Z]"), String.valueOf(getMinuteOne[j]))) { if (j < 10) {
                            finalMinute = "0" + j;  } else {
                            finalMinute = String.valueOf(j);  } break;  }
                }
        } //确定最终的时间  String finalweek="";  String finalhour="";  switch(tempweek){ case 'A':finalweek="MON";break;  case 'B':finalweek="TUE";break;  case 'C':finalweek="WED";break;  case 'D':finalweek="THU";break;  case 'E':finalweek="FRI";break;  case 'F':finalweek="SAT";break;  case 'G':finalweek="SUN";break;  } switch(temphour){ case '0':finalhour="00";break;  case '1':finalhour="01";break;  case '2':finalhour="02";break;  case '3':finalhour="03";break;  case '4':finalhour="04";break;  case '5':finalhour="05";break;  case '6':finalhour="06";break;  case '7':finalhour="07";break;  case '8':finalhour="08";break;  case '9':finalhour="09";break;  case 'A':finalhour="10";break;  case 'B':finalhour="11";break;  case 'C':finalhour="12";break;  case 'D':finalhour="13";break;  case 'E':finalhour="14";break;  case 'F':finalhour="15";break;  case 'G':finalhour="16";break;  case 'H':finalhour="17";break;  case 'I':finalhour="18";break;  case 'J':finalhour="19";break;  case 'K':finalhour="20";break;  case 'L':finalhour="21";break;  case 'M':finalhour="22";break;  case 'N':finalhour="23";break;  }
        System.out.println(finalweek+" "+finalhour+":"+finalMinute);  }
}

发表于 2018-02-08 22:31:46 回复(1)
 可以AC,这个题目最重要的就是理解题意,第一个是判断大写字母,第二个是只判断字符,可以是大写字母,当然也可以是任意的字符,只是想说这个题目出得真随便,应该是英语翻译过来的 import java.util.Scanner;

public class Main {     @SuppressWarnings("resource")     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         String[] stringArray = new String[4];         for (int i = 0; i < stringArray.length; i++) {             stringArray[i] = in.next();             // 这里真是坑苦我了,next()代表的是你接下来所有输入内容的第一项,nextline()代表如果你输入了一行,             // 那就是这一行,没有第几项的说法         }         // 现在已经拿到了四个字符串,开始进行处理         char weekday = '0';         // 建立一个字符类型的数组,存储第一个相同的字符与第二个相同的字符         char[] charArray = new char[2];         int charArrayIndex = 0;// 给字符类型的数组建立计数器         char finalFirstChar = 0;         char finalSecondChar = 0;         // 这里要明确的是第二个相同的字符         // 这里的相等是上下同位置相等         // 每一次循环过来这个i总会变了的,所以得到的值一直在变         int i;         for (i = 0; i < stringArray[0].length(); i++) {             if (stringArray[0].charAt(i) >= 'A' && stringArray[0].charAt(i) <= 'Z') {                 for (int j = 0; j < stringArray[1].length(); j++) {                     if (stringArray[0].charAt(i) == stringArray[1].charAt(j) && i == j) {                         charArray[0] = (char) stringArray[0].charAt(i);                         finalFirstChar = charArray[0];                         break;                     }                 }             }             if (charArray[0] != '\0') {                 break;             }         }         for (int i1 = i+1; i1 < stringArray[0].length(); i1++) {                 for (int j = 0; j < stringArray[1].length(); j++) {                     if (stringArray[0].charAt(i1) == stringArray[1].charAt(j) && i1 == j) {                             charArray[1] = (char) stringArray[0].charAt(i1);                             finalSecondChar = charArray[1];                             break;                 }             }             if (charArray[1] != '\0') {                 break;             }         }         // 做到这一步,已经拿到了星期几的,可以对这个字母做一个减去A的操作,得到的结果加一就是星期几         int weekdayNum = finalFirstChar - 'A';         String DAY = null;// 这就是星期几的前三个字母         switch (weekdayNum + 1) {         case 1:             DAY = "MON";             break;         case 2:             DAY = "TUE";             break;         case 3:             DAY = "WED";             break;         case 4:             DAY = "THU";             break;         case 5:             DAY = "FRI";             break;         case 6:             DAY = "SAT";             break;         case 7:             DAY = "SUN";             break;         default:             break;         }         // System.out.println(DAY);         // 星期几已经可以顺利输出,接下来研究第二个相同的字符         char secondCharacter = finalSecondChar;// 这是第二个相同的字符         String hour = null;         if (secondCharacter >= '0' && secondCharacter <= '9') {             hour = "0" + secondCharacter;         } else if (secondCharacter >= 'A' && secondCharacter <= 'N') {             hour = String.valueOf(secondCharacter - 'A' + 10);         }         // System.out.println(hour);         // 届此小时也已经打印出来了         int minute = 0;         String minuteString = null;         for (int i1 = 0; i1 < stringArray[2].length(); i1++) {             if ((stringArray[2].charAt(i1) >= 'A' && stringArray[0].charAt(i1) <= 'Z')                     || (stringArray[2].charAt(i1) >= 'a' && stringArray[0].charAt(i1) <= 'z')) {                 for (int j = 0; j < stringArray[3].length(); j++) {                     if (stringArray[2].charAt(i1) - stringArray[3].charAt(j) == 0&&i1==j) {                         minute = i1;                         break;                     }                 }             }             if (minute != 0) {                 break;             }         }         if (minute < 10) {             minuteString = "0" + String.valueOf(minute);         } else {             minuteString = String.valueOf(minute);         }         System.out.println(DAY + " " + hour + ":" + minuteString);     }

}

发表于 2017-09-18 20:31:03 回复(0)

官网AC,供参考。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String[] temp = new String[4];
        for (int i = 0; i < 4; i++) {
            temp[i] = input.readLine();
            if (temp[i].length() > 60 || temp[i] == null)
                System.exit(0);
        }
        input.close();

        getTime(temp);
    }

    private static void getTime(String[] str) {
        int length2 = str[0].length() < str[1].length() ? str[0].length() : str[1].length();
        int length4 = str[2].length() < str[3].length() ? str[2].length() : str[3].length();

        char DAY = ' ', HH = ' ';
        for (int i = 0, j = 0; i < length2; i++) {
            if (j == 0) {
                if (str[0].charAt(i) == str[1].charAt(i) && str[0].charAt(i) <= 'G' && str[0].charAt(i) >= 'A') {
                    DAY = str[0].charAt(i);
                    j++;
                }
            }
            else {
                if (str[0].charAt(i) == str[1].charAt(i) && (str[0].charAt(i) >= '0' && str[0].charAt(i) <= '9'
                        || str[0].charAt(i) <= 'N' && str[0].charAt(i) >= 'A')) {
                    HH = str[0].charAt(i);
                    break;
                }
            }
        }
        if (DAY == ' ' || HH == ' ')
            System.exit(0);

        int MM = -1;
        for (int i = 0; i < length4; i++) {
            if (str[2].charAt(i) == str[3].charAt(i) && (str[2].charAt(i) <= 'Z' && str[2].charAt(i) >= 'A'
                    || str[2].charAt(i) <= 'z' && str[2].charAt(i) >= 'a')) {
                MM = i;
                break;
            }
        }
        if (MM == -1)
            System.exit(0);

        printer(DAY, HH, MM);

    }

    private static void printer(char DAY, char HH, int MM) {
        switch (DAY - 'A') {
        case 0:
            System.out.print("MON ");
            break;
        case 1:
            System.out.print("TUE ");
            break;
        case 2:
            System.out.print("WED ");
            break;
        case 3:
            System.out.print("THU ");
            break;
        case 4:
            System.out.print("FRI ");
            break;
        case 5:
            System.out.print("SAT ");
            break;
        case 6:
            System.out.print("SUN ");
            break;
        }

        if (HH >= '0' && HH <= '9') {
            System.out.print("0" + HH);
        }
        else {
            System.out.print(10 + HH - 'A');
        }

        System.out.printf(":%02d\n", MM);
    }
}
发表于 2017-09-15 12:03:23 回复(0)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		String str0=input.next();
		String str1=input.next();
		String str2=input.next();
		String str3=input.next();
		String text = text(str0,str1,str2,str3);
		System.out.println(text);
	}

	private static String text
                    (String str0,String str1,String str2,String str3) {
		StringBuffer s =new StringBuffer();
		int num=0;
		int sum=0;
		String[] strs={"MON","TUE","WED","THU","FRI","SAT","SUN"};
		a:for(int i=0;i<str0.length();i++){
			char a=str0.toCharArray()[i];
			if(a>=65 && a<72){
				for(int j=0;j<str1.length();j++){
					char b=str1.toCharArray()[j];
					if(b==a){
						num=i+1;
						sum=j+1;
						s.append(strs[a-65]);
						break a;
					}
				}
			}
		}
		s.append(" ");
		a:for(int i=num;i<str0.length();i++){
			char a=str0.toCharArray()[i];
			if((a>47 && a<58)||( a>64 && a<79)){
				for(int j=sum;j<str1.length();j++){
					char b=str1.toCharArray()[j];
					if(b==a){
						s.append(a-48>10?a-55:"0"+(a-48));
						break a;
					}
				}
			}
		}
		s.append(":");
		a:for(int i=0;i<str2.length();i++){
			char a=str2.toCharArray()[i];
			if((a>64 && a<91) || (a>97 && a<123)){
				char b=str3.toCharArray()[i];
				if(b==a){
					s.append(i>9?i:"0"+i);
					break a;
				}
			}
		}
		return s.toString();
	}
}
最简单的思路
编辑于 2017-09-11 10:58:19 回复(0)

package newcode;


import java.util.Scanner;

import java.util.regex.Pattern;


public class SherlockDating {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String array[] = new String[4];

for (int i = 0; i < 4; i++) {

array[i] = scanner.nextLine().trim();

}


char a = findFirstDouble(array[0], array[1], true);

array[0] = array[0].substring(array[0].indexOf(a) + 1);

array[1] = array[1].substring(array[1].indexOf(a) + 1);

char b = findFirstDouble(array[0], array[1], false);

int c = findFirstDouble(array[2], array[3]);


String result = "";


switch (a) {

case 'A':

result += "MON ";

break;

case 'B':

result += "TUS ";

break;

case 'C':

result += "WED ";

break;

case 'D':

result += "THU ";

break;

case 'E':

result += "FRI ";

break;

case 'F':

result += "STA ";

break;

case 'G':

result += "SUN ";

break;

default:

break;

}


if (Character.isDigit(b))

result += ("0" + b);

else

result += b - 55;


result += ":";


if (c < 10)

result += ("0" + c);

else

result += c;


System.out.println(result);


scanner.close();

}


static char findFirstDouble(String a, String b, boolean whetherCapital) {

int position = -1;

for (int i = 0; i < a.length(); i++) {

boolean judge = whetherCapital ? Pattern.compile("[A-Z]").matcher(String.valueOf(a.charAt(i))).matches()

: true;

if (judge && (a.charAt(i) == b.charAt(i))) {

position = i;

break;

}

}

return a.charAt(position);

}


static int findFirstDouble(String a, String b) {

int position = -1;

for (int i = 0; i < a.length(); i++) {

boolean judge = Pattern.compile("[a-zA-Z]").matcher(String.valueOf(a.charAt(i))).matches();

if (judge && a.charAt(i) == b.charAt(i)) {

position = i;

break;

}

}

return position;

}

}


发表于 2017-08-28 11:03:24 回复(0)
用java的实现的方法,有什么可改进的吗?
public class Question4 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char[] chars1 = sc.next().toCharArray();
		char[] chars2 = sc.next().toCharArray();
		char[] chars3 = sc.next().toCharArray();
		char[] chars4 = sc.next().toCharArray();
		sc.close();
		String[] days = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
		String day="MON";
		String hour="00";
		// 对前两个字符数组进行比较
		int len1 = chars1.length;
		int len2 = chars2.length;
		int min = len1 < len2 ? len1 : len2;
		int count = 0; // 遇到相同字符的次数
		for (int i = 0; i < min; i++) {
			if (chars1[i] == chars2[i]) {
				if (count == 0 && !Character.isDigit(chars1[i]) && chars1[i] < 97) {
					int d = Character.toUpperCase(chars1[i]) - 65;
					day = days[d];
					count++;
				} else if (count == 1) {
					int h;
					if (!Character.isDigit(chars1[i])) {
						h = Character.toUpperCase(chars1[i]) - 64 + 9;
					} else {
						h = chars1[i] - 48;
					}
					hour = h+"";
					break;
				}
			}
		}
		String minute="00";
		// 对后两个字符串进行比较
		int len3 = chars3.length;
		int len4 = chars4.length;
		int min2 = len3 < len4? len3 : len4;
		for (int i = 0; i < min2; i++) {
			if(chars3[i]==chars4[i] && Character.isAlphabetic(chars3[i])){
				minute=i+"";
				break;
			}
		}
		hour=hour.length()==1?0+hour:hour;
		minute=minute.length()==1?0+minute:minute;
		System.out.printf("%s %s:%s",day,hour,minute);
	}
}

编辑于 2017-08-18 16:38:40 回复(0)