华为机考-手拍球游戏

**【手拍手计算次数和总数】**游戏规则:左手和右手拍球初始数为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

java 复制代码
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);
		}

	}

}
相关推荐
阿阳微客11 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
m0_5522008217 小时前
《UE5_C++多人TPS完整教程》学习笔记37 ——《P38 变量复制(Variable Replication)》
c++·游戏·ue5
量子炒饭大师3 天前
项目实战——C语言扫雷游戏
c语言·开发语言·游戏
开开心心就好3 天前
高效视频倍速播放插件推荐
python·学习·游戏·pdf·计算机外设·电脑·音视频
DanmF--4 天前
C#面向对象实践项目--贪吃蛇
开发语言·游戏·c#·游戏程序
Code_流苏4 天前
Python趣学篇:从零打造智能AI井字棋游戏(Python + Tkinter + Minimax算法)
python·算法·游戏·tkinter·智能井字棋·minimax算法
RedJACK~5 天前
【Go语言】Ebiten游戏库开发者文档 (v2.8.8)
开发语言·游戏·golang
Sui_Network5 天前
从公开到私密:重新思考 Web3 的数据安全
人工智能·游戏·web3·去中心化·区块链
土豆宝5 天前
AI玩游戏的一点尝试(5)—— 多样化的数字识别
人工智能·游戏
陈哥聊测试5 天前
游戏公司如何同时管好上百个游戏项目?
游戏·程序员·游戏开发