2020-3-22编程题

声明:本人只是按照题目编写出解题程序,并未考虑算法的效率,不足的地方望大家指正,有更好的解题思路,欢迎评论。

1、卖水果

题目:输入购买次数n和每次购买的水果,求卖出苹果的次数?
例:	
n=1 apple orange banana 
n=2 apple orange				
n=3 orange
例子中,每次购买的水中加入到string数组中,
String[1] = "apple orange banana";
String[2] = "apple orange";
String[3] = "orange";
输出:apple 2
//第一题,卖水果
	public static void f1(int n, String[] str) {
		int count = 0;
		for (int i = 0; i < str.length; i++) {
			if (str[i].toCharArray().length > 5) {
				String[] split = str[i].split(" ");
				for (int j = 0; j < split.length; j++) {
					if (split[j].equals("apple")) {
						count++;
					}
				}
			}
			if (str[i].equals("apple")) {
				count++;
			}
		}
		System.out.println("apple " +count);
	}
	

2、合并数列输出

题目:输入int x,int y,int[x]nums1,int[y]nums2
x和y分别表示,nuns1和nums2的长度
如:x=3 y = 4
nums1 = [1,2,3];
nums2=[3,4,5,2];
输出:1,3,4,5

如:x=2 y = 2
nums1 = [7,5];
nums2=[3,4];

输出:3,4,5,7
//第二题,合并输出数组
	public static void f2(int x, int y, int[] arr1, int[] arr2) {
		//数组合并
		int[] s = new int[x + y-1];
		for (int i = 0; i < x; i++) {
			s[i] = arr1[i];
		}
		for (int j = 0; j < y-1; j++) {
			s[x + j] = arr2[j];
		}
		//排序
		Arrays.sort(s);
		//查找重复的值
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int val : s) {
			Integer count = map.get(val);
			if (map.containsKey(val)) {
				map.put(val, count + 1);
			} else {
				map.put(val, 1);
			}
		}
		//遍历map,输出未重复的值
		for(int key:map.keySet()){
			if(map.get(key)==1){
				System.out.println(key);
			}
		}

	}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值