特别说明:由于本人编程水平有限,算法特别糟糕,所提供解答为个人随心所写,后两题时间复杂度都为O(n*n),这显然不是阅卷人员希望看到的,希望路过的各位大牛能多多指教,不吝赐与小可更佳的设计。
1. 电文加密(30分)
有一行电文译文下面规律译成密码:
A->Z a->z
B->Y b->y
C->X c->x
...
即第一个字母变成第26个字母,第i个字母变成第(26-i+1)个字母。非字母字符不变,请编写函数将输入字符串加密
public String encrypt(String input)
【输入】String input,源字符串
【返回】字符串类型,加密后的字符串
1) 输入:input = "abcd34!z"
返回:"zyxw34!a"
2) 输入:input = "Xyz45@#1b"
返回:"Cba45@#1y"
2. 找重复字符(30分)
有一字符串,里面可能包含英文字母(大写、小写)、数字、特殊字符,请编写函数找出里面的重复字符,并将这些重复字符组成新的字符串输出。
public String RepeatingChar(String input)
【输入】String input,输入字符串
【返回】字符串类型, 重复字符组成的新字符串
注:如果没有重复字符串,返回空字符串;如果有多个重复字符,只返回一个字符。
1) 输入:input = "ftgfklzevcg"
返回:"fg
2) 输入:input = "qwertyuiopqawsedrfr"
返回:"qwer"
3. 通配符匹配(40分)
判断包含通配符的匹配字符串是否完全匹配输入的字符串。匹配子串中包含的通配符仅有“*”和“?”,且它们都仅表示一个不为空的字符。
public int StrFMTCmp(String strCmp, String strMatch)
【输入】String strCmp,输入的字符串
String strMatch,匹配子串
【返回】整形,0:不匹配;1:匹配
输入:strCmp = "wertyb",strMatch = "w*r"
输出:1
输入:strCmp = "dfgibeg",strMatch = "*?c"
输出:0
输入:strCmp = "hjkjgb",strMatch = "*?k"
输出:1
由于是初级试题,都比较简单,不做过多叙述,直接附上个人编写源代码:
package huawei.exam; /** * <p> * Title: SWTest * </p> * * <p> * Description: * </p> * * <p> * Copyright: Copyright (c) 2012 * </p> * * <p> * Company: huawei * </p> * * @author kaiping he * @version 1.0 */ public class SWTest { public static void main(String[] args) { // TODO:可以在此处添加测试代码 SWTest test = new SWTest(); // 电文加密测试 System.out.println("---------------电文加密测试---------------"); String input1 = "abcd34!z"; String input2 = "Xyz45@#1b"; System.out.println("input1:" + input1 + "\noutput1:" + test.encrypt(input1) + "\ninput2:" + input2 + "\noutput2:" + test.encrypt(input2)); System.out.println("---------------找重复字符测试---------------"); String input3 = "ftgfklzevcg"; String input4 = "qwertyuiopqawsedrfr"; System.out.println("input1:" + input3 + "\noutput1:" + test.RepeatingChar(input3) + "\ninput2:" + input4 + "\noutput2:" + test.RepeatingChar(input4)); System.out.println("---------------通配符匹配测试---------------"); String strCmp1 = "wertyb"; String strMatch1 = "w*r"; String strCmp2 = "dfgibeg"; String strMatch2 = "*?c"; String strCmp3 = "hjkjgb"; String strMatch3 = "*?k"; System.out.println("strCmp1:" + strCmp1 + "\nstrMatch1:" + strMatch1 + "\nresult1:" + test.StrFMTCmp(strCmp1, strMatch1) + "\nstrCmp2:" + strCmp2 + "\nstrMatch2:" + strMatch2 + "\nresult2:" + test.StrFMTCmp(strCmp2, strMatch2) + "\nstrCmp3:" + strCmp3 + "\nstrMatch3:" + strMatch3 + "\nresult3:" + test.StrFMTCmp(strCmp3, strMatch3)); } /** * 电文加密 * * @param * @return */ public String encrypt(String input) { if (input == null) { return ""; } char temp[] = input.toCharArray(); for (int i = 0; i < temp.length; i++) { if (temp[i] >= 'a' && temp[i] <= 'z') { temp[i] = (char) ('z' - (temp[i] - 'a')); } else if (temp[i] >= 'A' && temp[i] <= 'Z') { temp[i] = (char) ('Z' - (temp[i] - 'A')); } } return new String(temp, 0, temp.length); } /** * 找重复字符 * * @param * @return */ public String RepeatingChar(String input) { StringBuffer result = new StringBuffer(""); if (input == null || "".equals(input)) { return result.toString(); } StringBuffer tmp = new StringBuffer(input); char c; boolean isRepeat; for (int i = 0; i < tmp.length(); i++) { c = tmp.charAt(i); isRepeat = false; for (int j = i + 1; j < tmp.length(); j++) { if (c == tmp.charAt(j)) { isRepeat = true; tmp.deleteCharAt(j); } } if(isRepeat){ result.append(c); } } return result.toString(); } /** * 通配符匹配 * * @param * @return */ public int StrFMTCmp(String strCmp, String strMatch) { int result = -1; if (strCmp != null && strMatch != null) { result = 0; char c; for (int i = 0; i < strCmp.length(); i++) { if (strCmp.length() - i < strMatch.length()) {// 如果剩下的字符个数已经小于匹配串的长度 break; } int j = 0; for (; j < strMatch.length(); j++) { c = strMatch.charAt(j); if (c == '*' || c == '?') { if (strCmp.charAt(i + j) == '\0') { break; } continue; } if (c != strCmp.charAt(i + j)) { break; } }// end for if (j == strMatch.length()) { result++; } }// end for }// end if return result; } }