题解 | #整数中1出现的次数(从1到n整数中1出现的次数)#
整数中1出现的次数(从1到n整数中1出现的次数)
http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
public:
int NumberOf1Between1AndN_Solution(int n) {
int i;
int sum=0;
for(i=1;i<=n;i+=1){
sum+=judge(i);
}
return sum;
}
int judge(int n){
int result;
int count=0;
while(n!=0){
result=n%10;
if(result==1){
count+=1;
}
n/=10;
}
return count;
}
};