牛客多校赛四

本文探讨了一个特殊的矩阵填充问题,目标是在nxn的矩阵中填入-1、0或1,使得每一行和每一列的和都是不同的值。文章提供了一种算法实现思路,并通过示例展示了如何解决这一问题。

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

 

 

 

 4. Chiaki has an n x n matrix. She would like to fill each entry by -1, 0 or 1 such that r1,r2,...,rn,c1,c2, ..., cn are distinct values, where ri be the sum of the i-th row and c i be the sum of the i-th column. 输入描述: There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 200), indicating the number of test cases. For each test case: The first line contains an integer n (1 ≤ n ≤ 200) -- the dimension of the matrix. 输出描述: For each test case, if no such matrix exists, output ``impossible'' in a single line. Otherwise, output ``possible'' in the first line. And each of the next n lines contains n integers, denoting the solution matrix. If there are multiple solutions, output any of them.

示例1: 输入 2 1 2

输出 impossible possible

1 0

1 -1

 

 

ss

 

#include<stdio.h>
int x[205][205]; 
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int m;
		scanf("%d",&m);
		if(m%2==1)
		printf("impossible\n");
		else
		{
			printf("possible\n");
			for(int i=1;i<=m;i++)
			{       x[i][i]=1;
					for(int j=1;j<i;j++)
					{
						if(j%2==1)
						{
							x[i][j]=1;
							x[j][i]=1;
						}
						else
						{
							x[i][j]=-1;
							x[j][i]=-1;	
						}	
					}
				if(i%2==1)
				{
					x[i][i+1]=0;		
				}
				else
				{
					x[i][i]=-1;
					x[i-1][i]=0;
				}	
			}
		for(int i=1;i<=m;i++)
		{
			int j;
			for(j=1;j<m;j++)
			{
				printf("%d ",x[i][j]);
			}
			printf("%d\n",x[i][j]);
		}
		}

	}
 } 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值