1 solutions

  • 0
    @ 2022-10-16 20:46:09
    import java.util.Scanner;
    
    public class Main {
    
        static long solve(long n) {
            if (n <= 9) return n;
            char[] s = String.valueOf(n).toCharArray();
            long res = 0L;
            int len = s.length;
            for (int i = len - 1; i > 0; i--) {
                res += 9;
                if (s[i] < '9') {
                    s[i - 1] = (char) (s[i - 1] - 1);
                }
            }
            res += s[0] - '0';
            return res;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            long n = in.nextLong();
            System.out.println(solve(n));
        }
    
    }
    
    
    
    • 1

    Information

    ID
    20939
    Time
    2000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    (None)
    # Submissions
    314
    Accepted
    81
    Uploaded By