package sort;
public class Test43 {
public static void main(String[] args) {
System.out.println(findones(451));
//解析 https://blog.youkuaiyun.com/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;
}
}
本文介绍了一个Java程序,该程序通过一个名为findones的方法计算并返回在给定整数中一位数出现的总次数。这个算法遍历每一位数字,使用除法和取余操作来区分高位和低位,从而精确计算每一位数字为1的情况。

被折叠的 条评论
为什么被折叠?



