OEIS A140358

时间:2023-02-08 20:44:21

以前也许做过?

有点方

最小整数1到k 加减得到 n 1+-2+-3+-...+-k = n 求最小k


#include <cstdio>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iostream>
#include <cstdio>
using namespace std;
#define N 105
int main() {
int n;
while (cin >> n) {
int s = 0;
int x = 0;
if (n < 0)
n = -n;
for (int i = 0; i*i+i <= 2*n; i++)
{
s += i;
if (n>=s&&n<(s+i+1))
{
x = i;
break;
}
}
int y = x;
s = (y*y + y) / 2;
if (s != n) {
int d = n - s;
if (y % 2 == 1 && d % 2 == 1) {
x = y + 2;
}
else if (y % 2 == 1 && d % 2 == 0) {
x = y + 1;
}
else if (y % 2 == 0 && d % 2 == 1) {
x = y + 1;
}
else if (y % 2 == 0 && d % 2 == 0) {
x = y + 3;
}
}
printf("%d\n", x);
}
return 0;
}