• Sum


    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;
    }
  • 相关阅读:
    文件I/O(二)
    linux学习之文件I/O篇(一)
    静态库和共享库
    vim-ide
    CentOS6 vsftpd 安装及优化方法
    Redmine2.5+CentOS6+Apache2
    分享一个TP5实现Create()方法的心得
    Windows证书的生成导出以及使用证书验证文件是否被修改
    如何设置程序UAC控制
    关于C#的可变长参数
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10799503.html
Copyright © 2020-2023  润新知