华为机考-手拍球游戏

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

	}

}
相关推荐
echeverra2 小时前
萌萌战将,喵的天下,三国猫H5游戏详细图文架设教程
游戏·游戏开发
Java&Develop2 小时前
html写一个象棋游戏
javascript·游戏·html
wanhengidc9 小时前
云手机 多开无忧 科技运行
运维·服务器·科技·游戏·智能手机
牛掰是怎么形成的18 小时前
性能优化:线程数量、CPU绑定、负载均衡——游戏多线程场景详解与C#实战
游戏·性能优化·负载均衡
_大学牲1 天前
Flutter 勇闯2D像素游戏之路(二):绘制加载游戏地图
flutter·游戏·游戏开发
top_designer1 天前
PS 样式参考:3D 白模直接出原画?概念美术的“光影魔术手”
游戏·3d·prompt·aigc·技术美术·建模·游戏美术
汝生淮南吾在北1 天前
SpringBoot+Vue游戏攻略网站
前端·vue.js·spring boot·后端·游戏·毕业设计·毕设
weixin_307779131 天前
满足游戏应用低延迟和历史查询需求的解决方案
游戏·云原生·架构·云计算·aws
da_vinci_x1 天前
PS 图案预览 + Sampler:告别“修接缝”,AI 量产 4K 无缝 PBR
人工智能·游戏·aigc·贴图·技术美术·游戏美术·法线贴图
摘星编程1 天前
从云游戏到Mac远程操控:UU远程游戏、办公场景全覆盖
游戏·macos·uu远程