first,单循环比赛安排算法

这是一个使用C++编写的单循环比赛安排算法。它接受队伍数量作为输入,然后生成并输出比赛的完整赛程。算法通过一个循环来确定每一轮的比赛对阵,并确保每个队伍在比赛过程中与其他所有队伍各比赛一次。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include  < iostream >

using   namespace  std;

void  fun(  int  m,  int *  arr )
{
    
int a = 1,b = 1,index = 1,loop = 0;
    
forint i = 0; i <= (m-1)*(m/2); i++ )
    
{
        
if( a >= m ) a = 1;
        
if( index > m /2 ) index = 1;
        
if( index == 1 )
        
{
            loop
++;
            
if( loop > m-1 ) break;
            
if( i == 1 ) b = m;
            
else b = a;
            
if( ( i-1 ) / ( m/2 ) % 2 == 0 ) arr[ 0 ] = a,arr[ 1 ] = m, arr+=2;
            
else arr[ 0 ] = m,arr[ 1 ] = a,arr+=2;
        }

        
else if( index > 1 && index <= m/2 )
        
{
            
if( b > 1 ) b--;
            
else b = m-1;
            arr[ 
0 ] = a,arr[ 1 ] = b,arr+=2;
        }

        index
++,a++;
    }

}



int  main()
{
    cout 
<< "队伍数量:";
    
    
int n = 0;
    
int m = 0;
    cin 
>> n;

    
    
if( n % 2 == 0 ) m = n;
    
else m = n+1;

    
int* arr = (int*)malloc( m * m * sizeofint ) );

    fun( m, arr );

    
forint i = 0; i < m*(m-1); i++ )
    
{
        
if( i % m == 0 ) cout << "" << (i / m)+1 << "" << endl;
        
if( i % 2 == 0 ) cout << arr[ i ];
        
else cout << " vs " << arr[ i ] << endl;
    }


    system(
"pause");

    
return 0;
}
 first,没啥理由,正好在敲这段代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值