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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值