51NOD 1615

来源:互联网 发布:36o软件管家下载 编辑:程序博客网 时间:2024/06/04 18:07

还是暴力 从1开始一直加 要是比n大了 做差看是不是偶数 偶数的话就直接输出 不然就继续加 证明的话 如果差△s是偶数的话 就在之前的+(△s/2)变成-(△s/2) 很好想

#include<bits/stdc++.h>#define ll long long/*char *TT,*mo,but[(1<<15)+2];#define getchar() ((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/using namespace std;const int N=100000+5;int n,p;inline int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int main(){    n=abs(read());    if(n==0)    {        cout<<0<<endl;return 0;    }    for(int i=1;i<=1e5;i++)    {        p+=i;        if(p<n) continue;        else if(p==n)        {            cout<<i<<endl;return 0;        }        else if((p-n)%2==0)        {            cout<<i<<endl;return 0;        }    }}
原创粉丝点击