当前位置:网站首页>专题教程——选队长游戏
专题教程——选队长游戏
2022-06-27 19:20:00 【continueLR】
今天同学们相约一起爬山游玩,为了更好的进行这场活动,大家准备推举 一个人作为出游的临时队长。为了体现合理公平,大家提出了一个比较有趣的 规则。所有人围成一圈,顺序排号。从第一个人开始报数(从 1 到 3 报数), 凡报到 3 的人退出圈子,剩下的人继续报数,最后留下的当选为队长。 请你通过编写程序,求出一组人中的队长是原来第几位同学。
思路:考察基础语法知识和数组的灵活运用;
将团队看做一个数组,一步一步分析
踩坑原因:1.count++,是先赋值后运算,和++count混淆;
2.index赋值问题,当count变为0以后,index一样计数,所以index的个数一直不变,不需要--
public class Pashan {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
//首先定义一个数组,登山队人数就是数组长度
//考虑三个变量。
//1.数组长度leftNumber,每淘汰一名队员,leftNumber-1。
//2.计数:index先给所有队员一个称号1,用于计数,每计一轮从新开始。
//3.报数:count,每报一位自加1,当报到3时淘汰,赋值为零。
int[]persons = new int[n];
int leftNumber = persons.length;
int index = 0;//计数
int count = 0;//报数
for (int i= 0;i<persons.length;i++){
persons[i]=1;//先给所有人赋值称号1
}
while (leftNumber>1){//人数大于1游戏才能进行,知道淘汰至一人,结束游戏
if (persons[index]==1){
count++;//报数
}
if (count==3){//报到3退出游戏
count=0;//退出游戏的赋值为0,下轮就不报数了
persons[index]=0;//同理,下标也赋值为0
leftNumber--;//数组长度减1
}
index++;//是先赋值后运算,不要和++count混淆;
if (index==n){//一轮计数完毕,
index=0;//重新计数
//当count变为0不报数了,但index一样计数。
//所以index的个数是一直不变的,不需要--
}
}
for (int i= 0;i<persons.length;i++){
//此处不要混淆,i是下标的值,i+1才是计数的值
System.out.println("队长是原来的第"+(i+1)+"个队员");
}
}
}
边栏推荐
- mime. Type file content
- Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
- 教程|fNIRS数据处理工具包Homer2下载与安装
- Save method of JPA stepping pit series
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- 308. 2D area and retrieval - variable segment tree / hash
- Is it safe to open an account and buy stocks? Who knows
- 安全高效,非接触“刷手”身份识别助力疫情防控
- Necessary software tools in embedded software development
- Share an experience of self positioning + problem solving
猜你喜欢

GFS分布式文件系统

分享|智慧环保-生态文明信息化解决方案(附PDF)

抖音的兴趣电商已经碰到流量天花板?

Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials

KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架

Educational Codeforces Round 108 (Rated for Div. 2)

Codeforces Round #717 (Div. 2)

VMware vSphere esxi 7.0 installation tutorial

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China
随机推荐
Codeforces Round #719 (Div. 3)
# Leetcode 821. Minimum distance of characters (simple)
Graduation design of police report convenience service platform based on wechat applet
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
100 important knowledge points that SQL must master: creating calculation fields
关于企业数字化的展望(38/100)
SQL Server for循环用法
Very comprehensive dolphin scheduler installation and use documents
Model reasoning acceleration based on tensorrt
MySQL usage notes 1
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
Shell command used in actual work - sed
Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
非常全面的DolphinScheduler(海豚调度)安装使用文档
CEPH distributed storage
White whoring red team goby & POC, how do you call white whoring?
squid代理服务器
Industry case | see the operation of bank digital transformation from the king of retail
Here are 12 commonly used function formulas for you. All used ones are good
Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials