3.16 PAT1050 乙级 python (三个测试点非零返回)

螺旋矩阵

import math
k = int(input())
lst = [int(i) for i in input().split()]
if k == 1:
	print(lst[0],end = '')
else:
	for i in range(int(math.sqrt(k)),1,-1):
		if k%i == 0:
			n = i
			break
	m = int(k/n)
	lst.sort(reverse = True)
	j = 0
	i1 = 0
	p = 0
	l = 0
	matrix = [[0 for i in range(n)] for i in range(m)]
	lst = list(map(str,lst))
	while(pl:
			matrix[i1][j] = lst[p]
			j -= 1
			p += 1
		while pl:
			matrix[i1][j] = lst[p]
			p += 1
			i1 -= 1
		i1 += 1
		j += 1
		l += 1
		m -= 1
		n -= 1
		if p == k-1:
			matrix[i1][j] = lst[p]
			p += 1
	for i in matrix:
		print(' '.join(i))

思路参考https://blog.csdn.net/qq_41398808/article/details/81675153

你可能感兴趣的:(PAT,乙级,python)