题意介绍
这一天,TT 遇到了一个神秘人。
神秘人给了两个数字,分别表示 n 和 k,并要求 TT 给出 k 个奇偶性相同的正整数,使得其和等于 n。
例如 n = 10,k = 3,答案可以为 [4 2 4]。
题意分析
两种情况,假设前k-1个数都为1,看最后一个数是否为奇数;假设前k-1个数都为2,看最后一个数是否为偶数。
通过代码
#include<bits/stdc++.h>
using namespace std;
int t,n, k;
int main() {
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> k;
int t1 = n - (k - 1);
if (t1>0 && t1 % 2 != 0) {
cout << "YES" << endl;
for (int j = 0; j < k - 1; j++) cout << 1 << " ";
cout << t1 << endl;
}
else {
int t2 = n - (k - 1) * 2;
if (t2>0 && t2 % 2 == 0) {
cout << "YES" << endl;
for (int j = 0; j < k - 1; j++) cout << 2 << " ";
cout << t2 << endl;
}
else cout << "NO" << endl;
}
}
return 0;
}