华为机考-手拍球游戏

【手拍手计算次数和总数】游戏规则:左手和右手拍球初始数为0,首先左手第一次拍球数1下,右手拍球1下,接下来左手在拍球时是上一次左手+上一次右手的总和,右手也是上一次左手+上一次右手拍球的总和,最后拍球总数必须等于输入的拍球总和,并按规定输出。

现给定一个拍球总数,和最后输出时要打印的哪只手拍过的所有次数。每超过10 个元素,请换行输出。

输入描述:

输入为一行,拍球总数+最后打印某只手拍球的所有数量,1<= 拍球总数<=5000

输出描述:

某只手的拍的次数+某只手的拍球的所有数量

 实例1:                                                                                                                                             

输入:

150,left hand

输出:

now is 11 times by left hand 
1 2 5 13 34 7

 实例2:                                                                                                                                             

输入:

100,right hand

输出:

now is 10 times by right hand 
1 3 8 21 12

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class testOk {

	@Test
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String s = in.nextLine();
		String[] split = s.split(",");

		// 计算
		Integer arriveCount = Integer.parseInt(split[0]);
		Integer countPaiJi = 0;
		Integer leftActualCount = 0;
		Integer rightActualCount = 0;
		Integer actualSumCount = 0;

		ArrayList<Integer> leftList = new ArrayList<>();
		ArrayList<Integer> rightList = new ArrayList<>();

		while (arriveCount.compareTo(actualSumCount) != 0) {
			countPaiJi = countPaiJi + 1;
			Integer cuurentCount = (leftActualCount + rightActualCount) - 0 == 0 ? 1 : (leftActualCount + rightActualCount);
			Integer virSumCount = actualSumCount + cuurentCount;
			if (virSumCount > arriveCount) {
				cuurentCount = arriveCount - actualSumCount;
			}
			if (countPaiJi % 2 == 0) {
				rightActualCount = cuurentCount;
				rightList.add(cuurentCount);
			} else {
				leftActualCount = cuurentCount;
				leftList.add(cuurentCount);
			}
			actualSumCount = actualSumCount + cuurentCount;
		}

		// 输出
		String beforeMsgString = "now is ";
		String endMsgString;
		ArrayList<Integer> responseList;
		if (split[1].contains("left")) {
			responseList = leftList;
			endMsgString = " times by left hand ";
		} else {
			responseList = rightList;
			endMsgString = " times by right hand ";
		}
		List<List<Integer>> partitionList = ListUtils.partition(responseList, 10);
		System.out.println(beforeMsgString + countPaiJi + endMsgString);
		if (CollectionUtils.isEmpty(partitionList)) {
			System.out.println("0");
		}
		for (List<Integer> integers : partitionList) {
			String collect = integers.stream().map(String::valueOf).collect(Collectors.joining(" "));
			System.out.println(collect);
		}

	}

}

相关推荐

  1. 华为-游戏

    2024-01-06 12:12:02       53 阅读
  2. 华为真题 -- 游戏分组

    2024-01-06 12:12:02       25 阅读
  3. 华为真题 -- 篮球游戏

    2024-01-06 12:12:02       25 阅读
  4. 华为真题 -- 密码解密

    2024-01-06 12:12:02       22 阅读
  5. 华为真题】字符串压缩

    2024-01-06 12:12:02       25 阅读
  6. 华为:HJ2 计算某字符出现次数

    2024-01-06 12:12:02       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 12:12:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 12:12:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 12:12:02       82 阅读
  4. Python语言-面向对象

    2024-01-06 12:12:02       91 阅读

热门阅读

  1. Python 基础面试第四弹

    2024-01-06 12:12:02       48 阅读
  2. C++_多继承

    2024-01-06 12:12:02       57 阅读
  3. uniapp 跨页面传参的几种方式

    2024-01-06 12:12:02       53 阅读
  4. Qt 浮点数比较大小

    2024-01-06 12:12:02       54 阅读