Skip to content

Instantly share code, notes, and snippets.

@ladyrick
Last active August 11, 2018 07:54
Show Gist options
  • Save ladyrick/55b275b4f162d91495ace3dda5a03546 to your computer and use it in GitHub Desktop.
Save ladyrick/55b275b4f162d91495ace3dda5a03546 to your computer and use it in GitHub Desktop.
求1到n的数字中,1的个数
int NumberOf1Between1AndN_Solution(int n)
{
int ones = 0;
for (long long m = 1; m <= n; m *= 10)
ones += (n / m + 8) / 10 * m + (n / m % 10 == 1) * (n % m + 1);
return ones;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment