问题描述
设有有2 n(n<=6)个球队进行单循环比赛,计划在2 n – 1天内完成,
每个队每天进行一场比赛。设计一个比赛的安排,使在2 n – 1天内每个队都与不同的对手比赛。
输入格式
输入文件matchplan.in共一行,输入n的数值。
输出格式
输出文件matchplan.out共(2 n – 1)行,第i行输出第i天的比赛安排。
格式为: A-B,C-D,……。其中i是天数,A,B分别为比赛双方的编号,每行共2 n-1个比赛场次。
样例输入
2
样例输出
<1>1-2,3-4
<2>1-3,2-4
<3>1-4,2-3
#include<stdio.h>
int b[30][30]={0};//标记曾经匹配过
int dq[30]={0};//当天匹配标记
int a[30];//被匹配者 (小组队伍红方)
int a1[30];//匹配者 (小组队伍蓝方)
int tw; //比赛小组队伍 (10支队伍 5队小组队伍)
int T=0;//判断当前天数比赛 第一顺序方案是否已制定
void f(int pos )
{int i,j,bt;//当前被匹配者 用于清除标记
if(tw<pos)//pos 代表当前选第几组的比赛
{
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"<%d>"</span><span class="token punctuation">,</span>a1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>T<span class="token operator">=</span>a1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//当前天数第一顺序计划已定 </span>
<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator"><=</span>tw<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d-%d "</span><span class="token punctuation">,</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>a1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//输出匹配小组队伍 </span>
b<span class="token punctuation">[</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>a1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> b<span class="token punctuation">[</span>a1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//相互标记为曾经匹配 </span>
<span class="token punctuation">}</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{</span> <span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator"><=</span><span class="token number">2</span><span class="token operator">*</span>tw<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>dq<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>a<span class="token punctuation">[</span>pos<span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>dq<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>bt<span class="token operator">=</span>i<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//按顺序拿出被匹配着; </span>
<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator"><=</span><span class="token number">2</span><span class="token operator">*</span>tw<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//匹配者循环 </span>
<span class="token keyword">if</span><span class="token punctuation">(</span> b<span class="token punctuation">[</span>a<span class="token punctuation">[</span>pos<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token operator">&&</span>dq<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//判断曾经是否匹配过并且当前是否被匹配 </span>
<span class="token punctuation">{</span>a1<span class="token punctuation">[</span>pos<span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>dq<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token function">f</span><span class="token punctuation">(</span>pos<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//试执行下一组比赛; </span>
dq<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//清除当前匹配者</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>T<span class="token operator">==</span>a1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>i<span class="token operator">=</span><span class="token number">2</span><span class="token operator">*</span>tw<span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//若当天第一顺序方案已确定则无需重复设计方案 </span>
<span class="token punctuation">}</span>
dq<span class="token punctuation">[</span>bt<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//清除当前被匹配者 </span>
<span class="token punctuation">}</span>
}
int main()
{
int n,i,j;
scanf("%d",&n);
tw=n;//小组队伍组数
n=n*2-1;
for(i=1;i<=n;i++)//循环天数;
f(1);//从第一组队伍开始
return 0;
}