鱼C论坛

 找回密码
 立即注册
查看: 3747|回复: 1

怎么用java编程积木游戏的题目?

[复制链接]
发表于 2018-12-27 10:51:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
幼儿园老师带领n个小朋友围成一圈做游戏,首先发给每个小朋友任意数量不等的积木,再给得到单数积木的小朋友1个积木凑成双数,然后开始一轮游戏,每个小朋友把自己的积木分一半给坐在左边的小朋友.往下给单数的小朋友加1个凑成双数后做下一轮,就这样一轮一轮做直到所有小朋友的积木数量都相同.编程模拟此过程,输出每一轮各个小朋友积木的数量.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-2 11:43:30 | 显示全部楼层
惨不忍睹的代码:
  1. package com.justsy.army.mgt.bwlist.controller;

  2. import java.util.Scanner;

  3. import org.apache.commons.lang.ArrayUtils;

  4. public class test {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         System.out.print("输入若干个数字(逗号分隔):");
  8.         String nextLine = scanner.nextLine();
  9.         String[] split = nextLine.split(",");
  10.         int[] students = convert2IntArray(split);
  11.         
  12.         //消除奇数
  13.         for( int i = 0;i<students.length;i++ ) {
  14.             if( students[i] % 2 != 0 ) {
  15.                 students[i] += 1;
  16.             }
  17.         }
  18.         //开始每轮游戏
  19.         while(!isEveryoneEqual(students)) {
  20.             //给左边
  21.             int giveNext = students[students.length-1] / 2;
  22.             for( int i = 0;i<students.length;i++ ) {
  23.                 int toAdd = giveNext;
  24.                 giveNext = students[i] / 2;
  25.                 students[i] = giveNext + toAdd;
  26.             }
  27.             //消除奇数
  28.             for( int i = 0;i<students.length;i++ ) {
  29.                 if( students[i] % 2 != 0 ) {
  30.                     students[i] += 1;
  31.                 }
  32.             }
  33.             //每轮输出
  34.             System.out.println(ArrayUtils.toString(students));
  35.         }
  36.         
  37.         System.out.printf("每个人都相等了:%d",students[0]);
  38.     }
  39.    
  40.     public static boolean isEveryoneEqual(int[] students) {
  41.         int temp = students[0];
  42.         for( int i = 1;i<students.length;i++ ) {
  43.             if( students[i] != temp )return false;
  44.         }
  45.         return true;
  46.         
  47.     }
  48.    
  49.     public static int[] convert2IntArray(String[] split) {
  50.         int[] ret = new int[split.length];
  51.         for(int i = 0;i<split.length;i++) {
  52.             ret[i] = Integer.parseInt(split[i]);
  53.         }
  54.         return ret;
  55.     }
  56.    
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 08:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表