Fibonacci数列计算+找出出现次数最多字符+不同数字的排列组合
1,斐波拉契数列的计算
package chapter9;import java.util.Scanner;public class Fibonacci {private static int k=0;public static void main(String[] args) { //递归求菲波那契数列,f1=1,f2=1Scanner ci = new Scanner(System.in);long a = ci.nextLong();System.out.println(fibonacci(a));System.out.println("共调用了"+k+"次");}private static long fibonacci(long a) {if(a==0||a==1) {k++;return a;}else return fibonacci(a-1)+fibonacci(a-2);}}
package chapter9;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.TreeSet;public class FindMaxAppear {// 求一个字符串中出现次数最多的那个字母及次数public static void main(String[] args) {String input = "abcabbbbbbkgllllslll";doString(input);//方法二:简单易行char[] a= input.toCharArray();int[] count = new int[127]; //ASCII码字符最多为127个for(int i=0;i<a.length;i++){count[a[i]]++; //字符为a[i]的出现次数++}//找出count中最大值,如果有多个就算多个int maxCount = 0;char maxValue;//出现次数最多的字符StringBuffer sb = new StringBuffer();for(int i=0;i<count.length;i++){if(count[i]>maxCount)maxCount=count[i];}for(int i=0;i<count.length;i++){if(count[i]==maxCount){maxValue = (char) i;sb.append(maxValue+",");}}System.out.println("出现次数最多的字符有:"+sb+"出现次数为:"+maxCount);//方法二}private static void doString(String input) {char[] chars = input.toCharArray();List<String> list = new ArrayList<String>();TreeSet<String> set = new TreeSet<String>(); // 对插入到的元素进行自然排序for (int i = 0; i < chars.length; i++) {list.add(String.valueOf(chars[i]));set.add(String.valueOf(chars[i])); // 添加了字母,而且没有重复的。}Collections.sort(list);// 对list进行排序StringBuffer sb = new StringBuffer();for (int i = 0; i < list.size(); i++) {sb.append(list.get(i));}input = sb.toString();// input已经是排好序的int max = 0; // 字符数最多的个数String maxString = "";List<String> maxList = new ArrayList<String>(); // 字符数最多的可能有多个,如同时有4个a和b,而且最多Iterator<String> it = set.iterator();while (it.hasNext()) {String current = it.next();int begin = input.indexOf(current);int end = input.lastIndexOf(current);int value = end - begin + 1; // 字符current出现过value次if (value > max) {max = value;maxString = current;maxList.add(current); } else if (value == max) {maxList.add(current);}}// while循环结束后,maxString存放的就是出现次数最多的字符,max为出现最多的次数int index = 0;for (int i = 0; i < maxList.size(); i++) { //找到maxString的下标,则maxList之后添加的可能是maxString,之前肯定不是出现次数最多的,否则if (maxList.get(i).equals(maxString)) {index = i;break;}}System.out.println("出现最多的字符分别为:");for (int i = index; i < maxList.size(); i++) {System.out.println(maxList.get(i) + " ");}System.out.println();System.out.println("出现最多的次数为:" + max);}}
package chapter9;import java.util.Arrays;import java.util.LinkedList;import java.util.List;public class ListAllNum { //列出数组中的所有数的组合,前提是没有重复数字public static void main(String[] args) {String[] str = {"1","2","3","4"};listAll(Arrays.asList(str),"");}private static void listAll(List<String> asList, String prefix) {System.out.println(prefix);for(int i=0;i<asList.size();i++){List<String> temp = new LinkedList(asList);//构造包含asList中元素的链表listAll(temp,prefix+temp.remove(i));}}}