华为机考-手拍球游戏

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

	}

}
相关推荐
weixin_lynhgworld12 小时前
从概率游戏到价值创造:盲盒一番赏的透明化革命
游戏
liulilittle1 天前
游戏加速器核心技术:动态超发
开发语言·网络·c++·网络协议·游戏·加速器·游戏加速
心疼你的一切2 天前
Unity 多人游戏框架学习系列一
学习·游戏·unity·c#·游戏引擎
Jamie201901063 天前
《恋与深空》中龙和蛇分别是谁的代表
游戏
李詹3 天前
Pitaya 是一个简单、快速、轻量级的游戏服务器框架,它为分布式多人游戏和服务器端应用程序提供了一个基本的开发框架
游戏·游戏引擎·游戏程序
TESmart碲视3 天前
显示器核心三要素详解:刷新率、分辨率、色深
物联网·游戏·计算机外设·电脑·智能硬件
凯基迪科技3 天前
游戏设备软件加密锁复制:技术壁垒与安全博弈
安全·游戏
WarPigs4 天前
游戏框架笔记
笔记·游戏·架构
幻雨様4 天前
UE5多人MOBA+GAS 19、创建升龙技能,以及带力的被动,为升龙技能添加冷却和消耗
游戏·ue5
charley.layabox4 天前
8月1日ChinaJoy酒会 | 游戏出海高端私享局 | 平台 × 发行 × 投资 × 研发精英畅饮畅聊
人工智能·游戏