Accepted
/in/foo.cc: In function 'll f(ll)': /in/foo.cc:22:1: warning: control reaches end of non-void function [-Wreturn-type] } ^
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,l=0,r=0x7fffffff,mid,temp;
ll f(ll n)
{
while(l!=r)
{
mid=(l+r)/2;
//cout<<"l="<<l<<" r="<<r<<" mid="<<mid;
temp=mid*mid;
//cout<<" temp="<<temp<<endl;
if(temp==n)
{
cout<<mid*2-1;
exit(0);
}
else if(temp>n)
r=mid;
else l=mid+1;
}
}
int main()
{
cin>>n;
f(n);
ll s=n-l*l;
if(s>0)
{
if(s>=l) cout<<2*l;
else cout<<2*l-1;
}
else if(s==0)
cout<<2*l-1;
else {
if(-s<=l) cout<<2*l-2;
else cout<<2*l-3;
}
return 0;
}