#26283: C++


liu76214@gmail.com (Andrew liu)

學校 : 新竹市立建功高級中學
編號 : 92407
來源 : [111.243.122.123]
最後登入時間 :
2021-08-15 15:26:39
f467. 10025 - The ? 1 ? 2 ? ... ? n = k problem -- UVA10025 | From: [111.243.140.129] | 發表日期 : 2021-07-30 14:42

#include <iostream>

 

using std::cin;

using std::cout;

using std::endl;

 

int main() {

int round;

cin >> round;

while (round--) {

int k, ans = 1;

cin >> k;

if (k < 0) {

k *= (-1);

}

 

if (k == 0) {

cout << 3 << endl; // 0 = -1-2+3

}

 

else {

int maximun, n = 1, gap = 0;

 

for (int i = 1;; i++) {

maximun = (1 + i) * n / 2; // 等差為一的級數

if (maximun >= k) {

break;

}

n++;

}

 

gap = (maximun - k);

 

if (gap % 2 == 0) { // in the maximun

cout << n << endl;

}

else if (gap % 2 != 0) { // out of maximun

while (gap % 2 != 0) {

maximun += ++n;

gap = (maximun - k);

}

cout << n << endl;

}

}

cout << endl;

}

 

return 0;

}

 
ZeroJudge Forum