您的位置:首页 > 其它

UVa 10025 - The ? 1 ? 2 ? ... ? n = k problem

2010-03-03 19:58 393 查看
/*
coder: ACboy
date: 2010-3-3
result: AC
description: UVa 10025 - The ? 1 ? 2 ? ... ? n = k problem
*/

#include <iostream>
using namespace std;

int main()
{
int k;
int n;
#ifndef ONLINE_JUDGE
freopen("10025.txt", "r", stdin);
#endif
cin >> n;
while (n--)
{
int temp;
int i;
cin >> k;
if (k < 0) k = -k;
for (i = 1; ;i++)
{
temp = (i + 1) * i / 2;
if (temp >= k) break;
}

if (temp == k || (temp - k) % 2 == 0) cout << i << endl;
else {
int ok = 0;
while (!ok) {
i++;
temp += i;
//cout << " i = " << i << " temp = " << temp << endl;
if ((temp - k) % 2 == 0) break;
}
cout << i << endl;
}
if (n > 0) cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  2010