直接上代码
试题1:
1: public class Test1 {
2:
3: static void convert(String str) {
4: int count = 1; // 用于记录当前连续字符的数量
5: char[] input = str.toCharArray();
6: char pre = '\u0000', cur; // char为空判断
7: char output;
8:
9: for (char ele : input) {
10: cur = ele;
11: if (cur == pre) {
12: count++;
13: // 连续两个字符
14: if (count == 2) {
15:
16: output = (char) traverse(cur,cur + 2);
17: System.out.print(output);
18: }
19: // 连续三个及以上字符
20: if (count >= 3) {
21: output = (char) traverse(cur,cur + count - 2);
22: System.out.print(output);
23: }
24: } else {
25: count = 1;
26: pre = cur; // 赋值
27:
28: output = (char) traverse(cur,ele + 1);//traverse
29: System.out.print(output);
30: }
31:
32: }
33: System.out.println();
34: }
35:
36: static int traverse(char ch,int num)
37: {
38: boolean upper = false;
39: if(Character.isUpperCase(ch))
40: {
41: upper = true;
42: }
43: // 越界z
44: if(upper==false&&num>122)
45: {
46: num = (num-97)%26+97;//num = (char)(num-26);
47: //System.out.println(num);
48: }
49: // 越界Z
50: if(upper&&num >90)
51: {
52: num = (num-65)%26+65;//num = (char)(num-26);
53: }
54: return num;
55: }
56:
57: /**
58: * @param args
59: */
60: public static void main(String[] args) {
61: // TODO Auto-generated method stub
62: String input1 = "abcd";
63: String input2 = "abbcd";// "abb";
64: String input3 = "abbbcd";
65: String input4 = "abbbbbcccd";
66: String input5 = "abzzcdeeeg";
67:
68: convert(input1);
69: convert(input2);
70: convert(input3);
71: convert(input4);
72: convert(input5);
73:
74: System.out.println((char)67);
75: System.out.println('a' > 96);
76: }
77: }
试题2:
1: import java.util.ArrayList;
2: import java.util.Collections;
3: import java.util.Comparator;
4: import java.util.HashMap;
5: import java.util.List;
6: import java.util.Map;
7: import java.util.Map.Entry;
8:
9: public class Test2 {
10:
11: static void sort(String str) {
12: char[] input = str.toCharArray();
13:
14: /* 字符串替换思路
15: * char[] replace = new char[input.length];
16: for (int i = 0; i < input.length; i++) {
17: // 这里可以用正则进行通配符的匹配
18: if (input[i] == ',' || (input[i] > '1' && input[i] < '9')) {
19: replace[i] = ' '; //数据替换
20: } else
21: replace[i] = input[i];
22: //System.out.print(replace[i]);
23: }
24: String rep = String.valueOf(replace); //数据处理后的字符串
25: System.out.println(rep);
26: String[] array = rep.split(" "); //这里没有对string合法做验证
27:
28: HashMap<String, Integer> map = new HashMap<String, Integer>();
29: for (String s : array) {
30: if (s != "" && !s.equals("")) {
31: map.put(s, s.length());
32: }
33: }*/
34:
35: HashMap<String, Integer> map = new HashMap<String, Integer>();
36: StringBuffer temp = new StringBuffer();
37: for(int i=0;i<input.length;i++)
38: {
39: // 小写大写字母
40: if((input[i]>96&&input[i]<123)||(input[i]>64&&input[i]<91))
41: {
42: temp.append(input[i]);
43: }
44: // 即单词已经找到
45: else if(temp.length()>1)
46: {
47: map.put(temp.toString(), temp.length());
48: temp = new StringBuffer();// 置空处理
49: }
50: }
51:
52: List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(
53: map.entrySet());
54:
55: //根据字符串长度进行排序
56: Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
57: public int compare(Map.Entry<String, Integer> o1,
58: Map.Entry<String, Integer> o2) {
59: return (o2.getValue() - o1.getValue());
60: //return (o1.getKey()).toString().compareTo(o2.getKey());
61: }
62: });
63:
64: for (int i = 0; i < list.size(); i++) {
65: String id = list.get(i).toString();
66: System.out.print(id + " ");
67: }
68: System.out.println();
69:
70: for (Entry<String, Integer> e : list) {
71: if (e.getValue() != 1)
72: System.out.print(e.getKey() + " ");
73: }
74:
75: }
76:
77: /**
78: * @param args
79: */
80: public static void main(String[] args) {
81: // TODO Auto-generated method stub
82:
83: String str1 = "some drivers drink when the d234drives";
84: sort(str1);
85: }
86: }
试题3:
1: public class Test3 {
2: // 适合int范围内数
3: static String minus(String s1, String s2) {
4: char[] str1 = s1.toCharArray();
5: char[] str2 = s2.toCharArray();
6:
7: int forward_number1 = 0, forward_number2 = 0; // 两个数的整数部分
8: int backward_number1 = 0, backward_number2 = 0; // 两个数的小数部分对应的整数
9:
10: int len1 = str1.length, len2 = str2.length;
11: int pos1 = -1, pos2 = -1;
12:
13: double number1, number2;
14:
15: if (s1.indexOf('.') != -1) {
16: pos1 = s1.indexOf('.');
17: pos2 = s2.indexOf('.');
18: }
19:
20: for (int i = 0; i < str1.length; i++) {
21: if (pos1 != -1 && i < pos1) {
22: forward_number1 = forward_number1 * 10 + (str1[i] - '0');
23: } else if (i > pos1)
24: backward_number1 = backward_number1 * 10 + (str1[i] - '0');
25: }
26: // System.out.println(forward_number1 + " " + backward_number1);
27:
28: for (int i = 0; i < str2.length; i++) {
29: if (pos2 != -1 && i < pos2) {
30: forward_number2 = forward_number2 * 10 + (str2[i] - '0');
31: } else if (i > pos2) {
32: backward_number2 = backward_number2 * 10 + (str2[i] - '0');
33: }
34: }
35: // System.out.println(forward_number2 + " " + backward_number2);
36:
37: // 更简单的处理方法是:直接将小数转换为整数
38: int mul = (len1 - pos1) > (len2 - pos2) ? (len1 - pos1 - 1) : (len2
39: - pos2 - 1);
40:
41: number1 = forward_number1 * Math.pow(10, mul) + backward_number1;
42: number2 = forward_number2 * Math.pow(10, mul) + backward_number2;
43: // System.out.println(number1 + " " + number2);
44:
45: double result = (number1 - number2) / Math.pow(10, mul);
46: // System.out.println(result);
47:
48: return String.valueOf(result);
49: }
50:
51: /**
52: * @param args
53: */
54: public static void main(String[] args) {
55: // TODO Auto-generated method stub
56:
57: String s11 = "1.1";
58: String s12 = "2.2";
59:
60: String s21 = "2.2";
61: String s22 = "1.1";
62:
63: String s31 = "10.1";
64: String s32 = "9.23";
65:
66: System.out.println(minus(s12, s11));
67: System.out.println(minus(s22, s21));
68: System.out.println(minus(s32, s31));
69: }
70:
71: }