• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

OEIS A140358

以前也许做过?
有点方

最小整数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;
}
posted @ 2016-09-19 20:56  HaibaraAi  阅读(482)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3