codeforces C. Minimum Ties (c++)

C. Minimum Ties

A big football championship will occur soon! teams will compete in it, and each pair of teams will play exactly one game against each other.

There are two possible outcomes of a game:

the game may result in a tie, then both teams get point;
one team might win in a game, then the winning team gets points and the losing team gets points.
The score of a team is the number of points it gained during all games that it played.

You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well.

Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible.

Input
The first line contains one integer () — the number of test cases.

Then the test cases follow. Each test case is described by one line containing one integer () — the number of teams.

Output
For each test case, print integers describing the results of the games in the following order: the first integer should correspond to the match between team and team , the second — between team and team , then and , …, and , and , and , …, and , and so on, until the game between the team and the team .

The integer corresponding to the game between the team and the team should be if wins, if wins, or if the game results in a tie.

All teams should get the same score, and the number of ties should be the minimum possible. If there are multiple optimal answers, print any of them. It can be shown that there always exists a way to make all teams have the same score.

题目大意:

n个球队,每个球队打一场,胜者加三分,输者不加分,如果平局两边各加一分.
问:最少有几次平局,可以使所有球队的最终总得分相同.

输入样例

2
2
3

输出样例

0 
1 -1 1 

解题思路

分为偶数和奇数两种情况
用数组表示
例如

cx[a][b]
则表示a球队与b球队比赛a队胜利

如果是偶数形式,那么用图形表示球队,对角线的球队平局
如果是奇数,那么所有和自己打的球队全部胜利

AC代码

#include <iostream>
using namespace std;
const int N=101;
int cx[N][N];
int main() {
    
    
	int t;
	cin>>t;
	while(t--) {
    
    
		int n;
		cin>>n;
		for(int i=1; i<=n; i++)
			for(int j=1; j<=n; j++)
				cx[i][j]=-1;//默认全部都输了
		if(n%2) {
    
     //如果是奇数
			for(int i=1; i<=n/2; i++)
				for(int j=1; j<=n; j++) {
    
    
					cx[j][(j+i-1)%n+1]=1;
				}
		} else {
    
    
			for(int i=1; i<=n/2; i++)
				for(int j=1; j<=n; j++) {
    
    
					if(i==n/2)
						cx[j][(i+j-1)%n+1]=0;
					else
						cx[j][(i+j-1)%n+1]=1;
				}
		}
		for(int i=1; i<n; i++)
			for(int j=i+1; j<=n; j++)
				cout<<cx[i][j]<<" ";
		puts(" ");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_34832548/article/details/113852013