Last active
August 11, 2018 07:54
-
-
Save ladyrick/55b275b4f162d91495ace3dda5a03546 to your computer and use it in GitHub Desktop.
求1到n的数字中,1的个数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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