从1到n整数中1出现的次数

题目
输入一个整数n,求1~n这n个整数的十进制表示中1出现的次数。例如,输入12,1-12这些整数中包含1的数字有1、10、11和12,1一共出现了5次。

暴力法

解题思路
累加1~n中每个整数1出现的次数。我们可以每次通过对10求余数判断整数的个位数字是不是1。如果这个数字大于10,则除以10之后再判断个位数字是不是1,时间复杂度为O(nlogn)。
源代码

package Arithmetic;

public class NumberOf1Between1AndN {
    public static int NumberOf1Between1AndN(int n) {
        int number = 0;
        for (int i = 1; i <= n; i++) {
            number += NumberOf1(i);
        }
        return number;
    }

    //从个位到最高位依次比较每一位上的数字是否为0
    private static int NumberOf1(int n) {
        int nums = 0;
        while (n > 0) {
            if (n % 10 == 1)
                nums++;
            n /= 10;
        }
        return nums;
    }

    public static void main(String[] args) {
        System.out.println(NumberOf1Between1AndN(12));
    }
}

分析数字规律

解题思路
1.个位
从1到n,每增加1,weight就会加1,当weight加到9时,在加1又会回到0重新开始。那么weight从0-9的这种周期变化会出现多少次?这取决于n的高位是多少。
在这里插入图片描述
以534为例,在从1增长到n的过程中,534的个位从0-9变化了53次,记为round。每一轮变化中,1在个位出现一次,所以一共出现了53次。
在看weight的值。weight为4,大于0,说明第54论变化是从0-4,1又出现了1次。我们记1出现的次数为count,所以:count = round + 1 = 53 + 1 = 54

如果此时weight为0,说明第54轮到0就结束了,那么:count = round = 53
2.十位
对于十位来说,其0-9周期的出现次数与个位的统计方式是相同的。
在这里插入图片描述
不同点在于:从1到n,每增加10,十位的weight才会增加1,所以,一轮0-9周期内,1会出现10次。即rount10。
在看weight的值。当此时weight为3,大于1,说明第6轮出现了10次1,则:
count = round
10+10 = 5*10+10 = 60

如果此时weight的值等于0(n=504),说明第6轮到0就停止了,所以:
count = round10+10 = 510 = 50

如果此时weight的值等于1(n=514),那么第6轮中1出现了多少次呢?很明显,这与个位数的值有关,个位数为k,第6轮中1就出现了k+1次(0-k)。我们记个位数为former,则:
count = round10+former +1= 510+4 = 55
3.更高位
更高位的计算方式与十位一致。
总结
若weight为0,则1出现的次数为:roundbase
若weight为1,则1出现的次数为:round
base + fromer + 1
若weight大于1,则1出现的次数为:roundbase+base
例如:
534= (53
1+1)+ (510+10)+(0100+100)= 214
源代码

public class NumberOf1Between1AndN {
	public static int NumberOf1Between1AndN1(int n) {
        if (n < 0)
            return 0;
        int count = 0;
        int base = 1;
        int round = n;
        while (round > 0) {
            int weight = round % 10;//低位数字
            round /= 10;//去除低位后剩下的数字
            count += round * base;
            if (weight == 1) {
                count += (n % base) + 1;
            } else if (weight > 1) {
                count += base;
            }
            base *= 10;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(NumberOf1Between1AndN1(12));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值