Advertisement
STANAANDREY

balbait (19)

Dec 28th, 2021
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int int64_t
  4.  
  5. int getNrDigs(int x) {
  6.     int r = 0;
  7.     while (x) {
  8.         r++;
  9.         x /= 10;
  10.     }
  11.     return r;
  12. }
  13.  
  14. signed main() {
  15.     int n;
  16.     cin >> n;
  17.  
  18.     int nrDigs = getNrDigs(n);
  19.     int ans = 0;
  20.     ans += (nrDigs - 1) * 9;
  21.  
  22.     for (int ch = '1'; ch <= '9'; ch++) {
  23.         if (stoll(string(nrDigs, ch)) > n) {
  24.             ans += int(ch - '0') - 1;
  25.             break;
  26.         }
  27.     }
  28.  
  29.     cout << ans << endl;
  30.     return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement