爱奇艺编程题:最后一位

题目:https://www.nowcoder.com/practice/fae8632cfc64433989720bc01e09f382?tpId=90&tqId=30806&rp=2&ru=/ta/2018test&qru=/ta/2018test/question-ranking

#include

using namespace std;

bool IsOk(uint64_t x,uint64_t sum){

uint64_t tmp = 0;

while(x){

tmp += x;

x /= 10;

}

return tmp == sum;

}

uint64_t func(uint64_t sum){

int digits = 0;

uint64_t tmp = sum;

while(tmp){

++digits;

tmp /= 10;

}

string molecule = "1" + string(digits - 1,'0');

string denominator(digits,'1');

uint64_t ret = (stoull(molecule) * 10  / stoull(denominator)) * sum / 10;

for(uint64_t i = 1;i <= 10;++i){

if(IsOk(ret + i,sum))

return ret + i;

if(IsOk(ret - i,sum))

return ret - i;

}

uint64_t special = 101002030405;

return special;

}

int main()

{

std::ios::sync_with_stdio(false);

std::cin.tie(0);

uint64_t n;

cin >> n;

uint64_t special = 101002030405;

if(func(n) != special)

cout<

else

cout<<-1<

return 0;

}

你可能感兴趣的:(爱奇艺编程题:最后一位)