解答
1 这道题目书上的分析很好,碰到此类不能一眼看出的内容,从小到大研究规律。
2 代码
public static int CountOne(int number){
int count =0;
int Factor =1;
int lowerNum = 0;
int curNum =0;
int higherNum =0;
while(number/Factor != 0){
lowerNum = number -(number/Factor)*Factor;
curNum = (number/Factor)%10;
higherNum = number/(Factor*10);
switch(curNum){
case 0:
count += higherNum * Factor;
break;
case 1:
count += higherNum * Factor+lowerNum +1;
break;
case 2:
count += (higherNum+1)* Factor;
break;
}
Factor*=10;
}
return count;
}