43 1-n整数中出现1 的次数

package sort;

public class Test43 {
    public static void main(String[] args) {
         System.out.println(findones(451));
         //解析  https://blog.csdn.net/ds19980228/article/details/82748063
    }
    public static int findones(int n) {
        int count = 0;
        int high = n;
        int low =0;
        int base = 1;
        while (high != 0) {
            int temp=high;
            high=temp/10;
            low =temp%10;

            count += high * base;
            if (low == 1)
                count += n % base + 1;
            if (low > 1)
                count += base;
                base*=10;
        }

        return count;

    }

}
 

发布了41 篇原创文章 · 获赞 1 · 访问量 747

猜你喜欢

转载自blog.csdn.net/coder_my_lover/article/details/105308434