Advertisement
dmkozyrev

460.cpp

Aug 14th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. int count5(long long n) {
  4.     int answer = 0;
  5.     while (n > 0) {
  6.         answer += (n % 10 == 5);
  7.         n /= 10;
  8.     }
  9.     return answer;
  10. }
  11.  
  12. long long count5_before(long long n) {
  13.     if (n < 5) {
  14.         return 0;
  15.     } else if (n <= 9) {
  16.         return 1;
  17.     } else if (n % 10 == 9) {
  18.         return (n+1) / 10 + 10 * count5_before((n-9) / 10);
  19.     } else {
  20.         return count5(n) + count5_before(n-1);
  21.     }
  22. }
  23.  
  24. int main() {
  25.     long long n;
  26.     scanf("%lld", &n);
  27.     printf("%lld", count5_before(n));
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement