当前位置:网站首页>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 ");
}
}
}
边栏推荐
猜你喜欢

Squid proxy server

VMware vSphere esxi 7.0 installation tutorial

VMware vSphere ESXi 7.0安装教程

Icml2022 | scalable depth Gaussian Markov random field

CEPH distributed storage

Codeforces Global Round 14

让马化腾失望了!Web3.0,毫无希望

After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"

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

KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
随机推荐
01-Golang-环境搭建
快速excel导出
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
Icml2022 | scalable depth Gaussian Markov random field
农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
Go从入门到实战——仅需任意任务完成(笔记)
Safe and efficient, non-contact "hand brushing" identification helps epidemic prevention and control
How to do a good job of gateway high availability protection in the big promotion scenario
Save method of JPA stepping pit series
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
Love math experiment | Issue 8 - building of Singapore house price prediction model
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
SQL必需掌握的100个重要知识点:组合 WHERE 子句
GFS分布式文件系统
Go from introduction to practice -- coordination mechanism (note)
让马化腾失望了!Web3.0,毫无希望
Acwing周赛57-数字操作-(思维+分解质因数)
Go从入门到实战——CSP并发机制(笔记)
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
A set of system to reduce 10 times the traffic pressure in crowded areas