这题挺让人头疼,但只要逻辑清晰就没有太大问题
题意:
1.依照两条字符串相同的字符来进行解码
2.按照要求输出格式,内容
注意:
1.一定要注意不要写太多无意义判断(可能导致指针越界!).
2.需要用到ASCLL值和char类型的转换,和对应ASCLL值的计算去得到需要的值.
3.格式要规范和注意不要超时!
逻辑:
1.获取内容
2.逐对判断内容
3.解码数据
4.输出内容
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/*获取输入的四条字符串*/
String row1 = scanner.next();
String row2 = scanner.next();
String row3 = scanner.next();
String row4 = scanner.next();
/*获取前后两对字符串分别的最短长度,避免发生指针越界*/
int min1 = row1.length()>row2.length()?row2.length():row1.length();
int min2 = row3.length()>row4.length()?row4.length():row3.length();
boolean isdouble = false;//用于方便判断第一对内容的第一关键字符和第二关键字符的区分.
for(int a = 0 ;a<min1;a++){
//读取对应下标的char字符.
char row1Value=row1.charAt(a),row2Value=row2.charAt(a);
//当两个字符一样时,且是属于0~9或A~N之间,一定不要用A~Z会报错误答案
if((row1Value==row2Value)&&((row1Value>='0'&&row1Value<='9')||(row1Value>='A'&&row1Value<='N'))){
if(isdouble){//是第二个关键字符
int ascll =((int)row1.charAt(a));//转为ASCLL码
/*[0~9]的ASCLL码范围[48~57],[A~Z]的ASCLL码范围[65~91]*/
//如果是属于[A-Z]就输出修改后的值,[0~9]同理.
System.out.print((ascll>=65?((ascll-55)+""):("0"+(ascll-48)))+":");
//跳出省内存
break;
//判断第一个关键字符是否在范围内,避免出现0~9被识别进入.
}else if((row1Value>='A'&&row1Value<='G')){
switch(row1Value){//这里可以写方法来返回对应内容(可以好看点
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;
}
isdouble = true;
}
}
}
for(int a = 0 ;a<min2;a++){
char row3Value=row3.charAt(a),row4Value=row4.charAt(a);
if(row3Value==row4Value&&(row3Value>='a'&&row3Value<='z')|| (row3Value>='A'&&row3Value<='Z')){//注意包括大小写
if(a<10){
System.out.print("0"+a);
}else{
System.out.print(a);
}
break;
}
}
}
}
感谢大家的阅读!