zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Docker搭建redis集群
    PHP中的OPCode和OPCache
    Redis的三种集群模式
    MySQL事务的隔离级别
    Docker镜像分层技术
    为什么 MongoDB 选择B树,Mysql 选择B+树?
    MongoDB的使用
    cesium+vue挖坑展示
    Ceium+Vue踩坑记录
    渲染总结——记录
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10799503.html
Copyright © 2011-2022 走看看