当前位置:网站首页>Special tutorial - Captain selection game
Special tutorial - Captain selection game
2022-06-27 21:39:00 【continueLR】
Today, the students made an appointment to climb the mountain for fun , In order to better carry out this activity , We are going to recommend A man as a temporary captain of the trip . In order to reflect rationality and fairness , We put forward an interesting The rules . Everyone in a circle , Sequence number . Count from the first person ( from 1 To 3 Number off ), Where to report 3 Of the people out of the circle , The rest continued to count , The last one left was elected captain . Please write a program , Find out how many students the team leader of a group is .
Ideas : Investigate the basic grammar knowledge and the flexible use of arrays ;
Think of the team as an array , Step by step analysis
Reasons for stepping on the pit :1.count++, It's assignment before operation , and ++count confusion ;
2.index Assignment problem , When count Turn into 0 in the future ,index Same count , therefore index The number of has remained the same , Unwanted --
public class Pashan {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
// First define an array , The number of climbers is the length of the array
// Consider three variables .
//1. The length of the array leftNumber, For every player eliminated ,leftNumber-1.
//2. Count :index First give all the team members a title 1, Used to count , Every round starts anew .
//3. Number off :count, Add one digit for each quotation 1, When you check in 3 Time out , Assign to zero .
int[]persons = new int[n];
int leftNumber = persons.length;
int index = 0;// Count
int count = 0;// Number off
for (int i= 0;i<persons.length;i++){
persons[i]=1;// First assign a title to everyone 1
}
while (leftNumber>1){// There are more people than 1 The game can be played , Know that one person is eliminated , End the game
if (persons[index]==1){
count++;// Number off
}
if (count==3){// To report for duty 3 Quit the game
count=0;// The assignment for exiting the game is 0, The next round will not count off
persons[index]=0;// Empathy , The subscript is also assigned to 0
leftNumber--;// Array length minus 1
}
index++;// It's assignment before operation , Do not mix ++count confusion ;
if (index==n){// A round of counting is over ,
index=0;// Recount
// When count Turn into 0 No counting , but index Same count .
// therefore index The number of is always the same , Unwanted --
}
}
for (int i= 0;i<persons.length;i++){
// Don't confuse here ,i Is the value of the subscript ,i+1 Is the value of the count
System.out.println(" The captain is the original No "+(i+1)+" A team member ");
}
}
}
边栏推荐
- A set of system to reduce 10 times the traffic pressure in crowded areas
- SQL必需掌握的100个重要知识点:用通配符进行过滤
- GFS distributed file system
- Go从入门到实战——共享内存并发机制(笔记)
- ARCS模型介绍
- Go从入门到实战——Context与任务取消(笔记)
- The difference between scrum and Kanban
- 动态刷新mapper看过来
- Go from introduction to actual combat -- channel closing and broadcasting (notes)
- mysql使用笔记一
猜你喜欢

Go從入門到實戰——接口(筆記)

SQL Server for循环用法

Oracle的CTAS能不能将约束等属性带到新表?

划重点!国产电脑上安装字体小技巧

Use the storcli tool to configure raid. Just collect this article

Go from entry to practice -- CSP concurrency mechanism (note)

ICML2022 | 可扩展深度高斯马尔可夫随机场

100 important knowledge points that SQL must master: using functions to process data

VMware vSphere ESXi 7.0安装教程

Icml2022 | scalable depth Gaussian Markov random field
随机推荐
Go from introduction to practice - polymorphism (note)
Is it safe to open an account and buy stocks? Who knows
鲜为人知的mysql导入数据
Full record of 2022 open source moment at Huawei partners and Developers Conference
TreeSet详解
Go从入门到实战——共享内存并发机制(笔记)
Love math experiment | Issue 8 - building of Singapore house price prediction model
SQL Server for循环用法
通过CE修改器修改大型网络游戏
系统自带的karsonzhang/fastadmin-addons报错
银河麒麟系统局域网文件共享教程
Safe and efficient, non-contact "hand brushing" identification helps epidemic prevention and control
BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
mysql使用笔记一
白嫖红队goby&POC,叫你如何白嫖?
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
linux下安装oracle11g 静默安装教程
微服务之远程调用
GFS distributed file system
划重点!国产电脑上安装字体小技巧