首页 > 其他 > 详细

Sum

时间:2019-05-01 12:34:34      阅读:204      评论:0      收藏:0      [点我收藏+]

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\n",t);
            break;
        }
        if((((1+t)*t/2))%2==0&&n%2==0)
        {
            printf("%d\n",t);
            break;
        }
    }
    }
    
    return 0;
}

 

Sum

原文:https://www.cnblogs.com/Staceyacm/p/10799503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!