【 声明:版权所有,转载请标明出处,请勿用于商业用途。 联系信箱:libin493073668@sina.com】
题目描述
编程之美上给出的规律:
class Solution { public: int NumberOf1Between1AndN_Solution(int n) { int count =0; int i =1; int current =0,after =0,before =0; while((n / i) !=0) { current = (n / i) %10; before = n / (i *10); after = n - (n / i) * i; if (current >1) count = count + (before +1) * i; else if (current ==0) count = count + before * i; else if(current ==1) count = count + before * i + after +1; i = i *10; } return count; } };
版权声明:本文为博主原创文章,如果转载,请注明出处
《剑指offer》整数中1出现的次数(从1到n整数中1出现的次数)
原文:http://blog.csdn.net/libin1105/article/details/48391399