题目:http://10.105.242.80/problem/p/130/
找出x的范围:sqrt(n)-s(x)<x<sqrt(n)
而s(x)<=9*9=81,所以 sqrt(n)-81<x<sqrt(n)
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll s(ll x){
ll result=0;
while(x!=0){
result+=x%10;
x=x/10;
}
return result;
}
int main(){
ll n;
scanf("%lld",&n);
bool ok=false;
ll ghn=(ll)sqrt(n);
for(ll i=81;i>=0;i--){
ll x=ghn-i;
if(x>0&&x*x+s(x)*x==n){
printf("%lld\n",x);
ok=true;
break;
}
}
if(!ok) printf("-1\n");
return 0;
}