本文最后更新于:3 years ago
题目链接:点击跳转
题意: 商店有n排货架,每排有m个位置能装货物,货物的价格从1到n*m(每个价格一个),店里有一个机器人会从货架每一行的头开始取价格,每取一个价格加上后会计算平均价格,但是这个机器人很特殊,他遇到带小数的数字时会故障,问有没有排列能使机器人不会出现故障
思路: 构建等差数列,根据行数的奇偶性来排列数字(如第一行:1 3 5 7 9, 1 3含两个1 和一个2,那么是2的倍数,1 3 5,含3个1和3个2,那么是3的倍数,以此类推),那么怎么判断不成立的情况呢,可以计算我们构建数列的最大值是否超出了价格区间,超过了就是NO
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include<bits/stdc++.h>
using namespace std; typedef long long ll;
signed main() { ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); int _; cin >> _; while (_--) { int n, m; cin >> n >> m; int maxn = ((n + 1) / 2 * m - 1) * 2 + 1; if (n % 2 == 0) maxn++; if (maxn > n * m) { cout << "NO" << endl; continue; } cout << "YES" << endl; int odd = 1, even = 2; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (j != 1) cout << ' '; if (i & 1) { cout << odd; odd += 2; } else { cout << even; even += 2; } } cout << endl; } } return 0; }
|