题解 | #序列和#
序列和
https://www.nowcoder.com/practice/46eb436eb6564a62b9f972160e1699c9
#include <iostream> using namespace std; int main() { int N, L; cin >> N >> L; bool is_find = false; for (int i = L; i <= 100; i++) { //当N的大小小于从0开始的连续L个数字之和时,必不可能成功 if (N < (i - 1)*i / 2) break; // 对当前连续数字是奇数个还是偶数个进行区分 if (i % 2 == 0) { // 偶数逻辑 // 如果当前无法均分,则继续 if (N % (i / 2) != 0 || (N / (i / 2)) % 2 == 0) continue; int center = N / (i / 2); int left = center / 2, right = center / 2 + 1; if (left - i / 2 + 1 < 0) continue; for (int j = left - i / 2 + 1; j < right + i / 2; j++) { cout << j << " "; } cout << endl; is_find = true; break; } else { if (N % i != 0) continue; int center = N / i; if (center - i / 2 < 0) continue; for (int j = center - i / 2; j <= center + i / 2; j++) { cout << j << " "; } cout << endl; is_find = true; break; } } if (!is_find) cout << "No" << endl; return 0; }