京东神奇数
求教有啥问题,调试一直到20%过不去
bool isMagicNum(int num)
{
string numStr(to_string(num));
if (numStr.size() <= 1)
return false;
sort(numStr.begin(), numStr.end());
reverse(numStr.begin(), numStr.end());
int sum1 = numStr[0] - '0';
int sum2 = numStr[1] - '0';
for (int index = 2; index < numStr.size(); index++)
{
if (sum1 < sum2)
{
sum1 = sum1 + numStr[index] - '0';
}
else
{
sum2 = sum2 + numStr[index] - '0';
}
}
if (sum1 == sum2)
return true;
else
return false;
}
int main()
{
int left, right;
cin >> left;
cin >> right;
int count = 0;
for (int i = left; i <= right; i++)
{
if (isMagicNum(i))
count++;
}
cout << count;
return 0;
}