整数中出现1的个数
class Solution {
public:
int NumberOf1Between1AndN_Solution(int n)
{
int dev = 10; //除数
int multi = 1;
int res = 0;
int low;
int cur = n%dev; //最初为个位数
int high = n / dev; //high从十位开始
int flag = false;
while(n / multi) //当高位数存在时 { if(cur > 1) res = res+ (high+1) *multi; if(cur == 1) { if(flag) res =res + high * multi + low+1; else res =1; } if(cur == 0) { res = res + high * multi; } flag = true; dev *= 10; multi *= 10; high = n / dev; cur = (n - high *dev) / multi; low = n - high*dev - cur *multi; } return res; }
};