Fibonacci String-Java代码

在这里插入图片描述
在这里插入图片描述

解析:
在这里插入图片描述
f(i)中各个字母出现的次数 = f(i - 1)中 各个字母出现的次数 + f(i - 2)中各个字母出现的次数.

我的思路是把字母和次数存储在一个双列集合中,然后进行计数运算,但是自己写的代码太麻烦了,即使也AC了,真心觉得自己很渣,看了别人的博客写的代码,写的很简练,所以。。。。。。
革命尚未成功,自己还需努力!

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		while (n-- > 0) {
			String s1 = sc.next();
			String s2 = sc.next();
			int k = sc.nextInt();
			if (k >= 2) {
				HashMap<Character, Integer> map0 = new HashMap<Character, Integer>();
				fillWithZero(map0);
				Statistics(map0, s1);
				HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
				fillWithZero(map1);
				Statistics(map1, s2);
				HashMap<Character, Integer> map2 = new HashMap<Character, Integer>();
				while (k-- > 1) {
					int num = 97;
					for (int i = 0; i < 26; i++) {
						char key = (char) num;
						map2.put(key, map0.get(key) + map1.get(key));
						num++;
					}
					map0.clear();
					map0.putAll(map1);
					map1.clear();
					map1.putAll(map2);
				}
				print(map1);
			} else {
				HashMap<Character, Integer> map = new HashMap<Character, Integer>();
				fillWithZero(map);
				if (k == 0) {
					Statistics(map, s1);
				} else if (k == 1) {
					Statistics(map, s2);
				}
				print(map);
			}
			System.out.println();
		}
	}

	public static void fillWithZero(HashMap<Character, Integer> map) {//把集合中的每一个键的次数都列为一
		int num = 97;
		for (int i = 0; i < 26; i++) {
			char C = (char) num;
			map.put(C, 0);
			num++;
		}
	}

	public static void Statistics(HashMap<Character, Integer> map, String s) {//遍历字符串s,统计每一个字母出现的次数
		for (int i = 0; i < s.length(); i++) {
			if (map.containsKey(s.charAt(i))) {
				map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
			}
		}
	}
/*本来想遍历双列集合,输出键和值,但是HashMap底层是由哈希算法实现的,不能保证怎么存就怎么取,也不能用Collections.sort();方法进行排序,所以就写了这样一个方法*/
	public static void print(HashMap<Character, Integer> map) {//按示例的格式输出
		int num = 97;
		for (int i = 0; i < 26; i++) {
			char key = (char) num;
			System.out.println(key + ":" + map.get(key));
			num++;
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用数组循环、递归和递归方法实现斐波那契数列的完整代码: 使用数组循环: ```java public class test4 { public static void main(String[] args) { int[] arr = new int = 1; arr = 1; for (int i = 2; i < arr.length; i++) { arr[i = arr[i - 1 + arr[i - 2]; } String s = Arrays.toString(arr); System.out.println("前十个斐波那契数是:" + s); } } ``` 使用递归方法: ```java public class Demo2 { private static int getFibo(int i) { if (i == 1 || i == 2) return 1; else return getFibo(i - 1) + getFibo(i - 2); } public static void main(String[] args) { System.out.println("斐波那契数列的前20项为:"); for (int j = 1; j <= 20; j++) { System.out.print(getFibo(j) + "\t"); if (j % 5 == 0) System.out.println(); } } } ``` 使用递归的方法: ```java public class test2 { public static void main(String[] args) { for (int counter = 1; counter <= 10; counter++) { System.out.printf("Fibonacci of %d is: %d\n", counter, fibonacci(counter)); } } public static long fibonacci(long number) { if ((number == 0) || (number == 1)) return number; else return fibonacci(number - 1) + fibonacci(number - 2); } } ``` 请注意,以上代码是完整的斐波那契数列的实现。不同的代码使用了不同的方法来计算斐波那契数列,你可以根据自己的需要选择其中一个方法来使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [斐波那契数列java代码实现](https://blog.csdn.net/weixin_48720080/article/details/120212504)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [斐波那契数列——java代码实现](https://blog.csdn.net/lb812913059/article/details/83313893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值