Consider the natural numbers from 1 to N. By associating to each number a sign (+ or -) and calculating the value of this expression we obtain a sum S. The problem is to determine for a given sum S the minimum number N for which we can obtain S by associating signs for all numbers between 1 to N.
For a given S, find out the minimum value N in order to obtain S according to the conditions of the problem.
输入
The input consists N test cases.
The only line of every test cases contains a positive integer S (0< S <= 100000) which represents the sum to be obtained.
A zero terminate the input.
The number of test cases is less than 100000.
输出
The output will contain the minimum number N for which the sum S can be obtained.
样例输入
12
0
样例输出
2
7
来源
POJ
加一点思维,很容易想,但是我犯了个错误,就是这个值可以超过n
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<set> #include<map> #include<vector> #include<cmath> const int maxn=1e5+5; typedef long long ll; using namespace std; int main() { int n; while(scanf("%d",&n)!=EOF) { if(n==0) break; for(int t=1;t<=2*n;t++) { if(((1+t)*t/2)<n) { continue; } if((((1+t)*t/2))%2==1&&n%2==1) { printf("%d ",t); break; } if((((1+t)*t/2))%2==0&&n%2==0) { printf("%d ",t); break; } } } return 0; }