自己编写的适用于单循环比赛的贝格尔编排法代码,若有可改进之处,望高手指正import java.util.Scanner;public class BergerTest {public static void main(String[] args) {// TODO Auto-generated method stubBergerTest ber = new BergerTest();System.out.println(“输入队伍数量:”);Scanner cin = new Scanner(System.in);int team_Num = cin.nextInt();if((team_Num&0x1)!=0){team_Num++;}int[] team_arr = new int[team_Num];ber.init(team_arr, team_Num);int flag = team_Num-1;//最后一直队伍或者空白队伍的标志int walk = team_Num/2 -1;//移动步长for(int i= 0; i< team_Num-1; i++){System.out.println(“第”+(i+1)+”轮:”);if(i>0){flag = ber.exchangFlag(team_arr, team_Num, flag);ber.move(team_arr, team_Num, flag, walk);}ber.display(team_arr, team_Num);}}public void display(int[] arr,int len){for(int i = 0; i < len/2; i++){System.out.println(arr[i]+”….”+arr[len-1-i]);}}//初始化队伍数组public void init(int[] arr,int len){for(int i = 0;i < len ; i++){arr[i] = i+1;}} //return the pos of flag after swappublic int exchangFlag(int[] arr,int len, int flag){int lastflag = flag;if(flag != 0){flag = 0;}else{flag = len -1;}swap(arr,flag,lastflag);return flag;}public void move(int[] arr,int len, int flag,int walk){int begin= 1;int nextPos,beginPosValue;intnextPosValue = arr[begin];for(int j = 0; j<len-1;j++){beginPosValue = nextPosValue;nextPos = getNextPos(begin, walk, flag,len);nextPosValue = arr[nextPos];arr[nextPos] = beginPosValue;begin = nextPos;}}public void swap(int[] arr, int flag, int lastflag) {int temp = arr[flag];arr[flag] = arr[lastflag];arr[lastflag] = temp;}public int getNextPos(int begin,int walk,int flag,int len){for(int i = 0 ;i < walk; i++){begin++;if((begin%len)== flag)begin++;}return begin%len;}}
相关文章
用哪个弄湿
2026年6月24日少儿国寿福庆典版优缺点在哪?不足和亮点各参半
2026年6月24日狗狗吃卫生巾怎么办
2026年6月24日房贷二次扣款会影响征信吗?这是很有可能的
2026年6月24日身份证过期坐火车怎么办
2026年6月24日农行房贷还款日几点扣款?这些事项要注意
2026年6月24日
评论
站内搜索
热门文章
友情链接
- 科技博客
- 文本分割器
- 房贷计算器
- 存款利率计算器
- 反应速度测试
- 舒尔特方格
- mx记录查询
- 尺子在线测量
- 国旗图片大全
- 贷款计算器
- 日期计算器
- 孕周计算器
- 在线秒表
- Favicon Generator
- 超小厨美食
- Morse Code Translator
- national flag
- email tools
- market hours
- regex generator
- 科技博客
- 娱乐八卦
- free image compressor
- how to delete
- just delete me
- play piano
- 风君网络
- easy piano songs
- symbol
- online counter
- wiztree
- wiztree download
- online scoreboard
- 7zip download
- sharex download
- bandizip
- password remover
- bitumen calculator
- Asphalt Calculator
- random pokemon generator
- vlc播放器
- Electricity Bill Calculator
- Elven Name Generator
- Car logos
- stadium view
- voidimageviewer
- space bar clicker
- starbucks nutrition calculator
- cooking calculator
- Country Drawing
- World TV channels
- 短剧大全
- lagos life simulator
发表回复