题目链接:点击打开链接
给出n, k构造一个n * n的矩阵, 元素从1到n ^ 2, 使得第k列和尽量大, 每行递增.
找规律, 发现前k - 1列按行递增, 同时后面的列按行递增, 即可构造矩阵成功.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
#define X first
#define Y second
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 505;
int n, k, num, ans[MAXN][MAXN], sum;
int main(int argc, char const *argv[])
{
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= k - 1; ++j)
ans[i][j] = ++num;
for(int i = 1; i <= n; ++i)
for(int j = k; j <= n; ++j)
ans[i][j] = ++num;
for(int i = 1; i <= n; ++i)
sum += ans[i][k];
printf("%d\n", sum);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j)
printf("%d ", ans[i][j]);
printf("\n");
}
return 0;
}