题目内容: 整数中1出现的次数(从1到n整数中1出现的次数)
尝试一
结果:答案正确:恭喜!您提交的程序通过了所有的测试用例
代码:
public class Solution {
public int NumberOf1Between1AndN_Solution(int n) {
/* 特殊情况处理*/
if(n<=0){
return 0;
}
if(n==1){
return 1;
}
//用于计数
int count=1;
for(int i=2;i<=n;++i){
//余数
int y=0;
//商
int s=i;
while(s!=0){
/*求余数*/
y=s%10;
if(y==1){
count++;
}
s=s/10;
}
}
return count;
}
}
分析:for循环里面套while循环