java实现的活动队伍单循环编排

来源:互联网 发布:java base64加密文件 编辑:程序博客网 时间:2024/05/05 23:01

import java.util.Scanner;
public class Test1 {public static void main(String[] args){    int n,m;    Scanner cin = new Scanner(System.in);    System.out.print("输入队伍的数量: ");    n= cin.nextInt();if(n%2==0) m=n;else m=n+1;int a=1,b=1,index=1,loop=0;for(int i=1; i<=(m-1)*(m/2); i++){if(a>=m) a=1;if(index>m/2) index=1;if(index==1){loop++;if(i==1){b=m;}else{b=a;}System.out.println("第"+loop+"轮");;if(((i-1)/(m/2))%2==0){System.out.println(a+"--"+m);}else{System.out.println(m+"--"+a);}}else if(index>1 && index<=m/2){if(b>1) b--;else b=m-1;System.out.println(a+"--"+b);}index++;a++;}  }}


以上为第一种实现方式。


第二种实现方式:

import java.util.Scanner;public class test {  public static void main(String args[]){int team_Num;//队伍的数量int team_Arr[];//队伍数组int team_temp[];boolean empty=false;//是否有轮空int jump;//调动幅度int round;//比赛轮数int flag;//标志,队伍的最大的,或者0,其他队伍在移动的时候,如果碰到他,将跳过int tempNum,tempNum1;//队伍在迭代时候保存临时变量的东西//--------------------初始化一些数据        Scanner cin = new Scanner(System.in);        System.out.print("输入队伍的数量: ");                        team_Num = cin.nextInt();if(team_Num%2 != 0)//队伍个数为奇数时{empty = true;team_Num++;}round = team_Num-1;jump = ((team_Num+1)/2)-1;team_Arr = new int[team_Num];team_temp = new int[team_Num];for(int i = 0;i< team_Num;i++){  team_Arr[i] = i+1;}if(empty){  team_Arr[team_Num-1]=0;} flag = team_Num-1;//---------------------开始计算了--------------for(int j = 0;j< round;j++){System.out.println("第"+(j+1)+"轮:");for(int m = 0;m< team_Num/2;m++){System.out.println(team_Arr[m]+"----"+team_Arr[team_Num-m-1]);}for(int g = 0;g< team_Num;g++){    team_temp[g] = team_Arr[g];}if(flag != 0 ){tempNum = team_Arr[flag];//temp 一开始总是记录0队或者最大队伍flag = 0;//flag 跳动tempNum1 = team_Arr[flag];team_Arr[flag] = tempNum;}else{    tempNum =team_Arr[flag];//temp 一开始总是记录0队或者最大队伍tempNum1 = team_Arr[team_Num-1];flag = team_Num-1;//flag 跳动team_Arr[flag]=team_temp[flag] = tempNum;team_Arr[0]=team_temp[0] = tempNum1;}for(int k = 0;k< team_Num-1;k++)//走动{int t = k;if(t >= team_Num)t = t - team_Num;int z = t;for(int u = 0;u< jump;u++){t++;if(t == team_Num)t = t - team_Num;if(t == flag)t++;if(t == team_Num)t = t-team_Num;}team_Arr[t] = team_temp[z];//}    }   }  }




0 0
原创粉丝点击