不记得原题,题目的大概意思是输入一段字符串,字符串里面包含字母大、小写和数字,需要应试者找出字符串中的数字,并把出现次数最多的数字求和输出。
也许我表达不够准确,举个例子吧:输入——ss2kk3kh4n11h11gg2k11字符串;输出:33。
本人程序源代码如下,请多多指点!
package test3;
/*
* 阿里笔试最后一题编程题
*/
import java.util.*;
public class Main1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
fuc(str);
}
}
public static void fuc(String str) {
List<Integer> list = new ArrayList<>();
int len = str.length();
int s = 0;
int z = 0;
int f = 0;
for (int i = 0; i < len; i++) {
boolean b = str.charAt(i) < 58 && str.charAt(i) > 47;
if (b)
s++;
else
z++;
if (s == 0) {
z = 0;
continue;
} else if (s > 0 && z == 1 || s > 0 && i == len - 1) {
String s1 = null;
if (i == len - 1) {
f++;
s1 = str.substring(i - f + 1, i + 1);
} else
s1 = str.substring(i - f, i);
int sum = 0;
for (int j = 0; j < f; j++) {
sum += (s1.charAt(j) - 48) * Math.pow(10, f - j - 1);
}
list.add(sum);
s = 0;
z = 0;
f = 0;
}
else if (s > 0 && z == 0) {
f++;
continue;
}
}
List<Integer> lis = new ArrayList<>();
Map<Integer, Integer> m = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
if (!lis.contains(list.get(i))) {
lis.add(list.get(i));
m.put(list.get(i), 1);
} else {
m.put(list.get(i), m.get(list.get(i)) + 1);
}
}
Set<Integer> set = m.keySet();
Iterator<Integer> it = set.iterator();
int max = 0;
int k = 0;
while (it.hasNext()) {
int it0 = it.next();
int it1 = m.get(it0);
if (max < it1) {
max = it1;
k = it0;
}
}
System.out.println(max * k);
}
}