import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Stream;
public class Test02 {
public static void main(String[] args) {
int sum = test(6);
System.out.println(sum);
String str = "thisismyhomemm";
char repeat = getRepeat(str);
System.out.println( repeat);
}
// 斐波那契数列 Fn = f(n-1)+f(n-2) 1 1 2 3 5 8 ....
public static int test(int n){
if (n == 1 || n == 2){
return 1;
}else{
return test(n-1) + test(n-2);
}
}
// 获取一个字符串中出现字符最多的字符
public static char getRepeat(String string){
char[] chars = string.toCharArray();
System.out.println(chars);
Map<Character,Integer> map = new HashMap<>();
for (int i = 0; i <chars.length-1 ; i++) {
int count = 0;
if (map.containsKey(chars[i])){
map.put(chars[i],map.get(chars[i])+1);
}else{
map.put(chars[i],1);
}
}
Collection<Integer> values = map.values();
Integer max = Collections.max(values);
Set<Map.Entry<Character, Integer>> entries = map.entrySet();
char c = 0;
for (Map.Entry<Character, Integer> entry : entries) {
if (entry.getValue() == max){
c = entry.getKey();
}
}
return c;
}
}
Java上机题一
最新推荐文章于 2022-06-26 16:43:28 发布